Skip to content

Commit 8ed15a1

Browse files
committed
specialize from_dict and to_dict on Struct
...rather than special-casing in the Message ABC. Signed-off-by: William Woodruff <[email protected]>
1 parent c8e6244 commit 8ed15a1

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

src/betterproto/__init__.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,15 +1403,6 @@ def to_dict(
14031403
Dict[:class:`str`, Any]
14041404
The JSON serializable dict representation of this object.
14051405
"""
1406-
# Mirror of from_dict: Struct's `fields` member is transparently
1407-
# dispatched through instead.
1408-
if isinstance(self, Struct):
1409-
output = {**self.fields}
1410-
for k in self.fields:
1411-
if hasattr(self.fields[k], "to_dict"):
1412-
output[k] = self.fields[k].to_dict(casing, include_default_values)
1413-
return output
1414-
14151406
output: Dict[str, Any] = {}
14161407
field_types = self._type_hints()
14171408
defaults = self._betterproto.default_gen
@@ -1530,12 +1521,6 @@ def to_dict(
15301521

15311522
@classmethod
15321523
def _from_dict_init(cls, mapping: Mapping[str, Any]) -> Mapping[str, Any]:
1533-
# Special case: google.protobuf.Struct has a single fields member but
1534-
# behaves like a transparent JSON object, so it needs to first be munged
1535-
# into `{fields: mapping}`.
1536-
if cls == Struct:
1537-
mapping = {"fields": mapping}
1538-
15391524
init_kwargs: Dict[str, Any] = {}
15401525
for key, value in mapping.items():
15411526
field_name = safe_snake_case(key)
@@ -1566,7 +1551,7 @@ def _from_dict_init(cls, mapping: Mapping[str, Any]) -> Mapping[str, Any]:
15661551
if isinstance(value, list)
15671552
else sub_cls.from_dict(value)
15681553
)
1569-
elif meta.map_types and meta.map_types[1] == TYPE_MESSAGE and cls != Struct:
1554+
elif meta.map_types and meta.map_types[1] == TYPE_MESSAGE:
15701555
sub_cls = cls._betterproto.cls_by_field[f"{field_name}.value"]
15711556
value = {k: sub_cls.from_dict(v) for k, v in value.items()}
15721557
else:
@@ -1941,7 +1926,6 @@ def which_one_of(message: Message, group_name: str) -> Tuple[str, Optional[Any]]
19411926
Int32Value,
19421927
Int64Value,
19431928
StringValue,
1944-
Struct,
19451929
Timestamp,
19461930
UInt32Value,
19471931
UInt64Value,

src/betterproto/lib/google/protobuf/__init__.py

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test_struct.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ def test_struct_roundtrip():
1414

1515
struct_from_dict = Struct().from_dict(data)
1616
assert struct_from_dict.fields == data
17+
assert struct_from_dict.to_dict() == data
1718
assert struct_from_dict.to_json() == data_json
1819

1920
struct_from_json = Struct().from_json(data_json)
2021
assert struct_from_json.fields == data
22+
assert struct_from_json.to_dict() == data
2123
assert struct_from_json == struct_from_dict
2224
assert struct_from_json.to_json() == data_json

0 commit comments

Comments
 (0)