Is there a way to have an array of multiple types in proto3?

2.5k Views Asked by At

I want to design a proto3 message from this Cesium class:Cesium Class. It is an array containing [string, double, double, double]. Is there anyway to do this?

2

There are 2 best solutions below

4
On

I don't believe you can create arbitrarily long messages like those shown in your link because your client and server must agree on the number and order of fields before transfer.

[Time, Longitude, Latitude, Height, Time, Longitude, Latitude, Height, ...]

Problem: without pre-defining many field numbers, how do I pass this many fields?

Option 1

Instead, you can create your own message type to represent a single instance:

message CartographicRadians {
    string time = 1;
    double longitude = 2;
    double latitude = 3;
    double height = 4;
}

Then use the custom type as a field. Here the repeated keyword indicates that you can send more than one CartographicRadians in the CartographicRadiansArray.

message CartographicRadiansArray {
    repeated CartographicRadians entries = 1;
}

Option 2

Another approach would be to create a self-recursive message which would function closer to the stream-like format described above.

message CartographicRadians {
    string time = 1;
    double longitude = 2;
    double latitude = 3;
    double height = 4;
    CartographicRadians nextEntry = 5;
}
0
On

The best option to serialize/deserialize an array with different types in protobuf is this one:

message Types{
  oneof types {
    string str= 1;
    double dou = 2;
  }
}

message CartographicRadians {
  repeated Types list = 1;