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

I am using grpc golang for communication between client and server application. Below is the code for the protoc buffer.

syntax = "proto3";
package Trail;

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

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

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

+4
source share
3 answers

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's just like

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

You must define such a message yourself, possibly using reflection and an intermediate type.

See sample application

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

+2
source

, "struct" , , golang map [string] {}

, {}, , , .

, . : https://github.com/golang/protobuf/issues/370

0

You can do

    message Request {
          repeated string inputVar = 1;
    }

if you want to define the string []

0
source

Source: https://habr.com/ru/post/1658915/


All Articles