2016-10-26 4 views
4

私はgrpc golangを使ってクライアントアプリケーションとサーバーアプリケーションの間で通信しています。 以下はprotocバッファのコードです。gRPC protocバッファーgolangのmap [string]インターフェース{}の変数を作成

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; 
} 

map [string]文字列ではなく、メッセージデータ構造内にmap [string] interface {}型のフィールドinputVarを作成する必要があります。 どうすれば実現できますか? ありがとうございます。

+0

を、。しかし、私は 'map 'がうまくいくかもしれないと思いますか? – Vatine

答えて

2

proto3はAny

import "google/protobuf/any.proto"; 

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

を入力していいますが、その実装を見れば、それはあなたがおそらく反射し、中間タイプを使用することにより、このようなメッセージを自分で定義する必要が

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

として単純です。

それは、プロトコルバッファに対処するために少し冗長、「構造体」タイプを取得している間example application

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

0

を参照してください、おそらくgolangのマップに近い[文字列]インターフェース{}

しかし、 interface {}のように実際の格納されている型が何であるかを判断するためにリフレクションスタイルのオーバーヘッドが必要になります。

例えばここのコメントを参照:「あなたはしたくない」のように聞こえるオフ手https://github.com/golang/protobuf/issues/370

関連する問題