1

I am writing a gRPC service that fetches exchange rates within a given date range from this API and the response is partitioned day by day, you can see response format here.

To parse this response, I need to have a proto message with a field something like

map<string, map<string, float>>

However proto does not allow defining nested maps. There are some solutions to create another message with a map field, however in my case, the inner map is generic. So, I could not define another message.

Is there a way of defining generic nested maps in proto3?

thisguy
  • 39
  • 6

1 Answers1

1

This definition should work:

syntax = "proto3";

package open.exchange.v1;

option go_package = "github.com/my_open_exchange";

// Rates define a generic Rates for a currency
// The key of the map is the related currency, the value
message Rates {
  map<string, float> Rate = 1;

}

// TimeSeriesRates define a Rate of a currency for a specific date
// The key is the date, the value the related map of rates
message TimeSeriesRates {
  map<string, Rates> Rate = 1;
}
// TimeSeriesResponse is the response of the TimeSeries endpoint
message TimeSeriesResponse {
  string start_date = 1;
  string end_date = 2;
  string base = 3;
  TimeSeriesRates rates = 4;
}


Matteo
  • 37,680
  • 11
  • 100
  • 115