Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a list of maps in Protocol Buffers?

I'm looking to create a gRPC response that returns a list of maps. Here is what I imagined the structure being:

message GetSettingsResponse {
    repeated map<string, string> settings = 1;
}

Repeating maps is not supported, however, and I had to nest the map in a separate message to make it work:

message GetSettingsResponse {
    repeated Setting settings = 1;
}

message Setting {
    map<string, string> setting = 1;
}

This works, but it forces us to write some confusing code on both the client and server. Is there any way to avoid this solution and get closer to my desired structure?

like image 309
Nitin Savant Avatar asked Aug 07 '18 17:08

Nitin Savant


People also ask

Does Protobuf support map?

Map is one of the composite datatypes of Protobuf.

Is Protobuf map ordered?

In general protobuf may serialize fields in a random order.

What is the difference between proto2 and proto3?

Proto3 is the latest version of Protocol Buffers and includes the following changes from proto2: Field presence, also known as hasField , is removed by default for primitive fields. An unset primitive field has a language-defined default value.

Should I use proto2 and proto3?

Proto2: supports optional natively, the wrapper types were recommended but should be avoided in new applications. Proto3: originally did not support presence tracking for primitive fields. As of 2020, proto3 supports both optional fields which have has_foo() methods and "singular" fields, which do not.


1 Answers

No, basically. What you have is the closest you can do in protobuf.

like image 189
Marc Gravell Avatar answered Sep 25 '22 11:09

Marc Gravell