Copy nested repeated messages

949 views
Skip to first unread message

ksamdev

unread,
Jun 7, 2011, 5:55:51 PM6/7/11
to prot...@googlegroups.com
Hi,

I've got nested messages like:

message A
{
   required double value = 1;
}

message B
{
  required A a = 1;
}

message C
{
  repeated B entries = 1;
}

The C object is saved in file and may have any number of B entries. However, now I'd like to save a copy of C in a different file with B's that match some criteria, e.g.

...
C original;
// Read object from file

C *copy = new C();
typedef ::google::protobuf::RepeatedPtrField<B> Bs;

for(Bs::const_iterator b = c->entries().begin();
c->entries().end() != b;
++b)
{
if (b->a().value() < 5)
continue;

// Copy B entry and add it to the Filtered C
B *entry = copy->add_entries();
*entry = *b;
}
// save copy

Sometimes it happens that I get the error message upon reading filtered C:

libprotobuf ERROR google/protobuf/message_lite.cc:123] Can't parse message of type "C" because it is missing required fields: entries[0].A

It seems that deep copy of nested repeated messages failed for some reason. Any ideas how to fix this?

ksamdev

unread,
Jun 7, 2011, 5:56:38 PM6/7/11
to prot...@googlegroups.com
Oh, I forgot to add some info on the Protobuf. I use v2.3.0 with C++.

Pherl Liu

unread,
Jun 21, 2011, 3:44:28 AM6/21/11
to prot...@googlegroups.com
This shouldn't happen.. That means your unfiltered C also contains uninitialized messages.. Did you add new required fields in your .proto file (or change optional to required)?

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/protobuf/-/WmZZUDM2ZDR0ZmNK.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.

Reply all
Reply to author
Forward
0 new messages