Marcin Bobowski
Marcin Bobowski

Reputation: 1775

Looking into weird protocol-buffer message (decoding and encoding)

I'm trying to figure out, is there a way to create a .proto definition, which could create and decode a message that looks like this:

parent_field {
   carrier_field { (field id: 1)
       subfield_1: 10 (field id: 1)
       subfield_2: 20 (field id: 2)
   }
   carrier_field: "string" (field id: 1)
}

which means that under same field identifier I can get either sub message or a string.

I tried something like:

message MessageWrapper {
   message ParentField {
      message SubMessage {
          ....
      }
      repeated SubMessage carrier_field = 1
   }
   ParentField parent_field = 1
}

but when I try to decode the message I get:

# protoc --experimental_allow_proto3_optional  --decode MessageWrapper proto_definitions/test.proto < test.buff 
Failed to parse input.

How should the .proto definition look like to be able to decode and encode a message showed above?

Upvotes: 1

Views: 336

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063338

The only way of doing that would be for carrier_field to be a bytes field that you would then separately decode as either a string or a protobuf message. Frankly, I'd advise against this: it would be better to use a oneof with different field numbers.

Upvotes: 1

Related Questions