How can we put a variant message ( one of a few message types ) inside a protobuf message?

后端 未结 2 1842
野的像风
野的像风 2020-12-17 17:56

How can we put a variant message ( one of a few message types ) inside a protobuf message?

message typeA {
    ....
}

message typeB {
    ....
}

message ty         


        
2条回答
  •  鱼传尺愫
    2020-12-17 18:47

    Check out the new oneof feature in version 2.6: https://developers.google.com/protocol-buffers/docs/reference/java-generated#oneof

    You can now do something like this:

    message TypeC {
        oneof oneof_name {
            TypeA a = 1;
            TypeB b = 2;
        }
    }
    

    Fields in the same oneof will share memory and only one field can be set at the same time.

提交回复
热议问题