Reading the enum extension value in the protocol buffer

I simply asked this question and decided to write an extension for the enumeration values โ€‹โ€‹in my protocol buffer. However, I have a very difficult time when we really read 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 tried these and other options:

 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); //DNE 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); 

None of the above works - either the method does not exist at all, or there is no corresponding call to this function. I worked through the documentation for several hours to no avail. I know this should be possible, but the only examples are writing .proto files, not reading from them - How do I do this? A short example will be greatly appreciated. Thanks in advance.

+4
source share
1 answer

This is a bit convoluted, but you need to get an EnumValueDescriptor for the phone type, and then call options().GetExtension(test::abbr) on this.

For instance:

 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); } } 
+4
source

Source: https://habr.com/ru/post/1244109/


All Articles