Skip to content

Commit 72a469c

Browse files
committed
feat: pydantic version of google pb
1 parent df1ba91 commit 72a469c

File tree

16 files changed

+5515
-1869
lines changed

16 files changed

+5515
-1869
lines changed

docs/migrating.rst

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,19 @@ wrappers used to provide optional zero value support. Each of these has a specia
8585
representation and is handled a little differently from normal messages. The Python
8686
mapping for these is as follows:
8787

88-
+-------------------------------+-----------------------------------------------+--------------------------+
89-
| ``Google Message`` | ``Python Type`` | ``Default`` |
90-
+===============================+===============================================+==========================+
91-
| ``google.protobuf.duration`` | :class:`datetime.timedelta` | ``0`` |
92-
+-------------------------------+-----------------------------------------------+--------------------------+
93-
| ``google.protobuf.timestamp`` | ``Timezone-aware`` :class:`datetime.datetime` | ``1970-01-01T00:00:00Z`` |
94-
+-------------------------------+-----------------------------------------------+--------------------------+
95-
| ``google.protobuf.*Value`` | ``Optional[...]``/``None`` | ``None`` |
96-
+-------------------------------+-----------------------------------------------+--------------------------+
97-
| ``google.protobuf.*`` | ``betterproto.lib.google.protobuf.*`` | ``None`` |
98-
+-------------------------------+-----------------------------------------------+--------------------------+
88+
+-------------------------------+-------------------------------------------------+--------------------------+
89+
| ``Google Message`` | ``Python Type`` | ``Default`` |
90+
+===============================+=================================================+==========================+
91+
| ``google.protobuf.duration`` | :class:`datetime.timedelta` | ``0`` |
92+
+-------------------------------+-------------------------------------------------+--------------------------+
93+
| ``google.protobuf.timestamp`` | ``Timezone-aware`` :class:`datetime.datetime` | ``1970-01-01T00:00:00Z`` |
94+
+-------------------------------+-------------------------------------------------+--------------------------+
95+
| ``google.protobuf.*Value`` | ``Optional[...]``/``None`` | ``None`` |
96+
+-------------------------------+-------------------------------------------------+--------------------------+
97+
| ``google.protobuf.*`` | ``betterproto.lib.std.google.protobuf.*`` | ``None`` |
98+
+-------------------------------+-------------------------------------------------+--------------------------+
99+
| ``google.protobuf.*`` | ``betterproto.lib.pydantic.google.protobuf.*`` | ``None`` |
100+
+-------------------------------+-------------------------------------------------+--------------------------+
99101

100102

101103
For the wrapper types, the Python type corresponds to the wrapped type, e.g.

gen.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
export PB_REPO=${1}
6+
if [ ! -d ${PB_REPO} ] || [ -z ${PB_REPO} ]; then export PB_REPO=/mnt/p0/Personal/protobuf/src; fi
7+
if [ -z ${PROTOC} ]; then export PROTOC=protoc; fi
8+
export FLAGS="${FLAGS} -I${PB_REPO}"
9+
10+
export SOURCES_PB="google/protobuf/any.proto google/protobuf/api.proto google/protobuf/descriptor.proto google/protobuf/duration.proto google/protobuf/empty.proto google/protobuf/field_mask.proto google/protobuf/source_context.proto google/protobuf/struct.proto google/protobuf/timestamp.proto google/protobuf/type.proto google/protobuf/wrappers.proto google/protobuf/compiler/plugin.proto"
11+
12+
export OUT_LIB=src/betterproto/lib
13+
export OUT_LIB_PYDANTIC=${OUT_LIB}/pydantic
14+
export OUT_LIB_STD=${OUT_LIB}/std
15+
16+
mkdir -p ${OUT_LIB_PYDANTIC} ${OUT_LIB_STD}
17+
touch ${OUT_LIB_PYDANTIC}/__init__.py ${OUT_LIB_STD}/__init__.py
18+
19+
export PBS=$(for pb_pkg_path in ${SOURCES_PB}; do echo "${PB_REPO}/${pb_pkg_path}"; done)
20+
21+
${PROTOC} ${FLAGS} \
22+
--python_betterproto_opt=pydantic_dataclasses,INCLUDE_GOOGLE \
23+
--python_betterproto_out=${OUT_LIB_PYDANTIC} \
24+
${PBS}
25+
26+
${PROTOC} ${FLAGS} \
27+
--python_betterproto_opt=INCLUDE_GOOGLE \
28+
--python_betterproto_out=${OUT_LIB_STD} \
29+
${PBS}

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ cmd = """
9595
protoc
9696
--plugin=protoc-gen-custom=src/betterproto/plugin/main.py
9797
--custom_opt=INCLUDE_GOOGLE
98-
--custom_out=src/betterproto/lib
98+
--custom_out=src/betterproto/lib/std
9999
-I /usr/local/include/
100100
/usr/local/include/google/protobuf/**/*.proto
101101
"""
102-
help = "Regenerate the types in betterproto.lib.google"
102+
help = "Regenerate the types in betterproto.lib.std.google"
103103

104104
# CI tasks
105105

src/betterproto/compile/importing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def parse_source_type_name(field_type_name: str) -> Tuple[str, str]:
4343

4444

4545
def get_type_reference(
46-
*, package: str, imports: set, source_type: str, unwrap: bool = True
46+
*, package: str, imports: set, source_type: str, unwrap: bool = True, pydantic: bool = False,
4747
) -> str:
4848
"""
4949
Return a Python type name for a proto type reference. Adds the import if
@@ -69,7 +69,7 @@ def get_type_reference(
6969
compiling_google_protobuf = current_package == ["google", "protobuf"]
7070
importing_google_protobuf = py_package == ["google", "protobuf"]
7171
if importing_google_protobuf and not compiling_google_protobuf:
72-
py_package = ["betterproto", "lib"] + py_package
72+
py_package = ["betterproto", "lib"] + (["pydantic"] if pydantic else []) + py_package
7373

7474
if py_package[:1] == ["betterproto"]:
7575
return reference_absolute(imports, py_package, py_type)

0 commit comments

Comments
 (0)