generate composite fields in grpc

This commit is contained in:
Jesse de Wit 2022-12-24 11:32:58 +01:00 committed by Christian Decker
parent 42e038b9ad
commit 241cd8d012
6 changed files with 44 additions and 4 deletions

Binary file not shown.

BIN
cln-grpc/src/convert.rs generated

Binary file not shown.

BIN
cln-rpc/src/model.rs generated

Binary file not shown.

View File

@ -210,6 +210,11 @@ class GrpcGenerator(IGenerator):
if f.path in overrides:
typename = overrides[f.path]
self.write(f"\t{opt}{typename} {f.normalized()} = {i};\n", False)
elif isinstance(f, CompositeField):
typename = f.typename
if f.path in overrides:
typename = overrides[f.path]
self.write(f"\t{opt}{typename} {f.normalized()} = {i};\n", False)
self.write(f"""}}
""")
@ -256,11 +261,14 @@ class GrpcConverterGenerator(IGenerator):
for f in field.fields:
if isinstance(f, ArrayField):
self.generate_array(prefix, f)
elif isinstance(f, CompositeField):
self.generate_composite(prefix, f)
pbname = self.to_camel_case(field.typename)
# And now we can convert the current field:
self.write(f"""\
#[allow(unused_variables)]
impl From<{prefix}::{field.typename}> for pb::{field.typename} {{
impl From<{prefix}::{field.typename}> for pb::{pbname} {{
fn from(c: {prefix}::{field.typename}) -> Self {{
Self {{
""")
@ -321,6 +329,13 @@ class GrpcConverterGenerator(IGenerator):
self.write(f"{name}: {rhs}, // Rule #2 for type {typ}\n", numindent=3)
elif isinstance(f, CompositeField):
rhs = ""
if f.required:
rhs = f'Some(c.{name}.into())'
else:
rhs = f'c.{name}.map(|v| v.into())'
self.write(f"{name}: {rhs},\n", numindent=3)
self.write(f"""\
}}
}}
@ -328,6 +343,12 @@ class GrpcConverterGenerator(IGenerator):
""")
def to_camel_case(self, snake_str):
components = snake_str.split('_')
# We capitalize the first letter of each component except the first one
# with the 'title' method and join them together.
return components[0] + ''.join(x.title() for x in components[1:])
def generate_requests(self, service):
for meth in service.methods:
req = meth.request
@ -380,12 +401,15 @@ class GrpcUnconverterGenerator(GrpcConverterGenerator):
for f in field.fields:
if isinstance(f, ArrayField):
self.generate_array(prefix, f)
elif isinstance(f, CompositeField):
self.generate_composite(prefix, f)
pbname = self.to_camel_case(field.typename)
# And now we can convert the current field:
self.write(f"""\
#[allow(unused_variables)]
impl From<pb::{field.typename}> for {prefix}::{field.typename} {{
fn from(c: pb::{field.typename}) -> Self {{
impl From<pb::{pbname}> for {prefix}::{field.typename} {{
fn from(c: pb::{pbname}) -> Self {{
Self {{
""")
@ -446,6 +470,13 @@ class GrpcUnconverterGenerator(GrpcConverterGenerator):
f'c.{name}' # default to just assignment
)
self.write(f"{name}: {rhs}, // Rule #1 for type {typ}\n", numindent=3)
elif isinstance(f, CompositeField):
rhs = ""
if f.required:
rhs = f'c.{name}.unwrap().into()'
else:
rhs = f'c.{name}.map(|v| v.into())'
self.write(f"{name}: {rhs},\n", numindent=3)
self.write(f"""\
}}

View File

@ -201,7 +201,16 @@ def gen_composite(c) -> Tuple[str, str]:
r += "".join([f[0] for f in fields])
r += "}\n\n"
return ("", r)
defi = ""
if c.deprecated:
defi += " #[deprecated]\n"
if c.required:
defi += f" #[serde(alias = \"{c.name.name}\")]\n pub {c.name}: {c.typename},\n"
else:
defi += f" #[serde(alias = \"{c.name.name}\", skip_serializing_if = \"Option::is_none\")]\n pub {c.name}: Option<{c.typename}>,\n"
return defi, r
class RustGenerator(IGenerator):

Binary file not shown.