On Thu, May 7, 2015 at 2:29 AM, Jan Kyjovský <jan.kyjov...@tieto.com> wrote:

> Hi,
>
> sorry for asking for advice so soon again.
>
> I have progressed a bit futher and I am now trying to display decoded
> data. For that purpose I have prepared some data and tried to decode them
> but I have encountered problem with repeated structures and with nested
> messages. I dont know how to get count of repeats in case of repeated
> fields so its problematic to address them. In case of nested messages I
> have experiencing difficulties how to get message.
>
> Here is function I have made so far. Note that I am suspecting that it
> will probably need some more tweaking so it can be called recursively (work
> still in progress). For now I am satisfied just by displaying data on first
> level.
>
> void ExpandSubMessage(Message *message, Descriptor *descriptor)
> {
>     for (int i = 0; i < descriptor->field_count(); i++)
>     {
>         const FieldDescriptor* field = descriptor->field(i);
>         switch (field->type())
>         {
>             case FieldDescriptor::TYPE_INT32:
>             {
>                 int nVal = message->GetReflection()->GetInt32(*message,
> field);
>                 printf("%s = %d\n", field->name().c_str(), nVal);
>
>                 break;
>             }
>             case FieldDescriptor::TYPE_STRING:
>             {
>                 std::string strVal =
> message->GetReflection()->GetString(*message, field);
>                 printf("%s = %s\n", field->name().c_str(), strVal.c_str());
>
>                 break;
>             }
> //            case FieldDescriptor::TYPE_ENUM:
> message->GetReflection()->GetEnum(*message, field); break;
>             case FieldDescriptor::TYPE_MESSAGE:
>             {
>                 Message *messVal;
>                 if (field->is_repeated())
>                 {
>                     message->GetReflection()->GetRepeatedMessage(*message,
> field, 1);
>                 }
>                 else
>                 {
>                     message->GetReflection()->GetMessage(*messVal, field);
>                 }
>                 printf("%s = %s\n", field->name().c_str(),
> /*messVal.DebugString().c_str()*/"Test");
>
>                 break;
>             }
>         }
>     }
> }
>
> I have tried to look for details in specification on your pages but didn't
> find anything that would answer my question.
>
> May I once again ask for your assistance?
>
What you are trying to do looks similar to what the TextFormat class does.
You can refer to that class as an example to learn how to use protobuf
reflection to traverse a message tree:
https://github.com/google/protobuf/blob/master/src/google/protobuf/text_format.cc#L1449


>
>  --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to protobuf@googlegroups.com.
> Visit this group at http://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to