DeX3
DeX3

Reputation: 5569

Protobuf: Nesting a message of arbitrary type

in short, is there a way to define a protobuf Message that contains another Message of arbitrary type? Something like:

message OuterMsg {
    required int32 type = 1;
    required Message nestedMsg = 2; //Any sort of message can go here
}

I suspect that there's a way to do this because in the various protobuf-implementations, compiled messages extend from a common Message base class.

Otherwise I guess I have to create a common base Message for all sorts of messages like this:

message BaseNestedMessage {
    extensions 1 to max;
}

and then do

message OuterMessage {
    required int32 type = 1;
    required BaseNestedMessage nestedMsg = 2;
}

Is this the only way to achieve this?

Upvotes: 8

Views: 13142

Answers (3)

Florian Wolters
Florian Wolters

Reputation: 4120

Alternatively to multiple optional fields, the oneof keyword can be used since v2.6 of Protocol Buffers.

message UnionMessage {
  oneof data {
    string a = 1;
    bytes b = 2;
    int32 c = 3;
  }
}

Upvotes: 3

jpa
jpa

Reputation: 12176

The most popular way to do is to make optional fields for each message type:

message UnionMessage
{
    optional MsgType1 msg1 = 1;
    optional MsgType2 msg2 = 2;
    optional MsgType3 msg3 = 3;
}

This technique is also described in the official Google documentation, and is well-supported across implementations: https://developers.google.com/protocol-buffers/docs/techniques#union

Upvotes: 10

Marc Gravell
Marc Gravell

Reputation: 1064014

Not directly, basically; protocol buffers very much wants to know the structure in advance, and the type of the message is not included on the wire. The common Message base-class is an implementation detail for providing common plumbing code - the protocol buffers specification does not include inheritance.

There are, therefore, limited options:

  • use different field-numbers per message-type
  • serialize the message separately, and include it as a bytes type, and convey the "what is this?" information separately (presumably a discriminator / enumeration)

I should also note that some implementations may provide more support for this; protobuf-net (C# / .NET) supports (separately) both inheritance and dynamic message-types (i.e. what you have above), but that is primarily intended for use only from that one library to that one library. Because this is all in addition to the specification (remaining 100% valid in terms of the wire format), it may be unnecessarily messy to interpret such data from other implementations.

Upvotes: 7

Related Questions