Skip to content

Commit ddeba26

Browse files
author
Your Name
committed
feat:support mysql field comment from desc
1 parent ce16a96 commit ddeba26

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
python3 -m grpc_tools.protoc --proto_path=. --proto_path=./ --python_out=./ --pyi_out=./ --grpc_python_out=./ ./protobuf_pydantic_gen/*.proto
22

3-
protoc --plugin=protoc-gen-custom=pydantic_protobuf_gen/main.py --custom_out=./models -I ./protos protos/example.proto protos/options.proto
3+
protoc --plugin=protoc-gen-custom=protobuf_pydantic_gen/main.py --custom_out=./models -I ./ -I ./protos protos/example.proto

models/example_model.py

+33-12
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,44 @@ class Example(SQLModel, table=True):
5858
default="John Doe",
5959
alias="full_name",
6060
primary_key=True,
61-
max_length=128)
62-
age: Optional[int] = Field(description="Age of the example", default=30, alias="years")
63-
emails: Optional[List[str]] = Field(description="Emails of the example", default=[])
64-
examples: Optional[List[Example2]] = Field(description="Nested message", default=[], sa_column=Column(JSON))
65-
entry: Optional[Dict[str, Any]] = Field(description="Properties of the example", default={}, sa_column=Column(JSON))
66-
nested: Optional[Nested] = Field(description="Nested message", sa_column=Column(JSON))
61+
max_length=128,
62+
sa_column_kwargs={
63+
'comment': '"Name of the example"'})
64+
age: Optional[int] = Field(
65+
description="Age of the example",
66+
default=30,
67+
alias="years",
68+
sa_column_kwargs={
69+
'comment': '"Age of the example"'})
70+
emails: Optional[List[str]] = Field(description="Emails of the example", default=[],
71+
sa_column_kwargs={'comment': '"Emails of the example"'})
72+
examples: Optional[List[Example2]] = Field(description="Nested message", default=[], sa_column=Column(
73+
JSON), sa_column_kwargs={'comment': '"Nested message"'})
74+
entry: Optional[Dict[str, Any]] = Field(description="Properties of the example", default={}, sa_column=Column(
75+
JSON), sa_column_kwargs={'comment': '"Properties of the example"'})
76+
nested: Optional[Nested] = Field(
77+
description="Nested message",
78+
sa_column=Column(JSON),
79+
sa_column_kwargs={
80+
'comment': '"Nested message"'})
6781
created_at: datetime.datetime = Field(
68-
description="Creation date of the example",
69-
default=datetime.datetime.now(),
70-
schema_extra={
71-
'required': True})
82+
description="Creation date of the example", default=datetime.datetime.now(), schema_extra={
83+
'required': True}, sa_column_kwargs={
84+
'comment': '"Creation date of the example"'})
7285
type: Optional[ExampleType] = Field(
7386
description="Type of the example",
7487
default=ExampleType.TYPE1,
7588
sa_column=Column(
76-
Enum[ExampleType]))
77-
score: Optional[float] = Field(description="Score of the example", default=0.0, le=100.0, sa_type=Integer)
89+
Enum[ExampleType]),
90+
sa_column_kwargs={
91+
'comment': '"Type of the example"'})
92+
score: Optional[float] = Field(
93+
description="Score of the example",
94+
default=0.0,
95+
le=100.0,
96+
sa_type=Integer,
97+
sa_column_kwargs={
98+
'comment': '"Score of the example"'})
7899

79100
def to_protobuf(self) -> _message.Message:
80101
_proto = pool.FindMessageTypeByName("pydantic_example.Example")

protobuf_pydantic_gen/main.py

+2
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,8 @@ def generate_code(request: plugin_pb2.CodeGeneratorRequest,
406406
sqlmodel_imports.add("JSON")
407407
sqlmodel_imports.add("Column")
408408
ext["sa_column"] = "Column(JSON)"
409+
if ext and ext.get("description") and msg_ext.get("as_table", False):
410+
ext["sa_column_kwargs"] = {"comment": ext["description"]}
409411

410412
attr = ",".join(f"{key}={value}" for key,
411413
value in ext.items())

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "protobuf-pydantic-gen"
3-
version = "0.1.4"
3+
version = "0.1.5"
44
description = "A tool for converting between Pydantic models and Protobuf messages, specifically enabling the generation of Pydantic BaseModel classes from .proto files."
55
authors = ["vforfreedom96 <[email protected]>"]
66
readme = "README.md"

0 commit comments

Comments
 (0)