rust-protobuf icon indicating copy to clipboard operation
rust-protobuf copied to clipboard

Implement Oneof::descriptor in generated code

Open stepancheg opened this issue 3 years ago • 7 comments
trafficstars

stepancheg avatar Apr 04 '22 01:04 stepancheg

Will this allow doing

msg.subOneOfMsg.descriptor().get_name()

?

esseswann avatar Apr 11 '22 18:04 esseswann

That operation would be not very useful: you already know what is descriptor name.

This would be useful in reflection (e.g. find all oneofs of a &dyn MessageDyn) and print their names.

stepancheg avatar Apr 28 '22 16:04 stepancheg

Our usecase is to serialize the enum's name to relational database in a manner similar to internally tagged enums and store it as a separate column

esseswann avatar Apr 29 '22 17:04 esseswann

@esseswann it should be possible now already, just with oneof descriptor it should be a little more convenient. Can you please describe, e.g. if you have

message Foo {
  int32 bar = 1;
  oneof baz {
    string qux = 2;
    bool quux = 3;
  }
}

// and the protobuf message
bar: 1
qux: "test"

how it should be serialized to the database?

stepancheg avatar Apr 29 '22 19:04 stepancheg

create table foos {
  bar integer,
  baz_type text,
  baz_value jsonb
};

insert into foos(bar, baz_type, baz_value) values(1, 'qux', 'test');

This way one can achieve proper indexing by baz_type and compensate lack of polymorphism in relational databases by using json field for arbitrary data

esseswann avatar May 01 '22 09:05 esseswann

I have created code example for your use case https://github.com/stepancheg/rust-protobuf/commit/f96a00c0a49ddb0a52116b633d28b2e25995edc6

stepancheg avatar May 01 '22 22:05 stepancheg

Perfect, thank you so much

esseswann avatar May 03 '22 09:05 esseswann