Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I define an ordered mixed message array in proto3?

I want to define in proto3 an ordered list of unrelated classes (messages) like this:

  1. Frog
  2. Dirt
  3. Air
  4. Computer 1
  5. Computer 2
  6. Politics

Is it possible? I can also live with having a base class (base message) if that exists in proto3... Its not clear to mean if the feature set of proto3 allows this. Thanks!

like image 579
visc Avatar asked Mar 03 '26 15:03

visc


1 Answers

The typical way of representing this would be

message Wrapper {
    oneof Thing {
        Frog frog = 1;
        //...
        Politics politics = 6;
    }
}

and use repeated Wrapper for the list/array. There is no one-step repeated oneof.

Alternatively, you could just use

repeated Frog frogs = 1;
//...
repeated Politics politics = 6;

However this second layout cannot preserve the order between different kinds of element.

like image 145
Marc Gravell Avatar answered Mar 06 '26 13:03

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!