Implement conversion JSON->GRPC also for requests type

This could be useful for existing software with  a JSON interface that want
to mimic the interface
This commit is contained in:
Riccardo Casatta 2023-01-11 10:54:32 +01:00 committed by Alex Myers
parent a71bd3ea37
commit 9c35f9c13a
3 changed files with 80 additions and 0 deletions

BIN
cln-grpc/src/convert.rs generated

Binary file not shown.

View File

@ -50,6 +50,20 @@ impl From<Feerate> for cln_rpc::primitives::Feerate {
}
}
impl From<cln_rpc::primitives::Feerate> for Feerate {
fn from(f: cln_rpc::primitives::Feerate) -> Feerate {
use feerate::Style;
let style = Some(match f {
JFeerate::Slow => Style::Slow(true),
JFeerate::Normal => Style::Normal(true),
JFeerate::Urgent => Style::Urgent(true),
JFeerate::PerKb(i) => Style::Perkb(i),
JFeerate::PerKw(i) => Style::Perkw(i),
});
Self { style }
}
}
impl From<OutputDesc> for JOutputDesc {
fn from(od: OutputDesc) -> JOutputDesc {
JOutputDesc {
@ -59,6 +73,15 @@ impl From<OutputDesc> for JOutputDesc {
}
}
impl From<JOutputDesc> for OutputDesc {
fn from(od: JOutputDesc) -> Self {
Self {
address: od.address,
amount: Some(od.amount.into()),
}
}
}
impl From<JAmountOrAll> for AmountOrAll {
fn from(a: JAmountOrAll) -> Self {
match a {
@ -131,6 +154,34 @@ impl From<RoutehintList> for cln_rpc::primitives::RoutehintList {
}
}
impl From<cln_rpc::primitives::Routehop> for RouteHop {
fn from(c: cln_rpc::primitives::Routehop) -> Self {
Self {
id: c.id.serialize().to_vec(),
feebase: Some(c.feebase.into()),
feeprop: c.feeprop,
expirydelta: c.expirydelta as u32,
short_channel_id: c.scid.to_string(),
}
}
}
impl From<cln_rpc::primitives::Routehint> for Routehint {
fn from(c: cln_rpc::primitives::Routehint) -> Self {
Self {
hops: c.hops.into_iter().map(|h| h.into()).collect(),
}
}
}
impl From<cln_rpc::primitives::RoutehintList> for RoutehintList {
fn from(c: cln_rpc::primitives::RoutehintList) -> Self {
Self {
hints: c.hints.into_iter().map(|e| e.into()).collect(),
}
}
}
impl From<TlvStream> for cln_rpc::primitives::TlvStream {
fn from(s: TlvStream) -> Self {
Self {
@ -148,6 +199,23 @@ impl From<TlvEntry> for cln_rpc::primitives::TlvEntry {
}
}
impl From<cln_rpc::primitives::TlvStream> for TlvStream {
fn from(s: cln_rpc::primitives::TlvStream) -> Self {
Self {
entries: s.entries.into_iter().map(|e| e.into()).collect(),
}
}
}
impl From<cln_rpc::primitives::TlvEntry> for TlvEntry {
fn from(e: cln_rpc::primitives::TlvEntry) -> Self {
Self {
r#type: e.typ,
value: e.value,
}
}
}
#[cfg(test)]
mod test {
use super::*;

View File

@ -322,6 +322,17 @@ class GrpcConverterGenerator(IGenerator):
'hash?': f'c.{name}.map(|v| v.to_vec())',
'secret': f'c.{name}.to_vec()',
'secret?': f'c.{name}.map(|v| v.to_vec())',
'msat_or_any': f'Some(c.{name}.into())',
'msat_or_all': f'Some(c.{name}.into())',
'msat_or_all?': f'c.{name}.map(|o|o.into())',
'feerate?': f'c.{name}.map(|o|o.into())',
'feerate': f'Some(c.{name}.into())',
'outpoint?': f'c.{name}.map(|o|o.into())',
'TlvStream?': f'c.{name}.map(|s| s.into())',
'RoutehintList?': f'c.{name}.map(|rl| rl.into())',
}.get(
typ,
f'c.{name}' # default to just assignment
@ -379,6 +390,7 @@ class GrpcConverterGenerator(IGenerator):
""")
self.generate_responses(service)
self.generate_requests(service)
def write(self, text: str, numindent: int = 0) -> None:
raw = dedent(text)