mirror of
https://github.com/ElementsProject/lightning.git
synced 2024-11-19 09:54:16 +01:00
generate composite fields in grpc
This commit is contained in:
parent
42e038b9ad
commit
241cd8d012
BIN
cln-grpc/proto/node.proto
generated
BIN
cln-grpc/proto/node.proto
generated
Binary file not shown.
BIN
cln-grpc/src/convert.rs
generated
BIN
cln-grpc/src/convert.rs
generated
Binary file not shown.
BIN
cln-rpc/src/model.rs
generated
BIN
cln-rpc/src/model.rs
generated
Binary file not shown.
@ -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"""\
|
||||
}}
|
||||
|
@ -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):
|
||||
|
BIN
contrib/pyln-testing/pyln/testing/node_pb2.py
generated
BIN
contrib/pyln-testing/pyln/testing/node_pb2.py
generated
Binary file not shown.
Loading…
Reference in New Issue
Block a user