-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from so1n/74-issue-67-extended-populate-by-nam…
…e-for-oneof 74 issue 67 extended populate by name for oneof
- Loading branch information
Showing
103 changed files
with
1,410 additions
and
536 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
example/proto_3_20_pydanticv1/demo_gen_code_by_text_comment_protobuf_field.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
example/proto_3_20_pydanticv1/demo_gen_code_by_text_comment_pyi.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
example/proto_3_20_pydanticv1/example/example_proto/common/single_p2p.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
example/proto_3_20_pydanticv1/example/example_proto/demo/alias_demo_p2p.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.