Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key in map fields cannot be float/double, bytes or message types

We are planning to move our existing services to move grpc service. so need to convert the services to proto Defined Message Type. In the reponse, we have map with customize object as key.

eg response:

 //java
 Map<key_object, Project>

//proto
map<key_object_not_supported, Project> projects = 3;

In the official doc, they mentioned,

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

Is it any alternative ways to achieve customise object key map in the proto3 ?

like image 853
Paraneetharan Saravanaperumal Avatar asked Mar 12 '23 05:03

Paraneetharan Saravanaperumal


1 Answers

Support for map is a rather new extension. If your key does not match the constraints, you can use the old way instead:

Define a key-pair message and use that as a repeated field. So, in your example:

message KeyValuePair {
  key_object_not_supported key = 1;
  Project value = 2;
}

message MyMap {
  repeated KeyValuePair entries = 1;
}
like image 53
Philipp Claßen Avatar answered Apr 27 '23 17:04

Philipp Claßen