Skip to content

Commit cd39656

Browse files
committed
betterproto: support Struct and Value
Signed-off-by: William Woodruff <[email protected]>
1 parent b8a091a commit cd39656

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/betterproto/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
hybridmethod,
5656
)
5757

58-
5958
if TYPE_CHECKING:
6059
from _typeshed import (
6160
SupportsRead,
@@ -1522,6 +1521,12 @@ def to_dict(
15221521

15231522
@classmethod
15241523
def _from_dict_init(cls, mapping: Mapping[str, Any]) -> Mapping[str, Any]:
1524+
# Special case: google.protobuf.Struct has a single fields member but
1525+
# behaves like a transparent JSON object, so it needs to first be munged
1526+
# into `{fields: mapping}`.
1527+
if cls == Struct:
1528+
mapping = {"fields": mapping}
1529+
15251530
init_kwargs: Dict[str, Any] = {}
15261531
for key, value in mapping.items():
15271532
field_name = safe_snake_case(key)
@@ -1552,7 +1557,7 @@ def _from_dict_init(cls, mapping: Mapping[str, Any]) -> Mapping[str, Any]:
15521557
if isinstance(value, list)
15531558
else sub_cls.from_dict(value)
15541559
)
1555-
elif meta.map_types and meta.map_types[1] == TYPE_MESSAGE:
1560+
elif meta.map_types and meta.map_types[1] == TYPE_MESSAGE and cls != Struct:
15561561
sub_cls = cls._betterproto.cls_by_field[f"{field_name}.value"]
15571562
value = {k: sub_cls.from_dict(v) for k, v in value.items()}
15581563
else:
@@ -1582,6 +1587,7 @@ def _from_dict_init(cls, mapping: Mapping[str, Any]) -> Mapping[str, Any]:
15821587
)
15831588

15841589
init_kwargs[field_name] = value
1590+
15851591
return init_kwargs
15861592

15871593
@hybridmethod
@@ -1926,6 +1932,7 @@ def which_one_of(message: Message, group_name: str) -> Tuple[str, Optional[Any]]
19261932
Int32Value,
19271933
Int64Value,
19281934
StringValue,
1935+
Struct,
19291936
Timestamp,
19301937
UInt32Value,
19311938
UInt64Value,

0 commit comments

Comments
 (0)