mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 05:12:45 +01:00
pyln.proto.message: expose array types, add set_field for Message class.
Exposing the array types is required for our dummyrunner in the lnprototest suite, since it wants to be able to generate fake fields. The set_field is similarly useful. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
f52065201b
commit
fd3ea91b44
@ -1,3 +1,4 @@
|
|||||||
|
from .array_types import SizedArrayType, DynamicArrayType, EllipsisArrayType
|
||||||
from .message import MessageNamespace, MessageType, Message, SubtypeType
|
from .message import MessageNamespace, MessageType, Message, SubtypeType
|
||||||
from .fundamental_types import split_field, FieldType
|
from .fundamental_types import split_field, FieldType
|
||||||
|
|
||||||
@ -10,6 +11,9 @@ __all__ = [
|
|||||||
"SubtypeType",
|
"SubtypeType",
|
||||||
"FieldType",
|
"FieldType",
|
||||||
"split_field",
|
"split_field",
|
||||||
|
"SizedArrayType",
|
||||||
|
"DynamicArrayType",
|
||||||
|
"EllipsisArrayType",
|
||||||
|
|
||||||
# fundamental_types
|
# fundamental_types
|
||||||
'byte',
|
'byte',
|
||||||
|
@ -545,22 +545,24 @@ class Message(object):
|
|||||||
|
|
||||||
# Convert arguments from strings to values if necessary.
|
# Convert arguments from strings to values if necessary.
|
||||||
for field in kwargs:
|
for field in kwargs:
|
||||||
f = self.messagetype.find_field(field)
|
self.set_field(field, kwargs[field])
|
||||||
if f is None:
|
|
||||||
raise ValueError("Unknown field {}".format(field))
|
|
||||||
|
|
||||||
v = kwargs[field]
|
|
||||||
if isinstance(v, str):
|
|
||||||
v, remainder = f.fieldtype.val_from_str(v)
|
|
||||||
if remainder != '':
|
|
||||||
raise ValueError('Unexpected {} at end of initializer for {}'.format(remainder, field))
|
|
||||||
self.fields[field] = v
|
|
||||||
|
|
||||||
bad_lens = self.messagetype.len_fields_bad(self.messagetype.name,
|
bad_lens = self.messagetype.len_fields_bad(self.messagetype.name,
|
||||||
self.fields)
|
self.fields)
|
||||||
if bad_lens:
|
if bad_lens:
|
||||||
raise ValueError("Inconsistent length fields: {}".format(bad_lens))
|
raise ValueError("Inconsistent length fields: {}".format(bad_lens))
|
||||||
|
|
||||||
|
def set_field(self, field: str, val: Any) -> None:
|
||||||
|
f = self.messagetype.find_field(field)
|
||||||
|
if f is None:
|
||||||
|
raise ValueError("Unknown field {}".format(field))
|
||||||
|
|
||||||
|
if isinstance(val, str):
|
||||||
|
val, remainder = f.fieldtype.val_from_str(val)
|
||||||
|
if remainder != '':
|
||||||
|
raise ValueError('Unexpected {} at end of initializer for {}'.format(remainder, field))
|
||||||
|
self.fields[field] = val
|
||||||
|
|
||||||
def missing_fields(self) -> List[str]:
|
def missing_fields(self) -> List[str]:
|
||||||
"""Are any required fields missing?"""
|
"""Are any required fields missing?"""
|
||||||
missing: List[str] = []
|
missing: List[str] = []
|
||||||
|
Loading…
Reference in New Issue
Block a user