当前位置: > > > > 获取 golang 的协议缓冲区选项信息
来源:stackoverflow
2024-04-21 17:24:34
0浏览
收藏
今天将给大家带来《获取 golang 的协议缓冲区选项信息》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到等等知识点,如果你是正在学习Golang或者已经是大佬级别了,都非常欢迎也希望大家都能给我建议评论哈~希望能帮助到大家!
问题内容
protocol buffer定义如下,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
类型的注释,但我无法理解它的含义,也没有找到有关 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
文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《获取 golang 的协议缓冲区选项信息》文章吧,也可关注公众号了解相关技术文章。