37 #ifdef MESSAGE_SUPPORT 38 bool util::get_oneof_field_type(
const google::protobuf::Message& message,
const std::string& oneof_name, std::string& item_name,
39 bool include_namespace)
42 const google::protobuf::OneofDescriptor* oneof =
nullptr;
43 if (oneof_name.empty())
46 if (message.GetDescriptor()->oneof_decl_count() == 1)
48 oneof = message.GetDescriptor()->oneof_decl(0);
52 PRINT_ERROR_NAMED(
"Multiple oneofs found in the message but no oneof_name provided, I don't know which one to choose.");
58 oneof = message.GetDescriptor()->FindOneofByName(oneof_name);
62 const google::protobuf::FieldDescriptor* oneof_field = message.GetReflection()->GetOneofFieldDescriptor(message, oneof);
63 if (oneof_field && oneof_field->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE)
66 item_name = message.GetReflection()->GetMessage(message, oneof_field).GetTypeName();
67 if (!include_namespace)
69 std::size_t found = item_name.find_last_of(
".");
70 item_name = item_name.substr(found + 1);
75 if (item_name.empty())
PRINT_ERROR_NAMED(
"no item found for oneof-name " + oneof_name +
".");
79 bool util::get_oneof_field_type_expand_isolated(
const google::protobuf::Message& top_message,
const google::protobuf::OneofDescriptor* top_oneof,
80 std::string& item_name,
bool include_namespace,
int max_depth)
82 const google::protobuf::FieldDescriptor* top_oneof_field = top_message.GetReflection()->GetOneofFieldDescriptor(top_message, top_oneof);
83 if (top_oneof_field && top_oneof_field->cpp_type() != google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE)
return false;
85 const google::protobuf::Message& message = top_message.GetReflection()->GetMessage(top_message, top_oneof_field);
87 int num_oneofs = message.GetDescriptor()->oneof_decl_count();
89 if (num_oneofs == 1 && max_depth > 0)
91 get_oneof_field_type_expand_isolated(message, message.GetDescriptor()->oneof_decl(0), item_name, include_namespace, max_depth - 1);
96 item_name = message.GetTypeName();
97 if (!include_namespace)
99 std::size_t found = item_name.find_last_of(
".");
100 item_name = item_name.substr(found + 1);
106 bool util::get_oneof_field_type_expand_isolated(
const google::protobuf::Message& message,
const std::string& top_oneof_name, std::string& item_name,
107 bool include_namespace,
int max_depth)
109 const google::protobuf::OneofDescriptor* top_oneof =
nullptr;
110 if (top_oneof_name.empty())
113 if (message.GetDescriptor()->oneof_decl_count() == 1)
115 top_oneof = message.GetDescriptor()->oneof_decl(0);
119 PRINT_ERROR_NAMED(
"Multiple oneofs found in the message but no top_oneof_name provided, I don't know which one to choose.");
125 top_oneof = message.GetDescriptor()->FindOneofByName(top_oneof_name);
129 return get_oneof_field_type_expand_isolated(message, top_oneof, item_name, include_namespace, max_depth);
#define PRINT_ERROR_NAMED(msg)