mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-30 03:18:20 +00:00
protobuf: improve to_dict function
This commit is contained in:
parent
17f6c33c3f
commit
832053e85d
@ -414,12 +414,21 @@ def dict_to_proto(message_type, d):
|
|||||||
return message_type(**params)
|
return message_type(**params)
|
||||||
|
|
||||||
|
|
||||||
def to_dict(msg):
|
def to_dict(msg, hexlify_bytes=True):
|
||||||
|
def convert_value(value):
|
||||||
|
if hexlify_bytes and isinstance(value, bytes):
|
||||||
|
return value.hex()
|
||||||
|
elif isinstance(value, MessageType):
|
||||||
|
return to_dict(value, hexlify_bytes)
|
||||||
|
elif isinstance(value, list):
|
||||||
|
return [convert_value(v) for v in value]
|
||||||
|
else:
|
||||||
|
return value
|
||||||
|
|
||||||
res = {}
|
res = {}
|
||||||
for key, value in msg.__dict__.items():
|
for key, value in msg.__dict__.items():
|
||||||
if value is None or value == []:
|
if value is None or value == []:
|
||||||
continue
|
continue
|
||||||
if isinstance(value, MessageType):
|
res[key] = convert_value(value)
|
||||||
value = to_dict(value)
|
|
||||||
res[key] = value
|
|
||||||
return res
|
return res
|
||||||
|
Loading…
Reference in New Issue
Block a user