Protocol-buffers – Protobuf3: How to describe the mapping of repeating strings?

Official documentation about map type says:

map 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 field, but it seems to be illegal in my libprotoc 3.0.0, complaining about expected “> “. So I want to know if there is any way to put duplicate strings into the map.

The possible solution may be:

message ListOfString {
repeated string value = 1;
}

// Then define:
map mapToRepeatedString = 1;

But ListOfString looks redundant here.

I have the same needs and got the same Error. I don’t believe this is possible. The following is the relevant BNF definition in 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< br />ident = letter {letter | decimalDigit | "_" }
field = ["repeated"] type fieldName "=" fieldNumber ["[" fieldOptions "]"] ";"

The “repeated” keyword only appears in the field definition. The map definition requires “type” and does not include repeated keywords.

This means there are some options.

>< br> You can follow the instructions to create a wrapper around the repeated value.

>The old method people defined defines the map, which is more cumbersome, but it is equivalent. This is the direction in the language guide Examples of backward compatibility.

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 need to convert the data to a map yourself , But in most languages ​​this should be quite simple. In Java:

 List map_field = // Existing list from protobuf.
Map > = 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 A collection of untyped lists from well-known types.

Official documentation about map type says:

< p>map 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 field, but it seems It is illegal for my libprotoc 3.0.0, complaining about the expected ">". So I want to know if there is any way to put the repeated string into the map.

The possible solution may be:< /p>

message ListOfString {
repeated string value = 1;
}

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

But ListOfString looks redundant here.

I have the same needs and got Same error. I don't believe this is possible. Here is the language Related BNF definitions in the specification.

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

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

The “repeated” keyword only appears in the field definition The map definition requires “type” and does not include repeated keywords.

This means there are some options.

>
You can follow the instructions to create one around the repeated value Wrapper.

>The old method people defined defines the map, which is more cumbersome, but equivalent. This is a backward compatible example in 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 need to convert the data to a map yourself, but in most languages ​​this should be fairly simple. In Java:

 List map_field = // Existing list from protobuf.
Map> = 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 a collection of untyped lists from well-known types.

Leave a Comment

Your email address will not be published.