Implement GRPC -> JSON conversions also for response types

This commit is contained in:
Riccardo Casatta 2023-01-12 11:14:47 +01:00 committed by Alex Myers
parent 9c35f9c13a
commit 21a8342289
3 changed files with 1159 additions and 0 deletions

1108
cln-grpc/src/convert.rs generated

File diff suppressed because it is too large Load Diff

View File

@ -254,6 +254,41 @@ pub enum ChannelSide {
REMOTE,
}
impl TryFrom<i32> for ChannelSide {
type Error = crate::Error;
fn try_from(value: i32) -> std::result::Result<Self, Self::Error> {
match value {
0 => Ok(ChannelSide::LOCAL),
1 => Ok(ChannelSide::REMOTE),
_ => Err(anyhow!(
"Invalid ChannelSide mapping, only 0 or 1 are allowed"
)),
}
}
}
impl TryFrom<i32> for ChannelState {
type Error = crate::Error;
fn try_from(value: i32) -> std::result::Result<Self, Self::Error> {
match value {
0 => Ok(ChannelState::OPENINGD),
1 => Ok(ChannelState::CHANNELD_AWAITING_LOCKIN),
2 => Ok(ChannelState::CHANNELD_NORMAL),
3 => Ok(ChannelState::CHANNELD_SHUTTING_DOWN),
4 => Ok(ChannelState::CLOSINGD_SIGEXCHANGE),
5 => Ok(ChannelState::CLOSINGD_COMPLETE),
6 => Ok(ChannelState::AWAITING_UNILATERAL),
7 => Ok(ChannelState::FUNDING_SPEND_SEEN),
8 => Ok(ChannelState::ONCHAIN),
9 => Ok(ChannelState::DUALOPEND_OPEN_INIT),
10 => Ok(ChannelState::DUALOPEND_AWAITING_LOCKIN),
_ => Err(anyhow!("Invalid channel state {}", value)),
}
}
}
impl<'de> Deserialize<'de> for Amount {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where

View File

@ -405,6 +405,7 @@ class GrpcUnconverterGenerator(GrpcConverterGenerator):
"""
def generate(self, service: Service):
self.generate_requests(service)
self.generate_responses(service)
def generate_composite(self, prefix, field: CompositeField) -> None:
# First pass: generate any sub-fields before we generate the
@ -436,12 +437,22 @@ class GrpcUnconverterGenerator(GrpcConverterGenerator):
'u32': f's',
'secret': f's.try_into().unwrap()'
}.get(typ, f's.into()')
# TODO fix properly
if typ in ["ListtransactionsTransactionsType"]:
continue
if name == 'state_changes':
self.write(f" state_changes: None,")
continue
if f.required:
self.write(f"{name}: c.{name}.into_iter().map(|s| {mapping}).collect(), // Rule #4\n", numindent=3)
else:
self.write(f"{name}: Some(c.{name}.into_iter().map(|s| {mapping}).collect()), // Rule #4\n", numindent=3)
elif isinstance(f, EnumField):
if f.path == 'ListPeers.peers[].channels[].htlcs[].state':
continue
if f.required:
self.write(f"{name}: c.{name}.try_into().unwrap(),\n", numindent=3)
else:
@ -453,7 +464,12 @@ class GrpcUnconverterGenerator(GrpcConverterGenerator):
# types, or have some conversion such as
# hex-decoding. Also includes the `Some()` that grpc
# requires for non-native types.
if name == "scriptPubKey":
name = "script_pub_key"
rhs = {
'u8': f'c.{name} as u8',
'u16': f'c.{name} as u16',
'u16?': f'c.{name}.map(|v| v as u16)',
'hex': f'hex::encode(&c.{name})',