Protocol Buffer definition as following, the TestMessage
has two options msg_option_a
and 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;
}
I'd like to read the definition value of the two options:
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
It can get all options info when print the whole MessageOptions
, GetUninterpretedOption()
return a array of option definition, but it has a zero length.
The following is the comment of type UninterpretedOption
, but I cant get what it means, and haven't found any info about 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.
I want to get a specific option value, but no ideas now.
Please help! Thanks!
use proto.GetExtension
get option value:
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