protocol-buffersproto3

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!


Solution

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