Skip to content

Commit

Permalink
fix: support pydantic-version google pb (#568)
Browse files Browse the repository at this point in the history
* feat: pydantic version of google pb

* fix: patch pb Struct to support json, dict rountrip

* fix: pydantic-version google pb, json, dict rntrip

* chore: remove `@generated`, remove gen, code fmt

* chore: test case for pydantic-version google pb
  • Loading branch information
ii64 authored Apr 8, 2024
1 parent df1ba91 commit c3c2055
Show file tree
Hide file tree
Showing 16 changed files with 5,606 additions and 1,869 deletions.
24 changes: 13 additions & 11 deletions docs/migrating.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,19 @@ wrappers used to provide optional zero value support. Each of these has a specia
representation and is handled a little differently from normal messages. The Python
mapping for these is as follows:

+-------------------------------+-----------------------------------------------+--------------------------+
| ``Google Message`` | ``Python Type`` | ``Default`` |
+===============================+===============================================+==========================+
| ``google.protobuf.duration`` | :class:`datetime.timedelta` | ``0`` |
+-------------------------------+-----------------------------------------------+--------------------------+
| ``google.protobuf.timestamp`` | ``Timezone-aware`` :class:`datetime.datetime` | ``1970-01-01T00:00:00Z`` |
+-------------------------------+-----------------------------------------------+--------------------------+
| ``google.protobuf.*Value`` | ``Optional[...]``/``None`` | ``None`` |
+-------------------------------+-----------------------------------------------+--------------------------+
| ``google.protobuf.*`` | ``betterproto.lib.google.protobuf.*`` | ``None`` |
+-------------------------------+-----------------------------------------------+--------------------------+
+-------------------------------+-------------------------------------------------+--------------------------+
| ``Google Message`` | ``Python Type`` | ``Default`` |
+===============================+=================================================+==========================+
| ``google.protobuf.duration`` | :class:`datetime.timedelta` | ``0`` |
+-------------------------------+-------------------------------------------------+--------------------------+
| ``google.protobuf.timestamp`` | ``Timezone-aware`` :class:`datetime.datetime` | ``1970-01-01T00:00:00Z`` |
+-------------------------------+-------------------------------------------------+--------------------------+
| ``google.protobuf.*Value`` | ``Optional[...]``/``None`` | ``None`` |
+-------------------------------+-------------------------------------------------+--------------------------+
| ``google.protobuf.*`` | ``betterproto.lib.std.google.protobuf.*`` | ``None`` |
+-------------------------------+-------------------------------------------------+--------------------------+
| ``google.protobuf.*`` | ``betterproto.lib.pydantic.google.protobuf.*`` | ``None`` |
+-------------------------------+-------------------------------------------------+--------------------------+


For the wrapper types, the Python type corresponds to the wrapped type, e.g.
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ cmd = """
protoc
--plugin=protoc-gen-custom=src/betterproto/plugin/main.py
--custom_opt=INCLUDE_GOOGLE
--custom_out=src/betterproto/lib
--custom_out=src/betterproto/lib/std
-I /usr/local/include/
/usr/local/include/google/protobuf/**/*.proto
"""
help = "Regenerate the types in betterproto.lib.google"
help = "Regenerate the types in betterproto.lib.std.google"

# CI tasks

Expand Down
11 changes: 9 additions & 2 deletions src/betterproto/compile/importing.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ def parse_source_type_name(field_type_name: str) -> Tuple[str, str]:


def get_type_reference(
*, package: str, imports: set, source_type: str, unwrap: bool = True
*,
package: str,
imports: set,
source_type: str,
unwrap: bool = True,
pydantic: bool = False,
) -> str:
"""
Return a Python type name for a proto type reference. Adds the import if
Expand All @@ -69,7 +74,9 @@ def get_type_reference(
compiling_google_protobuf = current_package == ["google", "protobuf"]
importing_google_protobuf = py_package == ["google", "protobuf"]
if importing_google_protobuf and not compiling_google_protobuf:
py_package = ["betterproto", "lib"] + py_package
py_package = (
["betterproto", "lib"] + (["pydantic"] if pydantic else []) + py_package
)

if py_package[:1] == ["betterproto"]:
return reference_absolute(imports, py_package, py_type)
Expand Down
Loading

0 comments on commit c3c2055

Please sign in to comment.