Find the message Type from Name of the message

28 views
Skip to first unread message

Kiran Kumar

unread,
May 21, 2019, 7:13:52 PM5/21/19
to Protocol Buffers

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,

Ilia Mirkin

unread,
May 21, 2019, 7:31:50 PM5/21/19
to Kiran Kumar, Protocol Buffers
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
> --
> You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
> To post to this group, send email to prot...@googlegroups.com.
> Visit this group at https://groups.google.com/group/protobuf.
> To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/68cf3dd3-8b5e-4f5c-8b87-b708b4e787da%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Kiran Kumar

unread,
May 25, 2019, 10:10:21 PM5/25/19
to Protocol Buffers
Thanks. This worked for me. Full code is as below.

outField = outModDesc->FindFieldByName(tmp_name);
const google::protobuf::Descriptor* tmpDesc = outField->message_type();
std::string subMsgType = tmpDesc->name();
std::string subFullMsgType = tmpDesc->full_name();
std::cout << " Type:" << subMsgType
               << "new Type" << newMsgType << std::endl;
                        
Output:
========
Type:ConfigMsg 
Full Type:frrcfg.ConfigMsg

Thanks,
Kiran.

On Tuesday, May 21, 2019 at 4:31:50 PM UTC-7, Ilia Mirkin wrote:
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.
Reply all
Reply to author
Forward
0 new messages