Nitin Savant
Nitin Savant

Reputation: 961

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?

Upvotes: 6

Views: 5205

Answers (2)

wzq.615
wzq.615

Reputation: 3

I came into a similar issue, that I'd like a Message corresponding to List<Map<String,Object>> in Java.

And I solved my problem by defining Message like this:

import "google/protobuf/struct.proto";

message MyMessage {
    repeated google.protobuf.Struct mapList = 1;
}

Hope it'll help.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062865

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

Upvotes: 8

Related Questions