Skip to content

Commit

Permalink
Merge pull request #77 from so1n/76-unbalanced-when-processing-cases-…
Browse files Browse the repository at this point in the history
…like-same-name-but-in-diffrent-ds

76 unbalanced when processing cases like same name but in diffrent ds
  • Loading branch information
so1n authored Nov 18, 2024
2 parents 1a5555c + 709fabb commit 5f7a6d8
Show file tree
Hide file tree
Showing 75 changed files with 1,972 additions and 99 deletions.
18 changes: 18 additions & 0 deletions example/example_proto/demo/demo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,21 @@ message AnOtherMessage {
string text = 1;
}
}

// Test inline structure of the same name
// from: https://github.com/so1n/protobuf_to_pydantic/issues/76
message TestSameName0 {
message Body {
string input_model = 1;
map<string, string> input_info = 3;
}
Body body = 1;
}

message TestSameName1 {
message Body {
string output_model = 1;
map<string, string> output_info = 3;
}
Body body = 1;
}
2 changes: 1 addition & 1 deletion example/gen_simple_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
def gen_code() -> None:
pydantic_model_to_py_file(
str(now_path.parent.joinpath(target_p, "demo_gen_code.py")),
*[msg_to_pydantic_model(model) for model in message_class_list if model.__name__ == "OptionalMessage"],
*[msg_to_pydantic_model(model) for model in message_class_list],
)


Expand Down
133 changes: 132 additions & 1 deletion example/proto_3_20_pydanticv1/demo_gen_code.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,121 @@
# This is an automatically generated file, please do not change
# gen by protobuf_to_pydantic[v0.3.0](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[0.0.0](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
from datetime import datetime
from enum import IntEnum

from google.protobuf.field_mask_pb2 import FieldMask # type: ignore
from google.protobuf.wrappers_pb2 import DoubleValue # type: ignore
from protobuf_to_pydantic.customer_validator.v1 import check_one_of
from pydantic import BaseModel, Field, root_validator


class AfterReferMessage(BaseModel):
uid: str = Field(default="")
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


class InvoiceItem2(BaseModel):
name: str = Field(default="")
amount: int = Field(default=0)
quantity: int = Field(default=0)
items: typing.List["InvoiceItem2"] = Field(default_factory=list)
invoice: "Invoice3" = Field()


class Invoice3(BaseModel):
name: str = Field(default="")
amount: int = Field(default=0)
quantity: int = Field(default=0)
items: typing.List[InvoiceItem2] = Field(default_factory=list)


class InvoiceItem(BaseModel):
name: str = Field(default="")
amount: int = Field(default=0)
quantity: int = Field(default=0)
items: typing.List["InvoiceItem"] = Field(default_factory=list)


class SexType(IntEnum):
man = 0
women = 1


class ExampleExampleProtoCommonSingleDemoEnum(IntEnum):
"""Note: The current class does not belong to the package
ExampleExampleProtoCommonSingleDemoEnum protobuf path:example/example_proto/common/single.proto"""

zero = 0
one = 1
two = 3


class ExampleExampleProtoCommonSingleDemoMessage(BaseModel):
"""Note: The current class does not belong to the package
ExampleExampleProtoCommonSingleDemoMessage protobuf path:example/example_proto/common/single.proto"""

earth: str = Field(default="")
mercury: str = Field(default="")
mars: str = Field(default="")


class UserMessage(BaseModel):
uid: str = Field(default="")
age: int = Field(default=0)
height: float = Field(default=0.0)
sex: SexType = Field(default=0)
demo: ExampleExampleProtoCommonSingleDemoEnum = Field(default=0)
is_adult: bool = Field(default=False)
user_name: str = Field(default="")
demo_message: ExampleExampleProtoCommonSingleDemoMessage = Field()


class MapMessage(BaseModel):
user_map: typing.Dict[str, UserMessage] = Field(default_factory=dict)
user_flag: typing.Dict[str, bool] = Field(default_factory=dict)


class RepeatedMessage(BaseModel):
str_list: typing.List[str] = Field(default_factory=list)
int_list: typing.List[int] = Field(default_factory=list)
user_list: typing.List[UserMessage] = Field(default_factory=list)


class NestedMessage(BaseModel):
class UserPayMessage(BaseModel):
bank_number: str = Field(default="")
exp: datetime = Field(default_factory=datetime.now)
uuid: str = Field(default="")

class IncludeEnum(IntEnum):
zero = 0
one = 1
two = 2

user_list_map: typing.Dict[str, RepeatedMessage] = Field(default_factory=dict)
user_map: typing.Dict[str, MapMessage] = Field(default_factory=dict)
user_pay: UserPayMessage = Field()
include_enum: IncludeEnum = Field(default=0)
not_enable_user_pay: UserPayMessage = Field()
empty: typing.Any = Field()
after_refer: AfterReferMessage = Field()


class OptionalMessage(BaseModel):
_one_of_dict = {"user.OptionalMessage.a": {"fields": {"x", "y"}, "required": False}}

Expand All @@ -28,3 +129,33 @@ class OptionalMessage(BaseModel):
default_template_test: float = Field(default=0.0)

one_of_validator = root_validator(pre=True, allow_reuse=True)(check_one_of)


class OtherMessage(BaseModel):
class Config:
arbitrary_types_allowed = True

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()


class TestSameName0(BaseModel):
class Body(BaseModel):
input_model: str = Field(default="")
input_info: typing.Dict[str, str] = Field(default_factory=dict)

body: Body = Field()


class TestSameName1(BaseModel):
class Body(BaseModel):
output_model: str = Field(default="")
output_info: typing.Dict[str, str] = Field(default_factory=dict)

body: Body = Field()
2 changes: 1 addition & 1 deletion 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.3.0](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[0.0.0](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
Expand Down
2 changes: 1 addition & 1 deletion 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.3.0](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[0.0.0](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
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.3.0](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[0.0.0](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
Expand Down Expand Up @@ -145,3 +145,19 @@ class Config:
class RootMessage(BaseModel):
field1: str = Field(default="")
field2: AnOtherMessage = Field()


class TestSameName0(BaseModel):
class Body(BaseModel):
input_model: str = Field(default="")
input_info: typing.Dict[str, str] = Field(default_factory=dict)

body: Body = Field()


class TestSameName1(BaseModel):
class Body(BaseModel):
output_model: str = Field(default="")
output_info: typing.Dict[str, str] = Field(default_factory=dict)

body: Body = 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.3.0](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[0.0.0](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
Expand Down Expand Up @@ -145,3 +145,19 @@ class Config:
class RootMessage(BaseModel):
field1: str = Field(default="")
field2: AnOtherMessage = Field()


class TestSameName0(BaseModel):
class Body(BaseModel):
input_model: str = Field(default="")
input_info: typing.Dict[str, str] = Field(default_factory=dict)

body: Body = Field()


class TestSameName1(BaseModel):
class Body(BaseModel):
output_model: str = Field(default="")
output_info: typing.Dict[str, str] = Field(default_factory=dict)

body: Body = 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.3.0](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[0.0.0](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.3.0](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[0.0.0](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
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.3.0](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[0.0.0](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
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.3.0](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[0.0.0](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
Expand Down Expand Up @@ -160,3 +160,24 @@ class RootMessage(BaseModel):

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


class TestSameName0(BaseModel):
"""
Test inline structure of the same name
from: https://github.com/so1n/protobuf_to_pydantic/issues/76
"""

class Body(BaseModel):
input_model: str = Field(default="")
input_info: typing.Dict[str, str] = Field(default_factory=dict)

body: "TestSameName0.Body" = Field()


class TestSameName1(BaseModel):
class Body(BaseModel):
output_model: str = Field(default="")
output_info: typing.Dict[str, str] = Field(default_factory=dict)

body: "TestSameName1.Body" = Field()
Loading

0 comments on commit 5f7a6d8

Please sign in to comment.