MergeFrom sounds good.

The only issue that I still will have is with the repeated fields. For some of 
them I will have an Id attribute. So what I would like to achieve is when I 
merge the messages instead of adding new partial message in the list it to 
combine those of the repeated messages that have the same Id.

>> custom algorithm based on reflection
I suppose this is what I need in this case. Could you please give me a bit more 
information what you mean.


From: Kenton Varda [mailto:ken...@google.com]
Sent: Friday, August 14, 2009 2:01 PM
To: George Georgiev
Cc: Protocol Buffers
Subject: Re: combine protobuf messages

On Fri, Aug 14, 2009 at 1:18 PM, George Georgiev 
<georgi.georg...@citrix.com<mailto:georgi.georg...@citrix.com>> wrote:
1. How to serialize parts from the message without validation

Use the "Partial" serialization and parsing methods, e.g. 
SerializePartialToString() and ParsePartialFromString().  These do not check 
required fields.

2. How to combine two parts. Or how to parse second part which may overlap some 
of the already existing parts

Use MergeFrom():

  MyMessage combined;
  // Iterate over messages from lowest to highest priority.
  for (int priority = 0; priority < max_priority; ++priority) {

  // Check
  if (!combined.IsInitialized()) {
    cerr << "Missing required fields: "
         << combined.InitializationErrorString() << endl;
    return false;

As documented, the semantics of MergeFrom() are:  "Singular fields will be 
overwritten, except for embedded messages which will be merged. Repeated fields 
will be concatenated."

So, if you merge the highest-priority message last, then its values will end up 
taking precedence.

If MergeFrom() isn't quite what you want, then you'll have to write a custom 
algorithm based on reflection.

You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to protobuf@googlegroups.com
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to