读取协议缓冲区中枚举扩展的值

编程入门 行业动态 更新时间:2024-10-26 18:23:22
本文介绍了读取协议缓冲区中枚举扩展的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我刚刚询问此问题,并决定为枚举值写入扩展名在我的协议缓冲区。然而,我有一个非常困难的时间实际读取的值,即使有这个简单的.proto文件:

I just asked this question and have resolved to write an extension for enum values in my protocol buffer. However I am having an extremely difficult time actually reading the values back, even with this simple .proto file:

package test; import "google/protobuf/descriptor.proto"; extend google.protobuf.EnumValueOptions { optional string abbr = 54321; } message Person { required string name = 1; required int32 id = 2; optional string email = 3; enum PhoneType { MOBILE = 0 [(abbr)="Mobile ph"]; HOME = 1 [(abbr)="HomePhone"]; WORK = 2 [(abbr)="Work number"]; } message PhoneNumber { required string number = 1; optional PhoneType type = 2 [default = HOME]; } repeated PhoneNumber phone = 4; } message AddressBook { repeated Person person = 1; }

我一直在尝试这些和其他变体:

I've been trying these and other variants:

test::Person::PhoneNumber::descriptor()->options().GetExtension(test::abbr); test::Person::PhoneNumber::descriptor().GetExtension(test::abbr); test::Person::descriptor()->options().GetExtension(test::abbr); const google::protobuf::Descriptor* message = test::Person::PhoneNumber::descriptor(); const google::protobuf::Descriptor* desc = phone2.descriptor(); desc->options().GetExtension(test::abbr); //D.N.E. google::protobuf::MessageOptions opts = message->options(); opts.GetExtension(test::abbr); const google::protobuf::EnumDescriptor* enm = message->FindEnumTypeByName("PhoneNumber"); // null, not found google::protobuf::EnumValueOptions opts2 = enm->value(1)->options(); opts2.GetExtension(test::abbr); test::Person::PhoneNumber::descriptor()->options().GetExtension(test::abbr);

上述任何一项工作 - 方法不存在或者没有匹配的调用到该函数签名。我已经经历了几个小时的文档无济于事。我知道这应该是可能的,但唯一的例子是写.proto文件,而不是从他们回来 - 我该怎么做?一个简短的例子将被极大地赞赏。先感谢。

None of the above work - either the method does not exist at all, or there is no matching call to that function signature. I've been going through the documentation for hours to no avail. I know it should be possible, but the only examples are of writing the .proto files, not reading back from them -- How do I do this? A short example would be immensely appreciated. Thanks in advance.

推荐答案

这是一个有点复杂,但你需要获得 EnumValueDescriptor 电话类型,然后在此上调用 options()。GetExtension(test :: abbr)。

It's a wee bit convoluted, but you need to get the EnumValueDescriptor for the phone type, and then call options().GetExtension(test::abbr) on this.

test::Person person; person.set_name("Caol Ila"); person.set_id(1); test::Person::PhoneNumber *phone = person.add_phone(); phone->set_number("01496 840207"); phone->set_type(test::Person::MOBILE); phone = person.add_phone(); phone->set_number("01496 840207"); phone->set_type(test::Person::HOME); phone = person.add_phone(); phone->set_number("01496 840207"); phone->set_type(test::Person::WORK); phone = person.add_phone(); phone->set_number("01496 840207"); const google::protobuf::EnumDescriptor* enum_desc = test::Person::PhoneType_descriptor(); std::string value; for (int phone_index = 0; phone_index < person.phone_size(); ++phone_index) { if (person.phone(phone_index).has_type()) { int phone_type = person.phone(phone_index).type(); value = enum_desc->value(phone_type)->options().GetExtension(test::abbr); } }

更多推荐

读取协议缓冲区中枚举扩展的值

本文发布于:2023-07-27 02:47:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219907.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区中   协议

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!