I have protobuf messages defined as below. I need to find the message type from the attribute name. For example, when the input is "cfgMsg" the output should be ConfigMsg or CfgServerMsg.ConfigMsg (full name).
message CfgServerMsg {
string name = 1;
ConfigMsg cfgMsg = 2;
}
message ConfigMsg {
string cfgName = 1;
uint32 msgId = 2;
}I have the below code. However, this is working for well defined types like string, int, float etc. and for messages it just prints "message" as the output.
I removed some code and presented only that is relevant to this question. So this is obviously not the complete code.
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;
}Output:
Type:string
Type:message
However, I want to get the actual message type which is "ConfigMsg" instead of just "message". Is there any such API available from protobuf ?
I did check this page https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.descriptor#FileDescriptor.name.details thoroughly, but couldn't find any thing useful for this.
If anyone has done similar thing or know some thing around this, it would be useful.
Thanks,
You can get the ->message_type() (assuming the ->type() is
TYPE_MESSAGE), which gets you a Descriptor which in turn has a
->name() / ->full_name() function.
Cheers,
-ilia
> To unsubscribe from this group and stop receiving emails from it, send an email to prot...@googlegroups.com.