当前位置 : 主页 > 大数据 > 区块链 >

protocol-buffers – Protobuf3:如何描述重复字符串的映射?

来源:互联网 收集:自由互联 发布时间:2021-06-22
Official documentation about map type说: mapkey_type, value_type map_field = N; …where the key_type can be any integral or string type (so, any scalar type except for floating point types and bytes). The value_type can be any type . 我想
Official documentation about map type说:

map<key_type, value_type> map_field = N;

…where the key_type can be any integral or string type (so, any
scalar type except for floating point types and bytes). The value_type
can be any type.

我想定义一个地图< string,重复字符串>字段,但它似乎是非法的我的libprotoc 3.0.0,抱怨预期“>”.所以我想知道是否有任何方法可以将重复的字符串放入地图中.

可能的解决方法可能是:

message ListOfString {
    repeated string value = 1;
}

// Then define:
map<string, ListOfString> mapToRepeatedString = 1;

但ListOfString在这里看起来多余.

我有同样的需求,并得到了同样的错误.我不相信这是可能的.以下是语言规范中的相关BNF定义.

https://developers.google.com/protocol-buffers/docs/reference/proto3-spec

messageType = [ "." ] { ident "." } messageName
mapField = "map" "<" keyType "," type ">" mapName "=" fieldNumber [ "["fieldOptions "]" ] ";"
type = "double" | "float" | "int32" | "int64" | "uint32" | "uint64"
  | "sint32" | "sint64" | "fixed32" | "fixed64" | "sfixed32" | "sfixed64"
  | "bool" | "string" | "bytes" | messageType | enumType
messageName = ident
ident = letter { letter | decimalDigit | "_" }
field = [ "repeated" ] type fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";"

“重复”关键字仅出现在字段定义中.地图定义需要“类型”,不包括重复的关键字.

这意味着有一些选择.

>
 您可以按照指示在重复值周围创建一个包装器.

>人们定义的旧方法定义了地图,这更加繁琐,但却是等价的.这是语言指南中的向后兼容示例.

https://developers.google.com/protocol-buffers/docs/proto3#maps

    

    message MapFieldEntry {
      key_type key = 1;
      repeated value_type value = 2;
    }
    repeated MapFieldEntry map_field = N;
    

您需要自己将数据转换为地图,但在大多数语言中这应该是相当简单的.在Java中:

    

    List<MapFieldEntry> map_field = // Existing list from protobuf.
    Map<key_type, List<value_type>> = map_field.stream()
        .collect(Collectors.toMap(kv -> kv.key, kv -> kv.value));

    

>
使用google.protobuf.ListValue
https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#listvalue

这是一个来自众所周知类型的无类型列表集合.

网友评论