0

We have protobuf messages which share common attributes, for example

message Message1 {
    string foo = 1;
    string bar = 2;
    string baz = 3;
    string custom1 = 4;
}

message Message2 {
    string foo = 1;
    string bar = 2;
    string baz = 3;
    string custom2 = 4;
}

message Message3 {
    string foo = 1;
    string bar = 2;
    string baz = 3;
    string custom3 = 4;
}

message Message4 {
    string foo = 1;
    string bar = 2;
    string baz = 3;
    string custom4 = 4;
}

How can we have the common attributes defined in a separate proto file and be reused in all the messages.

message MessageCommonFields {
    string foo = 1;
    string bar = 2;
    string baz = 3;
}
maverickz
  • 143
  • 1
  • 7
  • 1
    Just include the common message as field in your other massages – Schottky Mar 13 '22 at 11:48
  • As in something like this? message Message1 { MessageCommonFields common_fields = 1; string custom1 = 4; } will have a JSON representation of { "common_fields": { "foo": "", "bar": "", "baz": ""}, "custom1": ""} and not { "foo": "", "bar": "", "baz": "", "custom1": ""} though – maverickz Mar 17 '22 at 04:13
  • https://stackoverflow.com/a/41243392/2312951 seems to suggest that this isn't possible – maverickz Mar 17 '22 at 04:21
  • Yea inheritance is not possible that's why I suggested using composition. You'll have to live with nested JSONs then, the only other way is to copy your fields around. – Schottky Mar 17 '22 at 08:42

0 Answers0