[英]get protocol buffer option information for golang
協議緩沖區定義如下, TestMessage
具有兩個選項msg_option_a
和msg_option_b
:
syntax = "proto3";
package grpctest;
option go_package = "pb";
import "google/protobuf/descriptor.proto";
extend google.protobuf.MessageOptions {
int32 msg_option_a = 50011;
int32 msg_option_b = 50012;
}
message TestMessage {
option (msg_option_a) = 22;
option (msg_option_b) = 33;
string name = 1;
}
我想閱讀兩個選項的定義值:
var msg *pb.TestMessage
_, md := descriptor.ForMessage(msg)
options := md.GetOptions()
fmt.Println(options.String()) // --> [grpcapi.msg_option_a]:22 [grpcapi.msg_option_b]:33
fmt.Println(len(options.GetUninterpretedOption())) // --> 0
當打印整個MessageOptions
,它可以獲取所有選項信息, GetUninterpretedOption()
返回選項定義的數組,但長度為零。
以下是UninterpretedOption
類型的UninterpretedOption
,但我無法理解其含義,並且未找到有關DescriptorPool
任何信息:
// A message representing a option the parser does not recognize. This only
// appears in options protos created by the compiler::Parser class.
// DescriptorPool resolves these when building Descriptor objects. Therefore,
// options protos in descriptor objects (e.g. returned by Descriptor::options(),
// or produced by Descriptor::CopyTo()) will never have UninterpretedOptions
// in them.
我想獲得一個特定的期權價值 ,但現在還沒有想法。
請幫忙! 謝謝!
使用proto.GetExtension
獲取選項值:
var msg *pb.TestMessage
_, md := descriptor.ForMessage(msg)
options := md.GetOptions()
fmt.Println(options.String()) // --> [grpcapi.msg_option_a]:22 [grpcapi.msg_option_b]:33
fmt.Println(len(options.GetUninterpretedOption())) // --> 0
a, _ := proto.GetExtension(options, pb.E_MsgOptionA)
fmt.Println(*a.(*int32)) // --> 22
b, _ := proto.GetExtension(options, pb.E_MsgOptionB)
fmt.Println(*b.(*int32)) // --> 33
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.