Skip to content

Commit

Permalink
Merge pull request #69 from so1n/0.3.0-pre
Browse files Browse the repository at this point in the history
0.3.0 pre
  • Loading branch information
so1n authored Oct 14, 2024
2 parents 479ed40 + 4fd5424 commit d100d66
Show file tree
Hide file tree
Showing 117 changed files with 2,860 additions and 1,551 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.0.0
rev: v1.8.0
hooks:
- id: mypy
exclude: "^example/example_proto|^example/proto|^protobuf_to_pydantic/protos|test_p2p_validate.py$|test_pgv_validate.py$|protobuf_to_pydantic/contrib/proto_parser.py|example/validate_example/demo_gen_code_by_pgv.py|example/p2p_validate_example/demo_gen_code_by_p2p.py|protobuf_to_pydantic/_pydantic_adapter.py|protobuf_to_pydantic/customer_validator/__init__.py|protobuf_to_pydantic/customer_validator/v1.py|protobuf_to_pydantic/customer_validator/v2.py|protobuf_to_pydantic/customer_con_type/__init__.py|protobuf_to_pydantic/customer_con_type/v1.py|protobuf_to_pydantic/customer_con_type/v2.py|protobuf_to_pydantic/grpc_types.py"
Expand Down
283 changes: 159 additions & 124 deletions README.md

Large diffs are not rendered by default.

292 changes: 164 additions & 128 deletions README_ZH.md

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions buf-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ Regardless of which method is used to generate `Pydantic` code, the final projec
└── pyproject.toml
```
### 1.4.Generating Pydantic Code with Remote Plugins
> Note: Since the BUF plugin created by the developer cannot be used publicly, the remote plug-in cannot be used normally(See: [issue](https://github.com/bufbuild/plugins/issues/589#issuecomment-1799085322))。If necessary, please refer to the custom plugins in the next section to create your own plugins。

In addition to executing local plugin to generate `Pydantic` code, can also use remote plugin.

First, need to change the `buf.gen.yaml` file to the following content:
Expand Down
4 changes: 3 additions & 1 deletion buf-plugin/README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ plugins:
└── pyproject.toml
```
### 1.4.使用远程插件生成Pydantic代码
> Note: 由于开发者自己创建的buf插件是无法公开使用的,所以远程插件无法正常使用(具体见:[issue](https://github.com/bufbuild/plugins/issues/589#issuecomment-1799085322))。如有需要,请参考下一节的自定义插件自己创建插件。

除了执行本地插件生成Pydantic代码外,还可以使用远程插件,首先需要更改`buf.gen.yaml`文件为如下内容:
```yaml
version: v1
Expand All @@ -166,7 +168,7 @@ plugins:



## 2.Custom Plugins
## 2.自定义插件
> Note: 请确保你已经阅读了 [buf custom plugins dev doc](https://buf.build/docs/bsr/remote-plugins/custom-plugins)

`protobuf-to-pydantic`的标准`buf-plugin`可以满足大多数功能。如果您有自定义需求,您应该创建属于自己的自定义插件。
Expand Down
16 changes: 16 additions & 0 deletions example/example_proto/demo/demo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,19 @@ message Invoice3 {
int32 quantity = 3;
repeated InvoiceItem2 items = 4;
}

// Test Message references
// from: https://github.com/so1n/protobuf_to_pydantic/issues/64
message RootMessage {
string field1 = 1;
AnOtherMessage field2 = 2;
}

message AnOtherMessage {
string field1 = 1;
SubMessage field2 = 2;

message SubMessage {
string text = 1;
}
}
17 changes: 17 additions & 0 deletions example/example_proto/demo/single_config.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
syntax = "proto3";
package single_config;


// user info
message UserMessage {
// aha: {"required": true, "example": "10086", "title": "UID", "description": "user union id"}
string uid=1;
// aha: {"example": 18, "title": "use age", "ge": 0}
int32 age=2;
// aha: {"ge": 0, "le": 2.5}
float height=3;
bool is_adult=7;
// aha: {"description": "user name"}
// aha: {"default": "", "min_length": 1, "max_length": "10", "example": "so1n"}
string user_name=8;
}
6 changes: 3 additions & 3 deletions example/gen_p2p_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pydantic import confloat, conint
from pydantic.fields import FieldInfo

from protobuf_to_pydantic import _pydantic_adapter, desc_template, msg_to_pydantic_model, pydantic_model_to_py_file
from protobuf_to_pydantic import _pydantic_adapter, msg_to_pydantic_model, pydantic_model_to_py_file, template

# use pydantic v1 method, pydantic will print warning, ignore!~
warnings.filterwarnings("ignore")
Expand Down Expand Up @@ -42,7 +42,7 @@ def customer_any() -> Any:
now_path: pathlib.Path = pathlib.Path(__file__).absolute()


class CustomDescTemplate(desc_template.DescTemplate):
class CustomCommentTemplate(template.Template):
def template_timestamp(self, length_str: str) -> int:
timestamp: float = 1600000000
if length_str == "10":
Expand All @@ -65,7 +65,7 @@ def gen_code() -> None:
"conint": conint,
"customer_any": customer_any,
},
desc_template=CustomDescTemplate,
template=CustomCommentTemplate,
)
for model in message_class_list
],
Expand Down
12 changes: 6 additions & 6 deletions example/gen_text_comment_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from google.protobuf.message import Message

from protobuf_to_pydantic import _pydantic_adapter, msg_to_pydantic_model, pydantic_model_to_py_file
from protobuf_to_pydantic.desc_template import DescTemplate
from protobuf_to_pydantic.template import Template


class CustomDescTemplate(DescTemplate):
class CustomCommentTemplate(Template):
def template_timestamp(self, length_str: str) -> int:
timestamp: float = 1600000000
if length_str == "10":
Expand Down Expand Up @@ -59,7 +59,7 @@ def gen_code() -> None:
model,
parse_msg_desc_method=module,
local_dict=local_dict,
desc_template=CustomDescTemplate,
template=CustomCommentTemplate,
)
for model in message_class_list
],
Expand All @@ -72,7 +72,7 @@ def gen_code() -> None:
model,
parse_msg_desc_method=str(now_path.parent.parent),
local_dict=local_dict,
desc_template=CustomDescTemplate,
template=CustomCommentTemplate,
)
for model in message_class_list
],
Expand All @@ -85,7 +85,7 @@ def gen_code() -> None:
model,
parse_msg_desc_method=module,
local_dict=local_dict,
desc_template=CustomDescTemplate,
template=CustomCommentTemplate,
)
for model in message_class_list
],
Expand All @@ -97,7 +97,7 @@ def gen_code() -> None:
model,
parse_msg_desc_method=str(now_path.parent.parent),
local_dict=local_dict,
desc_template=CustomDescTemplate,
template=CustomCommentTemplate,
)
for model in message_class_list
],
Expand Down
12 changes: 6 additions & 6 deletions example/p2p_validate_by_comment_gen_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
from pydantic.fields import FieldInfo

from protobuf_to_pydantic import _pydantic_adapter, msg_to_pydantic_model, pydantic_model_to_py_file
from protobuf_to_pydantic.desc_template import DescTemplate
from protobuf_to_pydantic.template import Template


class CustomDescTemplate(DescTemplate):
class CustomCommentTemplate(Template):
def template_timestamp(self, length_str: str) -> int:
timestamp: float = 1600000000
if length_str == "10":
Expand Down Expand Up @@ -77,7 +77,7 @@ def gen_code() -> None:
model,
parse_msg_desc_method=module,
local_dict=local_dict,
desc_template=CustomDescTemplate,
template=CustomCommentTemplate,
)
for model in message_class_list
],
Expand All @@ -92,7 +92,7 @@ def gen_code() -> None:
model,
parse_msg_desc_method=str(now_path.parent.parent),
local_dict=local_dict,
desc_template=CustomDescTemplate,
template=CustomCommentTemplate,
)
for model in message_class_list
],
Expand All @@ -105,7 +105,7 @@ def gen_code() -> None:
model,
parse_msg_desc_method=module,
local_dict=local_dict,
desc_template=CustomDescTemplate,
template=CustomCommentTemplate,
)
for model in message_class_list
],
Expand All @@ -119,7 +119,7 @@ def gen_code() -> None:
model,
parse_msg_desc_method=str(now_path.parent.parent),
local_dict=local_dict,
desc_template=CustomDescTemplate,
template=CustomCommentTemplate,
)
for model in message_class_list
],
Expand Down
12 changes: 8 additions & 4 deletions example/plugin_config.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import logging
import time
from typing import List, Type
from typing import Dict, List, Type
from uuid import uuid4

from google.protobuf.any_pb2 import Any # type: ignore
from pydantic import confloat, conint
from pydantic.fields import FieldInfo

from protobuf_to_pydantic.desc_template import DescTemplate
from protobuf_to_pydantic.plugin.config import SubConfigModel
from protobuf_to_pydantic.template import Template

from . import single_config_pkg_plugin_config

logging.basicConfig(format="[%(asctime)s %(levelname)s] %(message)s", datefmt="%y-%m-%d %H:%M:%S", level=logging.INFO)

Expand All @@ -20,7 +23,7 @@ def customer_any() -> Any:
return Any # type: ignore


class CustomDescTemplate(DescTemplate):
class CustomCommentTemplate(Template):
def template_timestamp(self, length_str: str) -> int:
timestamp: float = 1600000000
if length_str == "10":
Expand All @@ -44,5 +47,6 @@ def exp_time() -> float:
"uuid4": uuid4,
}
comment_prefix = "p2p"
desc_template: Type[DescTemplate] = CustomDescTemplate
template: Type[Template] = CustomCommentTemplate
ignore_pkg_list: List[str] = ["validate", "p2p_validate"]
pkg_config: Dict[str, SubConfigModel] = {"single_config": SubConfigModel(module=single_config_pkg_plugin_config)}
15 changes: 14 additions & 1 deletion example/proto_3_20_pydanticv1/demo_gen_code.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This is an automatically generated file, please do not change
# gen by protobuf_to_pydantic[v0.2.6](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[v0.2.7](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
Expand All @@ -17,6 +17,14 @@ class AfterReferMessage(BaseModel):
age: int = Field(default=0)


class AnOtherMessage(BaseModel):
class SubMessage(BaseModel):
text: str = Field(default="")

field1: str = Field(default="")
field2: SubMessage = Field()


class EmptyMessage(BaseModel):
pass

Expand Down Expand Up @@ -130,3 +138,8 @@ class Config:
metadata: typing.Dict[str, typing.Any] = Field(default_factory=dict)
double_value: DoubleValue = Field(default_factory=DoubleValue)
field_mask: typing.Optional[FieldMask] = Field(default_factory=FieldMask)


class RootMessage(BaseModel):
field1: str = Field(default="")
field2: AnOtherMessage = Field()
4 changes: 2 additions & 2 deletions example/proto_3_20_pydanticv1/demo_gen_code_by_p2p.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This is an automatically generated file, please do not change
# gen by protobuf_to_pydantic[v0.2.6](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[v0.2.7](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
Expand Down Expand Up @@ -42,7 +42,7 @@
timestamp_lt_validator,
timestamp_within_validator,
)
from protobuf_to_pydantic.get_desc.from_pb_option.types import HostNameStr, UriRefStr
from protobuf_to_pydantic.field_info_rule.protobuf_option_to_field_info.types import HostNameStr, UriRefStr
from protobuf_to_pydantic.util import Timedelta
from pydantic import BaseModel, Field, root_validator, validator
from pydantic.networks import AnyUrl, EmailStr, IPvAnyAddress
Expand Down
4 changes: 2 additions & 2 deletions example/proto_3_20_pydanticv1/demo_gen_code_by_pgv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This is an automatically generated file, please do not change
# gen by protobuf_to_pydantic[v0.2.6](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[v0.2.7](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
Expand Down Expand Up @@ -39,7 +39,7 @@
timestamp_lt_validator,
timestamp_within_validator,
)
from protobuf_to_pydantic.get_desc.from_pb_option.types import HostNameStr, UriRefStr
from protobuf_to_pydantic.field_info_rule.protobuf_option_to_field_info.types import HostNameStr, UriRefStr
from protobuf_to_pydantic.util import Timedelta
from pydantic import BaseModel, Field, root_validator, validator
from pydantic.networks import AnyUrl, EmailStr, IPvAnyAddress
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This is an automatically generated file, please do not change
# gen by protobuf_to_pydantic[v0.2.6](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[v0.2.7](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
Expand All @@ -20,6 +20,14 @@ class AfterReferMessage(BaseModel):
age: int = Field(default=0, title="use age", ge=0.0, example=18)


class AnOtherMessage(BaseModel):
class SubMessage(BaseModel):
text: str = Field(default="")

field1: str = Field(default="")
field2: SubMessage = Field()


class EmptyMessage(BaseModel):
pass

Expand Down Expand Up @@ -132,3 +140,8 @@ class Config:
metadata: typing.Dict[str, typing.Any] = Field(default_factory=dict)
double_value: DoubleValue = Field(default_factory=DoubleValue)
field_mask: typing.Optional[FieldMask] = Field(default_factory=FieldMask)


class RootMessage(BaseModel):
field1: str = Field(default="")
field2: AnOtherMessage = Field()
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This is an automatically generated file, please do not change
# gen by protobuf_to_pydantic[v0.2.6](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[v0.2.7](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
Expand All @@ -20,6 +20,14 @@ class AfterReferMessage(BaseModel):
age: int = Field(default=0, title="use age", ge=0.0, example=18)


class AnOtherMessage(BaseModel):
class SubMessage(BaseModel):
text: str = Field(default="")

field1: str = Field(default="")
field2: SubMessage = Field()


class EmptyMessage(BaseModel):
pass

Expand Down Expand Up @@ -132,3 +140,8 @@ class Config:
metadata: typing.Dict[str, typing.Any] = Field(default_factory=dict)
double_value: DoubleValue = Field(default_factory=DoubleValue)
field_mask: typing.Optional[FieldMask] = Field(default_factory=FieldMask)


class RootMessage(BaseModel):
field1: str = Field(default="")
field2: AnOtherMessage = Field()
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This is an automatically generated file, please do not change
# gen by protobuf_to_pydantic[v0.2.6](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[v0.2.7](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
from enum import IntEnum
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This is an automatically generated file, please do not change
# gen by protobuf_to_pydantic[v0.2.6](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[v0.2.7](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
Expand Down Expand Up @@ -142,3 +142,21 @@ class InvoiceItem2(BaseModel):
quantity: int = Field(default=0)
items: typing.List["InvoiceItem2"] = Field(default_factory=list)
invoice: Invoice3 = Field()


class AnOtherMessage(BaseModel):
class SubMessage(BaseModel):
text: str = Field(default="")

field1: str = Field(default="")
field2: SubMessage = Field()


class RootMessage(BaseModel):
"""
Test Message references
from: https://github.com/so1n/protobuf_to_pydantic/issues/64
"""

field1: str = Field(default="")
field2: AnOtherMessage = Field()
Loading

0 comments on commit d100d66

Please sign in to comment.