2017-12-25 3 views
2

protoファイルで汎用メッセージを定義する方法。.protoファイルのメッセージタイプとしてcom.google.protobuf.Messageを定義する方法

例:

message GenericResponse 
{ 
    bool status = 1; 
    Foo foo= 2; 
    Bar bar = 3; 
    Baz baz = 4; 
} 

の代わりに上記の私は、以下のプロトコルを必要とするプロトコルを述べました。

message GenericResponse 
{ 
    bool status = 1; 
    google.protobuf.Message response = 2; 
} 

私はresponseFooまたはBarまたはBaz値を設定する必要があります。 これを達成する方法はありますか?

答えて

2

私はoneofが最善の策であることを示唆している:

message GenericResponse 
{ 
    bool status = 1; 
    oneof response { 
     Foo foo= 2; 
     Bar bar = 3; 
     Baz baz = 4; 
    } 
} 

ます。またAnyを使用することができますが、IMOそれは間違いであるとあなたのために物事が難しくなるだろう。

+0

しかし、私はこのような結果を得るでしょう: "status:true bar {id:6}"。 私が期待しているのは "status:true response {id:6}"です。 – Prasath

+0

@prasathはい、そうでなければ、あなたはその表現を知らないでしょう。シリアライザーは知りたいと思っています。 –

+0

javaのオブジェクトと同じprotoファイルにMessageを定義する必要があります。可能でないなら、私は "oneof"と一緒に行きます。 – Prasath

関連する問題