@@ -341,17 +341,19 @@ class ParsedField:
341
341
number : int
342
342
wire_type : int
343
343
value : Any
344
+ raw : bytes
344
345
345
346
346
347
def parse_fields (value : bytes ) -> Generator [ParsedField , None , None ]:
347
348
i = 0
348
349
while i < len (value ):
350
+ start = i
349
351
num_wire , i = decode_varint (value , i )
350
352
# print(num_wire, i)
351
353
number = num_wire >> 3
352
354
wire_type = num_wire & 0x7
353
355
354
- decoded : Any
356
+ decoded : Any = None
355
357
if wire_type == 0 :
356
358
decoded , i = decode_varint (value , i )
357
359
elif wire_type == 1 :
@@ -362,12 +364,12 @@ def parse_fields(value: bytes) -> Generator[ParsedField, None, None]:
362
364
i += length
363
365
elif wire_type == 5 :
364
366
decoded , i = value [i : i + 4 ], i + 4
365
- else :
366
- raise NotImplementedError (f"Wire type { wire_type } " )
367
367
368
368
# print(ParsedField(number=number, wire_type=wire_type, value=decoded))
369
369
370
- yield ParsedField (number = number , wire_type = wire_type , value = decoded )
370
+ yield ParsedField (
371
+ number = number , wire_type = wire_type , value = decoded , raw = value [start :i ]
372
+ )
371
373
372
374
373
375
# Bound type variable to allow methods to return `self` of subclasses
@@ -415,6 +417,7 @@ def __post_init__(self) -> None:
415
417
416
418
# Now that all the defaults are set, reset it!
417
419
self .__dict__ ["serialized_on_wire" ] = False
420
+ self .__dict__ ["_unknown_fields" ] = b""
418
421
419
422
def __setattr__ (self , attr : str , value : Any ) -> None :
420
423
if attr != "serialized_on_wire" :
@@ -469,7 +472,7 @@ def __bytes__(self) -> bytes:
469
472
meta .number , meta .proto_type , value , serialize_empty = serialize_empty
470
473
)
471
474
472
- return output
475
+ return output + self . _unknown_fields
473
476
474
477
# For compatibility with other libraries
475
478
SerializeToString = __bytes__
@@ -571,8 +574,7 @@ def parse(self: T, data: bytes) -> T:
571
574
else :
572
575
setattr (self , field .name , value )
573
576
else :
574
- # TODO: handle unknown fields
575
- pass
577
+ self ._unknown_fields += parsed .raw
576
578
577
579
return self
578
580
0 commit comments