Merge pull request #81 from discord/serialized_on_wire_repeated

Always set serialized_on_wire for all parsed message fields
This commit is contained in:
Bouke Versteegh 2020-07-08 23:10:14 +02:00 committed by GitHub
commit 8bcb67b66f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -724,6 +724,9 @@ class Message(ABC):
Parse the binary encoded Protobuf into this message instance. This Parse the binary encoded Protobuf into this message instance. This
returns the instance itself and is therefore assignable and chainable. returns the instance itself and is therefore assignable and chainable.
""" """
# Got some data over the wire
self._serialized_on_wire = True
for parsed in parse_fields(data): for parsed in parse_fields(data):
field_name = self._betterproto.field_name_by_number.get(parsed.number) field_name = self._betterproto.field_name_by_number.get(parsed.number)
if not field_name: if not field_name:

View File

@ -1,6 +1,6 @@
import betterproto import betterproto
from dataclasses import dataclass from dataclasses import dataclass
from typing import Optional from typing import Optional, List, Dict
def test_has_field(): def test_has_field():
@ -32,6 +32,25 @@ def test_has_field():
foo.bar = Bar() foo.bar = Bar()
assert betterproto.serialized_on_wire(foo.bar) is False assert betterproto.serialized_on_wire(foo.bar) is False
@dataclass
class WithCollections(betterproto.Message):
test_list: List[str] = betterproto.string_field(1)
test_map: Dict[str, str] = betterproto.map_field(
2, betterproto.TYPE_STRING, betterproto.TYPE_STRING
)
# Is always set from parse, even if all collections are empty
with_collections_empty = WithCollections().parse(bytes(WithCollections()))
assert betterproto.serialized_on_wire(with_collections_empty) == True
with_collections_list = WithCollections().parse(
bytes(WithCollections(test_list=["a", "b", "c"]))
)
assert betterproto.serialized_on_wire(with_collections_list) == True
with_collections_map = WithCollections().parse(
bytes(WithCollections(test_map={"a": "b", "c": "d"}))
)
assert betterproto.serialized_on_wire(with_collections_map) == True
def test_class_init(): def test_class_init():
@dataclass @dataclass