Radhika.S
Radhika.S

Reputation: 395

Create variable of type Map[string]interface{} in gRPC protoc buffer golang

I'm using grpc golang to communicate between client and server application. Below is the code for protoc buffer.

syntax = "proto3";
package Trail;

service TrailFunc {
  rpc HelloWorld (Request) returns (Reply) {}
}

// The request message containing the user's name.
message Request {
  map<string,string> inputVar = 1;
}
// The response message containing the greetings
message Reply {
  string outputVar = 1;
}

I need to create a field inputVar of type map[string]interface{} inside message data structure instead of map[string]string. How can I achieve it? Thanks in advance.

Upvotes: 22

Views: 39452

Answers (3)

F. Norbert
F. Norbert

Reputation: 871

I wrote a longer post about how to use google.protobuf.Struct to work with arbitrary JSON input. The structpb package capable to produce a map[string]interface{} from a structpb.Struct via its AsMap() function.

Official documentation: https://pkg.go.dev/google.golang.org/protobuf/types/known/structpb

Upvotes: 13

ptone
ptone

Reputation: 914

While it gets a little verbose to deal with, the "struct" type in protocol buffers is probably closer to golang's map[string]interface{}

But like interface{} will take some reflection-style overhead to determine what the actual stored type is.

for example see comment here: https://github.com/golang/protobuf/issues/370

Upvotes: 4

Marsel Novy
Marsel Novy

Reputation: 1817

proto3 has type Any

import "google/protobuf/any.proto";

message ErrorStatus {
  string message = 1;
  repeated google.protobuf.Any details = 2;
}

but if you look at its implementation, it is simply as

message Any {
  string type_url = 1;
  bytes value = 2;
}

You have to define such a message yourself by possibly using reflection and an intermediate type.

See example application

https://github.com/golang/protobuf/issues/60

Upvotes: 21

Related Questions