Skip to content

Commit 04fa18b

Browse files
committed
Struct support
1 parent b443e5d commit 04fa18b

4 files changed

Lines changed: 161 additions & 92 deletions

File tree

betterproto2/src/betterproto2/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@
138138
NEG_INFINITY = "-Infinity"
139139
NAN = "NaN"
140140

141+
# For Struct support
142+
JSON = int | float | bool | str | list["JSON"] | dict[str, "JSON"] | None
143+
141144

142145
class Casing(builtin_enum.Enum):
143146
"""Casing constants for serialization."""

betterproto2_compiler/pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ keywords = [
1515
requires-python = ">=3.10,<4.0"
1616
dependencies = [
1717
# TODO use the version from the current repo?
18-
"betterproto2[grpclib]>=0.7.0,<0.8",
18+
# "betterproto2[grpclib]>=0.7.0,<0.8",
19+
"betterproto2[grpclib]",
1920
"ruff~=0.9.3",
2021
"jinja2>=3.0.3",
2122
"typing-extensions>=4.7.1,<5",
2223
"strenum>=0.4.15,<0.5 ; python_version == '3.10'",
2324
]
2425

26+
[tool.uv.sources]
27+
"betterproto2" = { path = "../betterproto2" }
28+
2529
[project.urls]
2630
Documentation = "https://betterproto.github.io/python-betterproto2/"
2731
Repository = "https://github.com/betterproto/python-betterproto2"

betterproto2_compiler/src/betterproto2_compiler/known_types/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
UInt32Value,
1414
UInt64Value,
1515
)
16+
from .struct import ListValue, Struct, Value
1617
from .timestamp import Timestamp
1718

1819
# For each (package, message name), lists the methods that should be added to the message definition.
@@ -92,6 +93,24 @@
9293
BytesValue.from_wrapped,
9394
BytesValue.to_wrapped,
9495
],
96+
("google.protobuf", "Struct"): [
97+
Struct.from_dict,
98+
Struct.to_dict,
99+
Struct.from_wrapped,
100+
Struct.to_wrapped,
101+
],
102+
("google.protobuf", "ListValue"): [
103+
ListValue.from_dict,
104+
ListValue.to_dict,
105+
ListValue.from_wrapped,
106+
ListValue.to_wrapped,
107+
],
108+
("google.protobuf", "Value"): [
109+
Value.from_dict,
110+
Value.to_dict,
111+
Value.from_wrapped,
112+
Value.to_wrapped,
113+
],
95114
}
96115

97116
# A wrapped type is the type of a message that is automatically replaced by a known Python type.
@@ -107,4 +126,7 @@
107126
("google.protobuf", "BytesValue"): "bytes",
108127
("google.protobuf", "Timestamp"): "datetime.datetime",
109128
("google.protobuf", "Duration"): "datetime.timedelta",
129+
("google.protobuf", "Struct"): "dict[str, betterproto2.JSON]",
130+
("google.protobuf", "ListValue"): "list[betterproto2.JSON]",
131+
("google.protobuf", "Value"): "betterproto2.JSON",
110132
}

0 commit comments

Comments
 (0)