Protobuf3: How to describe map of repeated string?

后端 未结 2 1125
北恋
北恋 2020-12-03 03:10

The Official documentation about map type says:

map map_field = N;

...where the key_type can be a

相关标签:
2条回答
  • 2020-12-03 03:49

    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.

    0 讨论(0)
  • 2020-12-03 03:56

    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.
    0 讨论(0)
提交回复
热议问题