Git Product home page Git Product logo

Comments (3)

so1n avatar so1n commented on June 1, 2024

@anotherchudov

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.

ditsuke avatar ditsuke commented on June 1, 2024

@so1n by definition, doesn't oneof mean that all fields should be optional given one exists?

from protobuf_to_pydantic.

so1n avatar so1n commented on June 1, 2024

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.