Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: construct messages with nested struct #479

Merged
merged 8 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion proto/marshal/rules/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def to_proto(self, value):
try:
# Try the fast path first.
return self._descriptor(**value)
except TypeError as ex:
except (TypeError, ValueError) as ex:
parthea marked this conversation as resolved.
Show resolved Hide resolved
# If we have a type error,
parthea marked this conversation as resolved.
Show resolved Hide resolved
# try the slow path in case the error
# was an int64/string issue
Expand Down
31 changes: 1 addition & 30 deletions proto/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,36 +725,7 @@ def __init__(
"Unknown field for {}: {}".format(self.__class__.__name__, key)
)

try:
pb_value = marshal.to_proto(pb_type, value)
except ValueError:
# Underscores may be appended to field names
# that collide with python or proto-plus keywords.
# In case a key only exists with a `_` suffix, coerce the key
# to include the `_` suffix. It's not possible to
# natively define the same field with a trailing underscore in protobuf.
# See related issue
# https://github.com/googleapis/python-api-core/issues/227
if isinstance(value, dict):
if _upb:
# In UPB, pb_type is MessageMeta which doesn't expose attrs like it used to in Python/CPP.
keys_to_update = [
item
for item in value
if item not in pb_type.DESCRIPTOR.fields_by_name
and f"{item}_" in pb_type.DESCRIPTOR.fields_by_name
]
else:
keys_to_update = [
item
for item in value
if not hasattr(pb_type, item)
and hasattr(pb_type, f"{item}_")
]
for item in keys_to_update:
value[f"{item}_"] = value.pop(item)

pb_value = marshal.to_proto(pb_type, value)
pb_value = marshal.to_proto(pb_type, value)

if pb_value is not None:
params[key] = pb_value
Expand Down
23 changes: 23 additions & 0 deletions tests/test_marshal_types_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,26 @@ class Foo(proto.Message):
detached["bacon"] = True
foo.value = detached
assert foo.value == {"foo": "bar", "bacon": True}


def test_struct_nested():
class Foo(proto.Message):
struct_field: struct_pb2.Struct = proto.Field(
proto.MESSAGE,
number=1,
message=struct_pb2.Struct,
)

class Bar(proto.Message):
foo_field: Foo = proto.Field(
proto.MESSAGE,
number=1,
message=Foo,
)

foo = Foo({"struct_field": {"foo": "bagel"}})
assert foo.struct_field == {"foo": "bagel"}

bar = Bar({"foo_field": {"struct_field": {"foo": "cheese"}}})
assert bar.foo_field == Foo({"struct_field": {"foo": "cheese"}})
assert bar.foo_field.struct_field == {"foo": "cheese"}