1

I have a use case where the incoming payload follows a top level schema and a nested schema. There can be 4 types of top level schema and infinite types of nested schema. I want to define my top level schemas in such a way that the internal schema can correspond to any message type in a directory. Is that possible with protobuf? If yes, how? And how would the decoding happen in such case

E.g.

message topLevelMessage {
 string field1 = 1;
 string field2 = 2;
 string schemaType = 3;
 schemaType payload = 4;
}

I have tried researching and the best response I have gotten so far is to use map in protobuf which I'd rather not do


Edit: I'd also like to do something like below

message topLevelMessage {
 string field1 = 1;
 string field2 = 2;
 string schemaType = 3;
 repeated schemaType|messageType2|messageType3 payload = 4;
}

The motivation here is to make it easier to migrate our legacy service. Thereforce, there's less wiggle room

1 Answers1

1

I encourage you to read the Protocol Buffer documentation as most concepts are well-explained.

You can use Google's Well-known Types Any to embed (arbitrary|open-ended set of) protobuf messages.

But prototype this approach before you commit to it as it introduces complexity.

For union types (schemaType|messageType2|messageType3), you can use Oneof.

DazWilkin
  • 32,823
  • 5
  • 47
  • 88