2

As the protocol-buffers documentation describes:

Each map field generates a field in the struct of type map[TKey]TValue where TKey is the field's key type and TValue is the field's value type...

I would like to set TValue to be a slice of string. So that in Go it generates:

map[string][]string

Considering also this note on repeated fields:

Likewise, for the field definition repeated bytes foo = 1; the compiler will generate a Go struct with a [][]byte field named Foo

I'm trying to do the following:

message CallBackUrl {
    string base_url = 1;
    map<string, repeated string> params = 2;
}

But this just spits an error:

pb/authenticator.proto:57:26: Expected ">"

I could do this:

message StringSlice {
    repeated string slice = 1;
}

message CallBackUrl {
    map<string, StringSlice> params = 1;
}

Outputs:

type StringSlice struct {
    Slice []string
}

type CallBackUrl struct {
     Params               map[string]*StringSlice
}

However, in the implementation Params will need to be used as an url.Values to build a query string. This last solution would required an additional iteration, and copying the data into a new map[string][]string, which I would like to avoid.

(How) can I make protocol-buffers generate a (Go) map of string slices? I'm using the gRPC plugin.

Tim
  • 1,585
  • 1
  • 18
  • 23
  • Not possible as of now. Your second way of doing is the current hack for now. I too came across same issue when I used it for the first time and finally used the second approach. – Saurav Kumar Singh Dec 19 '19 at 13:33

0 Answers0