本文介绍了协议缓冲区3:将枚举作为映射中的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不允许将枚举用作映射中的键。

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


推荐答案

这是不允许的,因为它不会在proto3开放式枚举语义上不能很好地发挥作用。例如,在Java中,如果您有Map,则键只能是定义的值之一。如果您碰巧从远程客户端/服务器接收到的枚举键值不在定义的值集中,则无法将其放入地图中。这种限制迫使我们要么丢弃具有未知枚举键的映射条目(这与proto3开放式枚举语义相反),要么不允许枚举同时作为映射键。

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.

供参考:

这篇关于协议缓冲区3:将枚举作为映射中的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:04