This is my proto file:
import "google/protobuf/struct.proto";
service Renderer {
rpc Render(Request) returns (Response) {}
}
message Request {
string template = 1;
string locale = 2;
google.protobuf.Struct variables = 3;
}
In the following function:
func (s *Server) Render(
ctx context.Context,
message *Request,
) (*Response, error) {
fmt.Println(message.Variables.AsMap()) // is always empty
return &Response{}, nil
}
the variables field is always empty even when I send this to the server:
{
"template": "template_name",
"locale": "pt",
"variables": {
"foo": "bar"
}
}
I'm expecting variables to contain foo as a key and bar as the value. What I'm doing wrong? What can I do to achieve this in case I'm doing it wrong?
Have a look at
structpb. It contains a decent example.You can't use the JSON example below because it is a
map[string]stringand this is insufficiently general-purpose to represent dynamic types.If you use
structpbto generate your{"foo":"bar"}value:You'll see that
vis represented in Go as:This is what is expected by
Server.Renderasmessage.Variablesin your code.