0

I want to create a protobuf which returns a map containing custom keys. When I try to make one, I get an error: Key in map fields cannot be float/double, bytes or message types. I want to know if there is a workaround for it.

I am trying to create a map of following type (as an illustration):

message Name {
  string first_name = 1;
  string last_name = 2;
}

message GetNameResp {
  map<Name, string> name_info = 1;
}

So, can the Name here be a possible key to the map? The only solution I could find was to serialize the map and then return as repeated values.

Thanks!

Coder
  • 1,415
  • 2
  • 23
  • 49

1 Answers1

0

Map keys may not be message types:

https://developers.google.com/protocol-buffers/docs/proto3#maps

You would have to work around this in code.

You could convert Name to string:

  • serialize the protobuf message
  • marshaling to JSON
  • hashing the value (though you'd potentially have collisions)
DazWilkin
  • 32,823
  • 5
  • 47
  • 88