protocol-buffersprotocol-buffers-3

Protobuf3: How to describe map of repeated string?


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.


Solution

  • 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.