Protobuf3: How to describe map of repeated string?

2019-02-10 17:25发布

问题:

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.