Read value of enum extension in protocol buffer

百般思念 提交于 2019-12-04 11:20:37

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.

For example:

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);
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!