0
votes

I need to include an instance of the following class as a field in a protocol buffer schema:

public final class Metrics extends HashMap<Object, Double> {
}

but when I add something like:

map<bytes, double> metrics = 2;

I effectively get the message Map key cannot be float, double, bytes, message, or enum types.

One approach I've thought of was to turn the objects (keys) into bytes and then to base64 keys to create a new map, which would be the actual map to be sent to the wire (then reverse these operations when it got to the other side). But this seems too expensive and I feel like I would have to create an instance of the input streams (and close them) for each entry.

Is there a known approach for this case? How would you do it? I did not find any example of this.

Thanks

1
They can only be Object types. So you can use the wrapper classes for the primitives (e.g. Integer, Float, etc). - WJS
What kind of objects are the keys? Surely they aren't arbitrary objects? - Joni
This is a module created to integrate on several applications. The current one I'm integrating will actually be using an enum as the keys, so maybe the answer lies there - João Dias

1 Answers

1
votes

You can emulate map with a list of key-value pairs (this is how it was done before introduction of maps in proto3 and backporting them to proto2):

message Metric {
  bytes key = 1;
  double value = 2;
}

repeated Metric metrics = 2;