Skip to content

Commit 4ce0e72

Browse files
authored
Merge pull request #86 from so1n/85-any-way-to-default-initialize-protobuf-message-type-fields
Feat, Modify the default value of message type field and support requ…
2 parents 66c8a3a + bbefa81 commit 4ce0e72

File tree

103 files changed

+1472
-423
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+1472
-423
lines changed

demo.proto

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
syntax = "proto3";
2+
import "example/example_proto/common/p2p_validate.proto";
3+
package example;
4+
5+
message MyMessage1
6+
{
7+
optional string content = 1;
8+
}
9+
10+
message MyMessage2
11+
{
12+
// fix https://github.com/so1n/protobuf_to_pydantic/issues/82
13+
optional MyMessage1 my_message1 = 1[(p2p_validate.rules).message.required=true];
14+
// fix https://github.com/so1n/protobuf_to_pydantic/issues/85
15+
optional MyMessage1 my_message2 = 2;
16+
MyMessage1 my_message3 = 3[(p2p_validate.rules).message.required=true];
17+
MyMessage1 my_message4 = 4;
18+
}
19+
20+
message MyMessage3
21+
{
22+
// p2p: {"required": true}
23+
optional MyMessage1 my_message1 = 1;
24+
optional MyMessage1 my_message2 = 2;
25+
// p2p: {"required": true}
26+
MyMessage1 my_message3 = 3;
27+
MyMessage1 my_message4 = 4;
28+
}

demo_p2p.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This is an automatically generated file, please do not change
2+
# gen by protobuf_to_pydantic[0.0.0](https://github.com/so1n/protobuf_to_pydantic)
3+
# Protobuf Version: 4.24.4
4+
# Pydantic Version: 2.9.2
5+
import typing
6+
7+
from google.protobuf.message import Message # type: ignore
8+
from pydantic import BaseModel, Field
9+
10+
11+
class MyMessage1(BaseModel):
12+
content: typing.Optional[str] = Field(default="")
13+
14+
15+
class MyMessage2(BaseModel):
16+
my_message1: typing.Optional[MyMessage1] = Field()
17+
my_message2: typing.Optional[MyMessage1] = Field(default_factory=MyMessage1)
18+
my_message3: MyMessage1 = Field()
19+
my_message4: MyMessage1 = Field(default_factory=MyMessage1)
20+
21+
22+
class MyMessage3(BaseModel):
23+
my_message1: typing.Optional[MyMessage1] = Field()
24+
my_message2: typing.Optional[MyMessage1] = Field(default_factory=MyMessage1)
25+
my_message3: MyMessage1 = Field()
26+
my_message4: MyMessage1 = Field(default_factory=MyMessage1)

example/example_proto/p2p_validate/demo.proto

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,3 +599,13 @@ message AfterReferMessage {
599599
string uid=1 [(p2p_validate.rules).string = {example: "10086", title: "UID", description: "user union id", miss_default: true}];
600600
int32 age=2 [(p2p_validate.rules).int32 = {example: 18, title: "use age", ge: 0}];
601601
}
602+
603+
604+
message OptionalMessage{
605+
// fix https://github.com/so1n/protobuf_to_pydantic/issues/82
606+
optional MessageIgnoredTest my_message1 = 1[(p2p_validate.rules).message.required=true];
607+
// fix https://github.com/so1n/protobuf_to_pydantic/issues/85
608+
optional MessageIgnoredTest my_message2 = 2;
609+
MessageIgnoredTest my_message3 = 3[(p2p_validate.rules).message.required=true];
610+
MessageIgnoredTest my_message4 = 4;
611+
}

example/example_proto/p2p_validate_by_comment/demo.proto

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,3 +669,14 @@ message AfterReferMessage {
669669
// p2p: {"example": 18, "title": "use age", "ge": 0}
670670
int32 age=2;
671671
}
672+
673+
message OptionalMessage{
674+
// fix https://github.com/so1n/protobuf_to_pydantic/issues/82
675+
// p2p: {"required": true}
676+
optional MessageIgnoredTest my_message1 = 1;
677+
// fix https://github.com/so1n/protobuf_to_pydantic/issues/85
678+
optional MessageIgnoredTest my_message2 = 2;
679+
// p2p: {"required": true}
680+
MessageIgnoredTest my_message3 = 3;
681+
MessageIgnoredTest my_message4 = 4;
682+
}

example/proto_3_20_pydanticv1/demo_gen_code.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This is an automatically generated file, please do not change
2-
# gen by protobuf_to_pydantic[v0.3.0.3](https://github.com/so1n/protobuf_to_pydantic)
2+
# gen by protobuf_to_pydantic[v0.3.0.4](https://github.com/so1n/protobuf_to_pydantic)
33
# Protobuf Version: 3.20.3
44
# Pydantic Version: 1.10.7
55
import typing
@@ -22,7 +22,7 @@ class SubMessage(BaseModel):
2222
text: str = Field(default="")
2323

2424
field1: str = Field(default="")
25-
field2: SubMessage = Field()
25+
field2: SubMessage = Field(default_factory=SubMessage)
2626

2727

2828
class EmptyMessage(BaseModel):
@@ -82,7 +82,9 @@ class UserMessage(BaseModel):
8282
demo: ExampleExampleProtoCommonSingleDemoEnum = Field(default=0)
8383
is_adult: bool = Field(default=False)
8484
user_name: str = Field(default="")
85-
demo_message: ExampleExampleProtoCommonSingleDemoMessage = Field()
85+
demo_message: ExampleExampleProtoCommonSingleDemoMessage = Field(
86+
default_factory=ExampleExampleProtoCommonSingleDemoMessage
87+
)
8688

8789

8890
class MapMessage(BaseModel):
@@ -109,11 +111,11 @@ class IncludeEnum(IntEnum):
109111

110112
user_list_map: typing.Dict[str, RepeatedMessage] = Field(default_factory=dict)
111113
user_map: typing.Dict[str, MapMessage] = Field(default_factory=dict)
112-
user_pay: UserPayMessage = Field()
114+
user_pay: UserPayMessage = Field(default_factory=UserPayMessage)
113115
include_enum: IncludeEnum = Field(default=0)
114-
not_enable_user_pay: UserPayMessage = Field()
116+
not_enable_user_pay: UserPayMessage = Field(default_factory=UserPayMessage)
115117
empty: typing.Any = Field()
116-
after_refer: AfterReferMessage = Field()
118+
after_refer: AfterReferMessage = Field(default_factory=AfterReferMessage)
117119

118120

119121
class OptionalMessage(BaseModel):
@@ -123,7 +125,7 @@ class OptionalMessage(BaseModel):
123125
y: int = Field(default=0)
124126
name: typing.Optional[str] = Field(default="")
125127
age: typing.Optional[int] = Field(default=0)
126-
item: typing.Optional[InvoiceItem] = Field()
128+
item: typing.Optional[InvoiceItem] = Field(default_factory=InvoiceItem)
127129
str_list: typing.List[str] = Field(default_factory=list)
128130
int_map: typing.Dict[str, int] = Field(default_factory=dict)
129131
default_template_test: float = Field(default=0.0)
@@ -142,23 +144,23 @@ class Config:
142144

143145
class RootMessage(BaseModel):
144146
field1: str = Field(default="")
145-
field2: AnOtherMessage = Field()
147+
field2: AnOtherMessage = Field(default_factory=AnOtherMessage)
146148

147149

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

153-
body: Body = Field()
155+
body: Body = Field(default_factory=Body)
154156

155157

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

161-
body: Body = Field()
163+
body: Body = Field(default_factory=Body)
162164

163165

164166
class Demo1(BaseModel):

example/proto_3_20_pydanticv1/demo_gen_code_by_p2p.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This is an automatically generated file, please do not change
2-
# gen by protobuf_to_pydantic[v0.3.0.3](https://github.com/so1n/protobuf_to_pydantic)
2+
# gen by protobuf_to_pydantic[v0.3.0.4](https://github.com/so1n/protobuf_to_pydantic)
33
# Protobuf Version: 3.20.3
44
# Pydantic Version: 1.10.7
55
import typing
@@ -435,10 +435,10 @@ class NotEnableUserPayMessage(BaseModel):
435435

436436
string_in_map_test: typing.Dict[str, StringTest] = Field(default_factory=dict)
437437
map_in_map_test: typing.Dict[str, MapTest] = Field(default_factory=dict)
438-
user_pay: UserPayMessage = Field()
439-
not_enable_user_pay: NotEnableUserPayMessage = Field()
438+
user_pay: UserPayMessage = Field(default_factory=UserPayMessage)
439+
not_enable_user_pay: NotEnableUserPayMessage = Field(default_factory=NotEnableUserPayMessage)
440440
empty: typing.Any = Field()
441-
after_refer: AfterReferMessage = Field()
441+
after_refer: AfterReferMessage = Field(default_factory=AfterReferMessage)
442442

443443

444444
class OneOfNotTest(BaseModel):
@@ -476,6 +476,13 @@ class OneOfTest(BaseModel):
476476
one_of_validator = root_validator(pre=True, allow_reuse=True)(check_one_of)
477477

478478

479+
class OptionalMessage(BaseModel):
480+
my_message1: typing.Optional[MessageIgnoredTest] = Field()
481+
my_message2: typing.Optional[MessageIgnoredTest] = Field(default_factory=MessageIgnoredTest)
482+
my_message3: MessageIgnoredTest = Field()
483+
my_message4: MessageIgnoredTest = Field(default_factory=MessageIgnoredTest)
484+
485+
479486
class RepeatedTest(BaseModel):
480487
range_test: typing.List[str] = Field(default_factory=list, min_items=1, max_items=5)
481488
unique_test: typing.List[str] = Field(default_factory=list, unique_items=True)

example/proto_3_20_pydanticv1/demo_gen_code_by_pgv.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This is an automatically generated file, please do not change
2-
# gen by protobuf_to_pydantic[v0.3.0.3](https://github.com/so1n/protobuf_to_pydantic)
2+
# gen by protobuf_to_pydantic[v0.3.0.4](https://github.com/so1n/protobuf_to_pydantic)
33
# Protobuf Version: 3.20.3
44
# Pydantic Version: 1.10.7
55
import typing
@@ -290,10 +290,10 @@ class NotEnableUserPayMessage(BaseModel):
290290

291291
string_in_map_test: typing.Dict[str, StringTest] = Field(default_factory=dict)
292292
map_in_map_test: typing.Dict[str, MapTest] = Field(default_factory=dict)
293-
user_pay: UserPayMessage = Field()
294-
not_enable_user_pay: NotEnableUserPayMessage = Field()
293+
user_pay: UserPayMessage = Field(default_factory=UserPayMessage)
294+
not_enable_user_pay: NotEnableUserPayMessage = Field(default_factory=NotEnableUserPayMessage)
295295
empty: typing.Any = Field()
296-
after_refer: AfterReferMessage = Field()
296+
after_refer: AfterReferMessage = Field(default_factory=AfterReferMessage)
297297

298298

299299
class OneOfNotTest(BaseModel):

example/proto_3_20_pydanticv1/demo_gen_code_by_text_comment_protobuf_field.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This is an automatically generated file, please do not change
2-
# gen by protobuf_to_pydantic[v0.3.0.3](https://github.com/so1n/protobuf_to_pydantic)
2+
# gen by protobuf_to_pydantic[v0.3.0.4](https://github.com/so1n/protobuf_to_pydantic)
33
# Protobuf Version: 3.20.3
44
# Pydantic Version: 1.10.7
55
import typing
@@ -25,7 +25,7 @@ class SubMessage(BaseModel):
2525
text: str = Field(default="")
2626

2727
field1: str = Field(default="")
28-
field2: SubMessage = Field()
28+
field2: SubMessage = Field(default_factory=SubMessage)
2929

3030

3131
class EmptyMessage(BaseModel):
@@ -85,7 +85,9 @@ class UserMessage(BaseModel):
8585
demo: ExampleExampleProtoCommonSingleDemoEnum = Field(default=0)
8686
is_adult: bool = Field(default=False)
8787
user_name: str = Field(default="", description="user name", min_length=1, max_length=10, example="so1n")
88-
demo_message: ExampleExampleProtoCommonSingleDemoMessage = Field(customer_string="c1", customer_int=1)
88+
demo_message: ExampleExampleProtoCommonSingleDemoMessage = Field(
89+
default_factory=ExampleExampleProtoCommonSingleDemoMessage, customer_string="c1", customer_int=1
90+
)
8991

9092

9193
class MapMessage(BaseModel):
@@ -112,10 +114,10 @@ class IncludeEnum(IntEnum):
112114

113115
user_list_map: typing.Dict[str, RepeatedMessage] = Field(default_factory=dict)
114116
user_map: typing.Dict[str, MapMessage] = Field(default_factory=dict)
115-
user_pay: UserPayMessage = Field()
117+
user_pay: UserPayMessage = Field(default_factory=UserPayMessage)
116118
include_enum: IncludeEnum = Field(default=0)
117119
empty: typing.Any = Field()
118-
after_refer: AfterReferMessage = Field()
120+
after_refer: AfterReferMessage = Field(default_factory=AfterReferMessage)
119121

120122

121123
class OptionalMessage(BaseModel):
@@ -125,7 +127,7 @@ class OptionalMessage(BaseModel):
125127
y: int = Field(default=0, alias="yy", title="use age", ge=0.0, example=18)
126128
name: typing.Optional[str] = Field(default="")
127129
age: typing.Optional[int] = Field(default=0)
128-
item: typing.Optional[InvoiceItem] = Field()
130+
item: typing.Optional[InvoiceItem] = Field(default_factory=InvoiceItem)
129131
str_list: typing.List[str] = Field(default_factory=list)
130132
int_map: typing.Dict[str, int] = Field(default_factory=dict)
131133
default_template_test: float = Field(default=1600000000.0)
@@ -144,20 +146,20 @@ class Config:
144146

145147
class RootMessage(BaseModel):
146148
field1: str = Field(default="")
147-
field2: AnOtherMessage = Field()
149+
field2: AnOtherMessage = Field(default_factory=AnOtherMessage)
148150

149151

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

155-
body: Body = Field()
157+
body: Body = Field(default_factory=Body)
156158

157159

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

163-
body: Body = Field()
165+
body: Body = Field(default_factory=Body)

example/proto_3_20_pydanticv1/demo_gen_code_by_text_comment_pyi.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This is an automatically generated file, please do not change
2-
# gen by protobuf_to_pydantic[v0.3.0.3](https://github.com/so1n/protobuf_to_pydantic)
2+
# gen by protobuf_to_pydantic[v0.3.0.4](https://github.com/so1n/protobuf_to_pydantic)
33
# Protobuf Version: 3.20.3
44
# Pydantic Version: 1.10.7
55
import typing
@@ -25,7 +25,7 @@ class SubMessage(BaseModel):
2525
text: str = Field(default="")
2626

2727
field1: str = Field(default="")
28-
field2: SubMessage = Field()
28+
field2: SubMessage = Field(default_factory=SubMessage)
2929

3030

3131
class EmptyMessage(BaseModel):
@@ -85,7 +85,9 @@ class UserMessage(BaseModel):
8585
demo: ExampleExampleProtoCommonSingleDemoEnum = Field(default=0)
8686
is_adult: bool = Field(default=False)
8787
user_name: str = Field(default="", description="user name", min_length=1, max_length=10, example="so1n")
88-
demo_message: ExampleExampleProtoCommonSingleDemoMessage = Field(customer_string="c1", customer_int=1)
88+
demo_message: ExampleExampleProtoCommonSingleDemoMessage = Field(
89+
default_factory=ExampleExampleProtoCommonSingleDemoMessage, customer_string="c1", customer_int=1
90+
)
8991

9092

9193
class MapMessage(BaseModel):
@@ -112,10 +114,10 @@ class IncludeEnum(IntEnum):
112114

113115
user_list_map: typing.Dict[str, RepeatedMessage] = Field(default_factory=dict)
114116
user_map: typing.Dict[str, MapMessage] = Field(default_factory=dict)
115-
user_pay: UserPayMessage = Field()
117+
user_pay: UserPayMessage = Field(default_factory=UserPayMessage)
116118
include_enum: IncludeEnum = Field(default=0)
117119
empty: typing.Any = Field()
118-
after_refer: AfterReferMessage = Field()
120+
after_refer: AfterReferMessage = Field(default_factory=AfterReferMessage)
119121

120122

121123
class OptionalMessage(BaseModel):
@@ -125,7 +127,7 @@ class OptionalMessage(BaseModel):
125127
y: int = Field(default=0, alias="yy", title="use age", ge=0.0, example=18)
126128
name: typing.Optional[str] = Field(default="")
127129
age: typing.Optional[int] = Field(default=0)
128-
item: typing.Optional[InvoiceItem] = Field()
130+
item: typing.Optional[InvoiceItem] = Field(default_factory=InvoiceItem)
129131
str_list: typing.List[str] = Field(default_factory=list)
130132
int_map: typing.Dict[str, int] = Field(default_factory=dict)
131133
default_template_test: float = Field(default=1600000000.0)
@@ -144,20 +146,20 @@ class Config:
144146

145147
class RootMessage(BaseModel):
146148
field1: str = Field(default="")
147-
field2: AnOtherMessage = Field()
149+
field2: AnOtherMessage = Field(default_factory=AnOtherMessage)
148150

149151

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

155-
body: Body = Field()
157+
body: Body = Field(default_factory=Body)
156158

157159

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

163-
body: Body = Field()
165+
body: Body = Field(default_factory=Body)

example/proto_3_20_pydanticv1/example/example_proto/common/single_p2p.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This is an automatically generated file, please do not change
2-
# gen by protobuf_to_pydantic[v0.3.0.3](https://github.com/so1n/protobuf_to_pydantic)
2+
# gen by protobuf_to_pydantic[v0.3.0.4](https://github.com/so1n/protobuf_to_pydantic)
33
# Protobuf Version: 3.20.3
44
# Pydantic Version: 1.10.7
55
from enum import IntEnum

0 commit comments

Comments
 (0)