how to replace proto2 extension with proto3 any when extend different number of field?

1.9k Views Asked by At

I'm trying to learn proto3, and have some questions with any.

I use extension quite much, if my proto is like this:

message base {
    extensions 1 to 100;
}

// a.proto
extend base {
   optional int32 a = 1;
   optional int32 b = 2;
}

// b.proto
extend base {
   optional string c = 1;
   optional string d = 2;
   optional string e = 3;
   optional string f = 4;
}

then how to replace these extensions with any ? should i must write like

import google/protobuf/any.proto
message base {
    any a = 1;
    any b = 2;
    any c = 3;
    any d = 4;
}

?

may so many proto has extended base.proto and I cannot make sure the max extension number of these protos. then how can I replace these extensions with any?

If I have to write any from 1 to 100 in message base ... oh, that will be too terrible !

1

There are 1 best solutions below

1
On BEST ANSWER

You would typically structure it like this:

message base {
    any submsg = 1;
}

// a.proto
message submsg_a {
   optional int32 a = 1;
   optional int32 b = 2;
}

// b.proto
message submsg_b {
   optional string c = 1;
   optional string d = 2;
   optional string e = 3;
   optional string f = 4;
}

And then put either submsg_a or submsg_b inside the any field.