Protocol Buffers 3: Enums as keys in a map

夙愿已清 提交于 2020-01-24 02:43:05

问题


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;
}

回答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 entires 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



来源:https://stackoverflow.com/questions/41138001/protocol-buffers-3-enums-as-keys-in-a-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!