如何从 google proto buf 消息中的属性名称查找消息类型?
How to find Message Type from attribute name in google proto buf message?
我有如下定义的 protobuf 消息。我需要从属性名称中找到消息类型。例如,当输入为 "cfgMsg" 时,输出应为 ConfigMsg 或 CfgServerMsg.ConfigMsg(全名)。
message CfgServerMsg {
string name = 1;
ConfigMsg cfgMsg = 2;
}
message ConfigMsg {
string cfgName = 1;
uint32 msgId = 2;
}
我有以下代码。但是,这适用于定义明确的类型,如字符串、整数、浮点数等,对于消息,它只打印 "message" 作为输出。
我删除了一些代码,只提供了与这个问题相关的代码。所以这显然不是完整的代码。
google::protobuf::Message *modObj = new ModObj();
const google::protobuf::Descriptor *outModDesc
= modObj->GetDescriptor();
const Reflection *outModRefl = modObj->GetReflection();
const FieldDescriptor *field;
// Loop to iterate over all the fields
{
field = outModDesc->FindFieldByName(tmp_name);
std::string type = field->type_name();
std::cout << "Type:" << type << std::endl;
}
输出:
Type:string
Type:message
但是,我想获取 "ConfigMsg" 而不是 "message" 的实际消息类型。 protobuf 有这样的 API 吗?
我仔细检查了此页面 https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.descriptor#FileDescriptor.name.details,但找不到任何有用的信息。
如果有人做过类似的事情或知道这方面的一些事情,那将会很有用。
谢谢,
我从另一个小组得到了一些线索,我可以用 C++ 编写代码来获取实际的消息类型。在下面发布详细信息以帮助其他人。
google::protobuf::Message *modObj = new ModObj();
const google::protobuf::Descriptor *outModDesc
= modObj->GetDescriptor();
const Reflection *outModRefl = modObj->GetReflection();
const FieldDescriptor *field;
// Loop to iterate over all the fields
{
field = outModDesc->FindFieldByName(tmp_name);
std::string type = field->type_name();
std::cout << "Type:" << type << std::endl;
outField = outModDesc->FindFieldByName(tmp_name);
const google::protobuf::Descriptor* tmpDesc = outField->message_type();
std::string subMsgType = tmpDesc->name();
std::string fullMsgType = tmpDesc->full_name();
std::cout << " Type: " << subMsgType
<< ", Full Type: " << fullMsgType << std::endl;
}
代码输出:
Type: ConfigMsg, FullType: frrcfg.ConfigMsg
我有如下定义的 protobuf 消息。我需要从属性名称中找到消息类型。例如,当输入为 "cfgMsg" 时,输出应为 ConfigMsg 或 CfgServerMsg.ConfigMsg(全名)。
message CfgServerMsg {
string name = 1;
ConfigMsg cfgMsg = 2;
}
message ConfigMsg {
string cfgName = 1;
uint32 msgId = 2;
}
我有以下代码。但是,这适用于定义明确的类型,如字符串、整数、浮点数等,对于消息,它只打印 "message" 作为输出。
我删除了一些代码,只提供了与这个问题相关的代码。所以这显然不是完整的代码。
google::protobuf::Message *modObj = new ModObj();
const google::protobuf::Descriptor *outModDesc
= modObj->GetDescriptor();
const Reflection *outModRefl = modObj->GetReflection();
const FieldDescriptor *field;
// Loop to iterate over all the fields
{
field = outModDesc->FindFieldByName(tmp_name);
std::string type = field->type_name();
std::cout << "Type:" << type << std::endl;
}
输出: Type:string Type:message
但是,我想获取 "ConfigMsg" 而不是 "message" 的实际消息类型。 protobuf 有这样的 API 吗?
我仔细检查了此页面 https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.descriptor#FileDescriptor.name.details,但找不到任何有用的信息。
如果有人做过类似的事情或知道这方面的一些事情,那将会很有用。
谢谢,
我从另一个小组得到了一些线索,我可以用 C++ 编写代码来获取实际的消息类型。在下面发布详细信息以帮助其他人。
google::protobuf::Message *modObj = new ModObj();
const google::protobuf::Descriptor *outModDesc
= modObj->GetDescriptor();
const Reflection *outModRefl = modObj->GetReflection();
const FieldDescriptor *field;
// Loop to iterate over all the fields
{
field = outModDesc->FindFieldByName(tmp_name);
std::string type = field->type_name();
std::cout << "Type:" << type << std::endl;
outField = outModDesc->FindFieldByName(tmp_name);
const google::protobuf::Descriptor* tmpDesc = outField->message_type();
std::string subMsgType = tmpDesc->name();
std::string fullMsgType = tmpDesc->full_name();
std::cout << " Type: " << subMsgType
<< ", Full Type: " << fullMsgType << std::endl;
}
代码输出:
Type: ConfigMsg, FullType: frrcfg.ConfigMsg