Enums are not allowed to be used as keys in map. PaxType here is an enum and not allowed to be used as key.
enum PaxType {
ADULT = 0 ;
CHILD = 1 ;
INFANT = 2 ;
}
message FlightData {
map<PaxType, FareType> fareType = 1;
}
Enums are not allowed to be used as keys in map. PaxType here is an enum and not allowed to be used as key.
enum PaxType {
ADULT = 0 ;
CHILD = 1 ;
INFANT = 2 ;
}
message FlightData {
map<PaxType, FareType> fareType = 1;
}
This is disallowed because it doesn't play well with proto3 open enum semantics. For example, in Java, if you have a Map, the key can only be one of the defined values. If you happen to receive an enum key value from a remote client/server that's not in the defined value set, it can't be put in the Map. This limitation forces us to either drop map entries with unknown enum keys (which is against proto3 open enum semantics), or disallow enum as map keys all together.
for reference: https://groups.google.com/forum/#!topic/protobuf/ikeldBe60eI
Define your own map entry type such as:
enum MyEnum {
FOO = 0;
BAR = 1;
BAZ = 2;
}
message MapEntry {
MyEnum type = 1;
int32 count = 2;
}
message MyMessage {
repeated MapEntry counts = 1;
}
You won't get the exact semantics or behavior of a map
, such as duplicate entries with the same keys being reduced to the last-received value, but you'll get the same compatibility benefits and the same general utility.
As Vivek Sinha already said is it not allowed to use Enums as keys in maps. However you can use a list and populate it in the same order you define your enum. For example when you program a game that uses bioms and you want to have a mapping from a biom type
to the number of existing bioms
in the world. This could be accomplished as follows:
enum BiomType {
PLACEHOLDER = 0;
FOREST = 1;
DESERT = 2;
}
message Map {
repeated int32 biom_distribution = 1;
}
When you populate the biom_distribution
list you have to keep the order of elements in the enum (BiomType
in this case).