Skip to content

Commit

Permalink
betterproto: support Struct and Value (#551)
Browse files Browse the repository at this point in the history
* betterproto: support `Struct` and `Value`

Signed-off-by: William Woodruff <[email protected]>

* betterproto: handle struct in to_dict as well

Signed-off-by: William Woodruff <[email protected]>

* tests: add Struct roundtrip tests

Signed-off-by: William Woodruff <[email protected]>

* specialize from_dict and to_dict on Struct

...rather than special-casing in the Message ABC.

Signed-off-by: William Woodruff <[email protected]>

* betterproto: `poe format`

Signed-off-by: William Woodruff <[email protected]>

* Update src/betterproto/__init__.py

Co-authored-by: James Hilton-Balfe <[email protected]>

* remove future annotations

Signed-off-by: William Woodruff <[email protected]>

* replace type[...] with typing.T

Signed-off-by: William Woodruff <[email protected]>

* quote instead

Signed-off-by: William Woodruff <[email protected]>

---------

Signed-off-by: William Woodruff <[email protected]>
Co-authored-by: James Hilton-Balfe <[email protected]>
  • Loading branch information
woodruffw and Gobot1234 authored Jan 2, 2024
1 parent ce5093e commit 5666393
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
31 changes: 31 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

0 comments on commit 5666393

Please sign in to comment.