1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-01 20:08:26 +00:00

protobuf: set missing fields to None

This commit is contained in:
Jan Pochyla 2016-11-23 14:49:55 +01:00
parent a8b519d127
commit 065cbdbc3b

View File

@ -20,8 +20,8 @@ from micropython import const
from streams import StreamReader, BufferWriter from streams import StreamReader, BufferWriter
def build_protobuf_message(message_type, callback=None, *args): def build_protobuf_message(msg_type, callback=None, *args):
message = message_type() msg = msg_type()
try: try:
while True: while True:
field, fvalue = yield field, fvalue = yield
@ -29,14 +29,22 @@ def build_protobuf_message(message_type, callback=None, *args):
if issubclass(ftype, MessageType): if issubclass(ftype, MessageType):
fvalue = yield from build_protobuf_message(ftype) fvalue = yield from build_protobuf_message(ftype)
if fflags & FLAG_REPEATED: if fflags & FLAG_REPEATED:
prev_value = getattr(message, fname, []) prev_value = getattr(msg, fname, [])
prev_value.append(fvalue) prev_value.append(fvalue)
fvalue = prev_value fvalue = prev_value
setattr(message, fname, fvalue) setattr(msg, fname, fvalue)
except EOFError: except EOFError:
fill_missing_fields(msg)
if callback is not None: if callback is not None:
callback(message, *args) callback(msg, *args)
return message return msg
def fill_missing_fields(msg):
for tag in msg.FIELDS:
field = msg.FIELDS[tag]
if not hasattr(msg, field[0]):
setattr(msg, field[0], None)
class Type: class Type:
@ -189,10 +197,10 @@ class MessageType(Type):
return e.value return e.value
@classmethod @classmethod
async def dump(cls, message, target): async def dump(cls, msg, target):
for ftag in cls.FIELDS: for ftag in cls.FIELDS:
fname, ftype, fflags = cls.FIELDS[ftag] fname, ftype, fflags = cls.FIELDS[ftag]
fvalue = getattr(message, fname, None) fvalue = getattr(msg, fname, None)
if fvalue is None: if fvalue is None:
continue continue
key = (ftag << 3) | ftype.WIRE_TYPE key = (ftag << 3) | ftype.WIRE_TYPE