Protobuf3: How to describe map of repeated string?

本小妞迷上赌 提交于 2019-12-03 19:13:08

问题


The Official documentation about map type says:

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.

I want to define a map<string, repeated string> field, but it seems illegal on my libprotoc 3.0.0, which complains Expected ">". So I wonder if there is any way to put repeated string into map.

A Possible workaround could be:

message ListOfString {
    repeated string value = 1;
}

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

But ListOfString here looks redundant.


回答1:


I had the same need, and got the same error. I do not believe this is possible. Here is the relevant BNF definitions from the language specification.

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 "]" ] ";"

"repeated" keyword only appears in the field definition. The map definition requires a "type", which does not include the repeated keyword.

That means there are a few options.

  • You could create a wrapper around the repeated value as you indicated.
  • There is the older way people defined define maps, which is more burdensome but is equivalent. This is the backwards compatible example from the language guide. 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;
        
    You would need to convert the data to a map yourself, but this should be fairly trivial in most languages. In 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));
    
        
  • Use google.protobuf.ListValue https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#listvalue This is an untyped list collection from their well known types.



回答2:


I think it should be as follows.

message ListOfString {
   repeated string what_ever_name = 1;
}

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

Remember what_ever_name should be same in both places.



来源:https://stackoverflow.com/questions/38886789/protobuf3-how-to-describe-map-of-repeated-string

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