繁体   English   中英

获取golang的协议缓冲区选项信息

[英]get protocol buffer option information for golang

协议缓冲区定义如下, TestMessage具有两个选项msg_option_amsg_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM