mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-20 23:54:22 +01:00
msggen: Use the inferred optional field
Changelog-Changed: msggen: The generated interfaces `cln-rpc` anc `cln-grpc` can now work with a range of versions rather than having to match the CLN version
This commit is contained in:
committed by
Rusty Russell
parent
392cacac81
commit
60b12ec096
@@ -194,7 +194,7 @@ class GrpcGenerator(IGenerator):
|
||||
if overrides.get(f.path, "") is None:
|
||||
continue
|
||||
|
||||
opt = "optional " if not f.required else ""
|
||||
opt = "optional " if f.optional else ""
|
||||
if isinstance(f, ArrayField):
|
||||
typename = typemap.get(f.itemtype.typename, f.itemtype.typename)
|
||||
if f.path in overrides:
|
||||
@@ -288,18 +288,18 @@ class GrpcConverterGenerator(IGenerator):
|
||||
'secret': f'i.to_vec()',
|
||||
}.get(typ, f'i.into()')
|
||||
|
||||
if f.required:
|
||||
if not f.optional:
|
||||
self.write(f"{name}: c.{name}.into_iter().map(|i| {mapping}).collect(), // Rule #3 for type {typ}\n", numindent=3)
|
||||
else:
|
||||
self.write(f"{name}: c.{name}.map(|arr| arr.into_iter().map(|i| {mapping}).collect()).unwrap_or(vec![]), // Rule #3\n", numindent=3)
|
||||
elif isinstance(f, EnumField):
|
||||
if f.required:
|
||||
if not f.optional:
|
||||
self.write(f"{name}: c.{name} as i32,\n", numindent=3)
|
||||
else:
|
||||
self.write(f"{name}: c.{name}.map(|v| v as i32),\n", numindent=3)
|
||||
|
||||
elif isinstance(f, PrimitiveField):
|
||||
typ = f.typename + ("?" if not f.required else "")
|
||||
typ = f.typename + ("?" if f.optional else "")
|
||||
# We may need to reduce or increase the size of some
|
||||
# types, or have some conversion such as
|
||||
# hex-decoding. Also includes the `Some()` that grpc
|
||||
@@ -344,7 +344,7 @@ class GrpcConverterGenerator(IGenerator):
|
||||
|
||||
elif isinstance(f, CompositeField):
|
||||
rhs = ""
|
||||
if f.required:
|
||||
if not f.optional:
|
||||
rhs = f'Some(c.{name}.into())'
|
||||
else:
|
||||
rhs = f'c.{name}.map(|v| v.into())'
|
||||
@@ -446,7 +446,7 @@ class GrpcUnconverterGenerator(GrpcConverterGenerator):
|
||||
self.write(f" state_changes: None,")
|
||||
continue
|
||||
|
||||
if f.required:
|
||||
if not f.optional:
|
||||
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)
|
||||
@@ -454,13 +454,13 @@ class GrpcUnconverterGenerator(GrpcConverterGenerator):
|
||||
elif isinstance(f, EnumField):
|
||||
if f.path == 'ListPeers.peers[].channels[].htlcs[].state':
|
||||
continue
|
||||
if f.required:
|
||||
if not f.optional:
|
||||
self.write(f"{name}: c.{name}.try_into().unwrap(),\n", numindent=3)
|
||||
else:
|
||||
self.write(f"{name}: c.{name}.map(|v| v.try_into().unwrap()),\n", numindent=3)
|
||||
pass
|
||||
elif isinstance(f, PrimitiveField):
|
||||
typ = f.typename + ("?" if not f.required else "")
|
||||
typ = f.typename + ("?" if f.optional else "")
|
||||
# We may need to reduce or increase the size of some
|
||||
# types, or have some conversion such as
|
||||
# hex-decoding. Also includes the `Some()` that grpc
|
||||
@@ -503,7 +503,7 @@ class GrpcUnconverterGenerator(GrpcConverterGenerator):
|
||||
self.write(f"{name}: {rhs}, // Rule #1 for type {typ}\n", numindent=3)
|
||||
elif isinstance(f, CompositeField):
|
||||
rhs = ""
|
||||
if f.required:
|
||||
if not f.optional:
|
||||
rhs = f'c.{name}.unwrap().into()'
|
||||
else:
|
||||
rhs = f'c.{name}.map(|v| v.into())'
|
||||
|
||||
@@ -132,7 +132,7 @@ def gen_enum(e):
|
||||
decl = "" # No declaration if we have an override
|
||||
typename = overrides[e.path]
|
||||
|
||||
if e.required:
|
||||
if not e.optional:
|
||||
defi = f" // Path `{e.path}`\n"
|
||||
defi += rename_if_necessary(str(e.name), e.name.normalized())
|
||||
defi += f" pub {e.name.normalized()}: {typename},\n"
|
||||
@@ -152,7 +152,7 @@ def gen_primitive(p):
|
||||
if p.deprecated:
|
||||
defi += " #[deprecated]\n"
|
||||
defi += rename_if_necessary(org, p.name.name)
|
||||
if p.required:
|
||||
if not p.optional:
|
||||
defi += f" pub {p.name}: {typename},\n"
|
||||
else:
|
||||
defi += f" #[serde(skip_serializing_if = \"Option::is_none\")]\n pub {p.name}: Option<{typename}>,\n"
|
||||
@@ -191,7 +191,7 @@ def gen_array(a):
|
||||
if a.deprecated:
|
||||
defi += " #[deprecated]\n"
|
||||
defi += rename_if_necessary(alias, name)
|
||||
if a.required:
|
||||
if not a.optional:
|
||||
defi += f" pub {name}: {'Vec<'*a.dims}{itemtype}{'>'*a.dims},\n"
|
||||
else:
|
||||
defi += f" #[serde(skip_serializing_if = \"crate::is_none_or_empty\")]\n pub {name}: Option<{'Vec<'*a.dims}{itemtype}{'>'*a.dims}>,\n"
|
||||
@@ -216,7 +216,7 @@ def gen_composite(c) -> Tuple[str, str]:
|
||||
defi = ""
|
||||
if c.deprecated:
|
||||
defi += " #[deprecated]\n"
|
||||
if c.required:
|
||||
if not c.optional:
|
||||
defi += f" pub {c.name}: {c.typename},\n"
|
||||
else:
|
||||
defi += f" #[serde(skip_serializing_if = \"Option::is_none\")]\n pub {c.name}: Option<{c.typename}>,\n"
|
||||
|
||||
@@ -64,17 +64,23 @@ class VersionAnnotationPatch(Patch):
|
||||
# if f.added is None and 'added' not in m:
|
||||
# m['added'] = 'pre-v0.10.1'
|
||||
|
||||
assert m.get('added', None) is not None or f.added is not None, f"Field {f.path} does not have an `added` annotation"
|
||||
added = m.get('added', None)
|
||||
deprecated = m.get('deprecated', None)
|
||||
|
||||
assert added or not f.added, f"Field {f.path} does not have an `added` annotation"
|
||||
|
||||
# We do not allow the added and deprecated flags to be
|
||||
# modified after the fact.
|
||||
assert f.added is None or f.added == m['added']
|
||||
assert f.deprecated is None or f.deprecated == m.get('deprecated', None)
|
||||
if f.added and added and f.added != m['added']:
|
||||
raise ValueError(f"Field {f.path} changed `added` annotation: {f.added} != {m['added']}")
|
||||
|
||||
if f.deprecated and deprecated and f.deprecated != deprecated:
|
||||
raise ValueError(f"Field {f.path} changed `deprecated` annotation: {f.deprecated} != {m['deprecated']}")
|
||||
|
||||
if f.added is None:
|
||||
f.added = m['added']
|
||||
f.added = added
|
||||
if f.deprecated is None:
|
||||
f.deprecated = m.get('deprecated', None)
|
||||
f.deprecated = deprecated
|
||||
|
||||
# Backfill the metadata using the annotation
|
||||
self.meta['model-field-versions'][f.path] = {
|
||||
|
||||
636
contrib/pyln-testing/pyln/testing/node_pb2.py
generated
636
contrib/pyln-testing/pyln/testing/node_pb2.py
generated
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user