Skip to content

betterproto: support Struct and Value #551

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

Merged
merged 9 commits into from
Jan 2, 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
1 change: 1 addition & 0 deletions src/betterproto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,7 @@ def _from_dict_init(cls, mapping: Mapping[str, Any]) -> Mapping[str, Any]:
)

init_kwargs[field_name] = value

return init_kwargs

@hybridmethod
Expand Down
33 changes: 33 additions & 0 deletions src/betterproto/lib/google/protobuf/__init__.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions tests/test_struct.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import json

from betterproto.lib.google.protobuf import Struct


def test_struct_roundtrip():
data = {
"foo": "bar",
"baz": None,
"quux": 123,
"zap": [1, {"two": 3}, "four"],
}
data_json = json.dumps(data)

struct_from_dict = Struct().from_dict(data)
assert struct_from_dict.fields == data
assert struct_from_dict.to_dict() == data
assert struct_from_dict.to_json() == data_json

struct_from_json = Struct().from_json(data_json)
assert struct_from_json.fields == data
assert struct_from_json.to_dict() == data
assert struct_from_json == struct_from_dict
assert struct_from_json.to_json() == data_json