Comments (3)
If want fields a,b to be optional, then the contents of the protobuf should be changed to the following:
syntax = "proto3";
message A {
bool a = 1;
}
message B {
bool b = 1;
}
message C {
oneof z {
optional A a = 1;
optional B b = 2;
}
optional B c = 3;
}
But Protobuf doesn't support this syntax, I will consider another way to support a,b fields as optional, as follows:
syntax = "proto3";
message A {
bool a = 1;
}
message B {
bool b = 1;
}
message C {
oneof z {
option (p2p_validate.optional) = ["a", "b"];
A a = 1;
B b = 2;
}
optional B c = 3;
}
This feature will be implemented in the next version.
from protobuf_to_pydantic.
@so1n by definition, doesn't oneof
mean that all fields should be optional given one exists?
from protobuf_to_pydantic.
@ditsuke In Protobuf, oneof simply restricts a set of fields to only one that has a value, while other fields have default values (not null).
For example:
message OneOfOptionalTest {
string header = 1;
oneof id {
string x = 2;
int32 y = 3;
bool z = 4;
}
optional string name = 5;
optional int32 age= 6;
repeated string str_list = 7;
map<string, int32> int_map = 8;
}
The type of each property in the Python object generated by OneOfOptional is as follows:
field | type |
---|---|
x | str |
y | int |
z | bool |
name | str , None |
age | int, None |
str_list | list[str] |
int_map | dict[str, int] |
At runtime:
>>> demo1 = OneOfOptionalTest(x="1")
>>> demo1.x
... '1'
>>> demo2 = OneOfOptionalTest(x="1", y=2)
>>> demo2.x
... ''
>>> demo2.y
... 2
>>> demo3 = OneOfOptionalTest(name=None, age=3)
>>> demo3.name
... ''
>>> demo3.age
... 3
from protobuf_to_pydantic.
Related Issues (20)
- add `pass` to generated class when message body is empty HOT 1
- Support `BaseModel` serialization to `Protobuf Message`
- Genereted code does not pass mypy HOT 1
- Typos in the word "pydantic" in the README.md file HOT 1
- Add Pydantic V2 supoort
- Making a field required HOT 1
- Support of protobuf services HOT 1
- Optional fields do not generate correct defaults in Pydantic HOT 4
- default_factory and default generated together
- Integrate plugin with buf cli HOT 2
- ImportError: cannot import name 'PydanticGeneralMetadata' from 'pydantic._internal._fields' with latest pydantic HOT 1
- No module named 'mypy_protobuf' HOT 1
- py3.11 support
- [DELETED]
- Typo in readme.
- Missing typing import for optional field HOT 1
- reorder generated classes to avoid unresolved references HOT 3
- Optimize the generation of con* functions in the generated code HOT 1
- RecursionError: maximum recursion depth exceeded in __instancecheck__ HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from protobuf_to_pydantic.