Implement GRPC -> JSON conversions also for response types
This commit is contained in:
parent
9c35f9c13a
commit
21a8342289
File diff suppressed because it is too large
Load Diff
|
@ -254,6 +254,41 @@ pub enum ChannelSide {
|
||||||
REMOTE,
|
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 {
|
impl<'de> Deserialize<'de> for Amount {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
where
|
where
|
||||||
|
|
|
@ -405,6 +405,7 @@ class GrpcUnconverterGenerator(GrpcConverterGenerator):
|
||||||
"""
|
"""
|
||||||
def generate(self, service: Service):
|
def generate(self, service: Service):
|
||||||
self.generate_requests(service)
|
self.generate_requests(service)
|
||||||
|
self.generate_responses(service)
|
||||||
|
|
||||||
def generate_composite(self, prefix, field: CompositeField) -> None:
|
def generate_composite(self, prefix, field: CompositeField) -> None:
|
||||||
# First pass: generate any sub-fields before we generate the
|
# First pass: generate any sub-fields before we generate the
|
||||||
|
@ -436,12 +437,22 @@ class GrpcUnconverterGenerator(GrpcConverterGenerator):
|
||||||
'u32': f's',
|
'u32': f's',
|
||||||
'secret': f's.try_into().unwrap()'
|
'secret': f's.try_into().unwrap()'
|
||||||
}.get(typ, f's.into()')
|
}.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:
|
if f.required:
|
||||||
self.write(f"{name}: c.{name}.into_iter().map(|s| {mapping}).collect(), // Rule #4\n", numindent=3)
|
self.write(f"{name}: c.{name}.into_iter().map(|s| {mapping}).collect(), // Rule #4\n", numindent=3)
|
||||||
else:
|
else:
|
||||||
self.write(f"{name}: Some(c.{name}.into_iter().map(|s| {mapping}).collect()), // Rule #4\n", numindent=3)
|
self.write(f"{name}: Some(c.{name}.into_iter().map(|s| {mapping}).collect()), // Rule #4\n", numindent=3)
|
||||||
|
|
||||||
elif isinstance(f, EnumField):
|
elif isinstance(f, EnumField):
|
||||||
|
if f.path == 'ListPeers.peers[].channels[].htlcs[].state':
|
||||||
|
continue
|
||||||
if f.required:
|
if f.required:
|
||||||
self.write(f"{name}: c.{name}.try_into().unwrap(),\n", numindent=3)
|
self.write(f"{name}: c.{name}.try_into().unwrap(),\n", numindent=3)
|
||||||
else:
|
else:
|
||||||
|
@ -453,7 +464,12 @@ class GrpcUnconverterGenerator(GrpcConverterGenerator):
|
||||||
# types, or have some conversion such as
|
# types, or have some conversion such as
|
||||||
# hex-decoding. Also includes the `Some()` that grpc
|
# hex-decoding. Also includes the `Some()` that grpc
|
||||||
# requires for non-native types.
|
# requires for non-native types.
|
||||||
|
|
||||||
|
if name == "scriptPubKey":
|
||||||
|
name = "script_pub_key"
|
||||||
|
|
||||||
rhs = {
|
rhs = {
|
||||||
|
'u8': f'c.{name} as u8',
|
||||||
'u16': f'c.{name} as u16',
|
'u16': f'c.{name} as u16',
|
||||||
'u16?': f'c.{name}.map(|v| v as u16)',
|
'u16?': f'c.{name}.map(|v| v as u16)',
|
||||||
'hex': f'hex::encode(&c.{name})',
|
'hex': f'hex::encode(&c.{name})',
|
||||||
|
|
Loading…
Reference in New Issue