Skip to content

Commit

Permalink
Merge pull request #75 from so1n/74-issue-67-extended-populate-by-nam…
Browse files Browse the repository at this point in the history
…e-for-oneof

74 issue 67 extended populate by name for oneof
  • Loading branch information
so1n authored Nov 17, 2024
2 parents 953fb73 + b381ac9 commit e91625f
Show file tree
Hide file tree
Showing 103 changed files with 1,410 additions and 536 deletions.
30 changes: 30 additions & 0 deletions example/example_proto/demo/alias_demo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// fix issue: #74 https://github.com/so1n/protobuf_to_pydantic/issues/74
syntax = "proto3";
package alias_demo;

import "google/protobuf/timestamp.proto";



// Annotations are used in runtime mode
// p2p: {"oneof:data": {"required": true}}
// p2p: {"oneof:data": {"oneof_extend": {"optional": ["location_value"]}}}
message ReportData {
// Annotations are used in plugin mode
// p2p: {"required": true, "oneof_extend": {"optional": ["location_value"]}}
oneof data {
GeoLocation location_value = 5;
google.protobuf.Timestamp time_value = 6;
}
}

message GeoLocation {
float latitude = 1;
float longitude = 2;
optional double altitude_meters = 3;
}

message Report {
optional string source_id = 2;
ReportData data = 3;
}
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],
*[msg_to_pydantic_model(model) for model in message_class_list if model.__name__ == "OptionalMessage"],
)


Expand Down
3 changes: 2 additions & 1 deletion example/plugin_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from protobuf_to_pydantic.plugin.config import SubConfigModel
from protobuf_to_pydantic.template import Template

from . import all_field_set_option_config, single_config_pkg_plugin_config
from . import all_field_set_option_config, populate_by_name_plugin_config, 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 Down Expand Up @@ -52,4 +52,5 @@ def exp_time() -> float:
pkg_config: Dict[str, SubConfigModel] = {
"all_field_set_optional": SubConfigModel(module=all_field_set_option_config, use_root_config=True),
"single_config": SubConfigModel(module=single_config_pkg_plugin_config),
"alias_demo": SubConfigModel(module=populate_by_name_plugin_config, use_root_config=True),
}
31 changes: 31 additions & 0 deletions example/populate_by_name_plugin_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from pydantic import BaseModel, ConfigDict

from protobuf_to_pydantic._pydantic_adapter import VERSION

try:
from pydantic.alias_generators import to_camel
except ImportError:

def to_camel(string: str) -> str: # type: ignore[misc]
return "".join(word.capitalize() for word in string.split("_"))


if VERSION < "2.6.0":

class MyBaseSchema(BaseModel):
model_config = ConfigDict(
alias_generator=to_camel,
populate_by_name=True,
)

else:
from pydantic import AliasGenerator

class MyBaseSchema(BaseModel): # type: ignore[no-redef]
model_config = ConfigDict(
alias_generator=AliasGenerator(validation_alias=to_camel),
populate_by_name=True,
)


base_model_class = MyBaseSchema
117 changes: 1 addition & 116 deletions example/proto_3_20_pydanticv1/demo_gen_code.py
Original file line number Diff line number Diff line change
@@ -1,121 +1,20 @@
# This is an automatically generated file, please do not change
# gen by protobuf_to_pydantic[v0.2.7](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[v0.3.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 @@ -129,17 +28,3 @@ 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()
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.2.7](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[v0.3.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.2.7](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[v0.3.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.2.7](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[v0.3.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.2.7](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[v0.3.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.2.7](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[v0.3.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
@@ -0,0 +1,34 @@
# 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)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
from datetime import datetime

from google.protobuf.message import Message # type: ignore
from pydantic import Field, root_validator

from example.populate_by_name_plugin_config import MyBaseSchema
from protobuf_to_pydantic.customer_validator import check_one_of


class GeoLocation(MyBaseSchema):
latitude: float = Field(default=0.0)
longitude: float = Field(default=0.0)
altitude_meters: typing.Optional[float] = Field(default=0.0)


class ReportData(MyBaseSchema):
"""
Annotations are used in runtime mode
"""

_one_of_dict = {"ReportData.data": {"fields": {"location_value", "time_value"}, "required": True}}
one_of_validator = root_validator(pre=True, allow_reuse=True)(check_one_of)
location_value: typing.Optional[GeoLocation] = Field(default=None)
time_value: datetime = Field(default_factory=datetime.now)


class Report(MyBaseSchema):
source_id: typing.Optional[str] = Field(default="")
data: ReportData = Field()
Loading

0 comments on commit e91625f

Please sign in to comment.