I need to get some data using gRPC from a server which have different types but are semantically related. You can think of it as a data which can have types A, B, and C. I was thinking about what is the proper method of transferring this data to client. I personally can think of three different methods:
- Using single message with
oneof:
In this method, I just define a single message as the following:
message MyData {
oneof test_oneof {
DataA a = 1;
DataB b = 2;
DataC c = 3;
}
}
Now I just add a single rpc method which can get different types of data. The problem with this method is extensibility. I will not be able to update my message by adding new message types DataD to this oneof as stated here (or at least that is what I understood, though I think oneof will be somehow useless with this limitation).
- Using single message with
Any:
In this method, I just define a single message as the following:
import "google/protobuf/any.proto";
message MyData {
google.protobuf.Any data = 1;
}
Here, I need a single rpc method. This method is also extensible, but the drawback is that Any types are still under development. In addition, code will be more complex because of required reflection codes.
- Using multiple rpc calls:
In this method, I just define a single rpc call for each data type. This method is extensible too, but it creates a huge amount of similar code and I personally don't like it at all. I personally think since these data are semantically related, they need to be transferred using single rpc call.
What do you think as the best method for transferring this data using gRPC and protobuf?