1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 07:28:10 +00:00

feat: Add support for experimental field flag in protobuf.

This commit is contained in:
Andrew Kozlik 2020-10-07 18:18:03 +02:00 committed by Andrew Kozlik
parent b213a55428
commit 9e5d5bd5f9
4 changed files with 20 additions and 0 deletions

View File

@ -32,6 +32,12 @@ extend google.protobuf.MessageOptions {
} }
/** Options for tagging field types */
extend google.protobuf.FieldOptions {
optional bool experimental = 52001; // indicate that a field is intended for development and beta testing only
}
/** /**
* Mapping between Trezor wire identifier (uint) and a protobuf message * Mapping between Trezor wire identifier (uint) and a protobuf message
*/ */

View File

@ -65,6 +65,7 @@ class ProtoField:
orig = attr.ib() orig = attr.ib()
repeated = attr.ib() repeated = attr.ib()
required = attr.ib() required = attr.ib()
experimental = attr.ib()
type_name = attr.ib() type_name = attr.ib()
proto_type = attr.ib() proto_type = attr.ib()
py_type = attr.ib() py_type = attr.ib()
@ -78,6 +79,7 @@ class ProtoField:
def from_field(cls, descriptor, field): def from_field(cls, descriptor, field):
repeated = field.label == field.LABEL_REPEATED repeated = field.label == field.LABEL_REPEATED
required = field.label == field.LABEL_REQUIRED required = field.label == field.LABEL_REQUIRED
experimental = bool(descriptor._get_extension(field, "experimental"))
# ignore package path # ignore package path
type_name = field.type_name.rsplit(".")[-1] type_name = field.type_name.rsplit(".")[-1]
@ -115,6 +117,7 @@ class ProtoField:
orig=field, orig=field,
repeated=repeated, repeated=repeated,
required=required, required=required,
experimental=experimental,
type_name=type_name, type_name=type_name,
proto_type=proto_type, proto_type=proto_type,
py_type=py_type, py_type=py_type,
@ -306,6 +309,8 @@ class Descriptor:
flags = "p.FLAG_REPEATED" flags = "p.FLAG_REPEATED"
elif field.required: elif field.required:
flags = "p.FLAG_REQUIRED" flags = "p.FLAG_REQUIRED"
elif field.experimental:
flags = "p.FLAG_EXPERIMENTAL"
else: else:
flags = field.default_value flags = field.default_value

View File

@ -179,6 +179,7 @@ class LimitedReader:
FLAG_REPEATED = object() FLAG_REPEATED = object()
FLAG_REQUIRED = object() FLAG_REQUIRED = object()
FLAG_EXPERIMENTAL = object()
if False: if False:
MessageTypeDef = Union[ MessageTypeDef = Union[
@ -220,6 +221,8 @@ def load_message(
for fname, _, fdefault in fields.values(): for fname, _, fdefault in fields.values():
if fdefault is FLAG_REPEATED: if fdefault is FLAG_REPEATED:
fdefault = [] fdefault = []
elif fdefault is FLAG_EXPERIMENTAL:
fdefault = None
setattr(msg, fname, fdefault) setattr(msg, fname, fdefault)
if False: if False:
@ -252,6 +255,9 @@ def load_message(
if wtype != ftype.WIRE_TYPE: if wtype != ftype.WIRE_TYPE:
raise TypeError # parsed wire type differs from the schema raise TypeError # parsed wire type differs from the schema
if fdefault is FLAG_EXPERIMENTAL and not experimental_enabled:
raise ValueError # experimental fields not enabled
ivalue = load_uvarint(reader) ivalue = load_uvarint(reader)
if ftype is UVarintType: if ftype is UVarintType:

View File

@ -280,6 +280,7 @@ class CountingWriter:
FLAG_REPEATED = object() FLAG_REPEATED = object()
FLAG_REQUIRED = object() FLAG_REQUIRED = object()
FLAG_EXPERIMENTAL = object()
def decode_packed_array_field(ftype: FieldType, reader: Reader) -> List[Any]: def decode_packed_array_field(ftype: FieldType, reader: Reader) -> List[Any]:
@ -334,6 +335,8 @@ def load_message(reader: Reader, msg_type: Type[MT]) -> MT:
for fname, _, fdefault in fields.values(): for fname, _, fdefault in fields.values():
if fdefault is FLAG_REPEATED: if fdefault is FLAG_REPEATED:
msg_dict[fname] = [] msg_dict[fname] = []
elif fdefault is FLAG_EXPERIMENTAL:
msg_dict[fname] = None
elif fdefault is not FLAG_REQUIRED: elif fdefault is not FLAG_REQUIRED:
msg_dict[fname] = fdefault msg_dict[fname] = fdefault