javaprotocol-buffersgrpcgrpc-javaprotobuf-java

Packing map<string, string> type into google.protobuf.Any


I'm struggling with packing a protobuf map type into google.protobuf.Any in Java. The proto message I'm working with has the following schema:

message Msg {
  google.protobuf.Any value = 1;
}

For example, to pack a StringValue into Any, I can simply construct a StringValue and use Any.pack:

Msg.newBuilder().setValue(Any.pack(StringValue.of("value"))).build()

I can do the same with all the other protobuf types since their respective message representations are available in the library, such as Int32Value, DoubleValue, ListValue, and so on.

However, I'd like to pack a protobuf map type into google.protobuf.Any, but the challenge here is that protobuf-java library does not provide Message representation for maps. Do you have any suggestions or ideas on how to do that?


Solution

  • Map has the parameters <string, string>, so there can't be a pre-made wrapper.

    But you can make a wrapper yourself, just like the others in wrappers.proto you are familiar with:

    message MapStringStringValue {
      map<string, string> value = 1;
    }