Everything looks good now, except default values. I love the fact that if the 
default value is explicitly stored in the message it override the base value, 
and it doesn't if it is not. Am I right?

1. The open question is how to make the message to store when it is serialized 
the explicitly stored default values.

Great project.


From: Kenton Varda []
Sent: Friday, August 14, 2009 5:33 PM
To: George Georgiev
Cc: Protocol Buffers
Subject: Re: combine protobuf messages

If you can just write code specific to your message type which deals with 
merging items with the same ID, I'd suggest doing that.  If you really need to 
write a totally generic algorithm, you can use reflection as documented here:

On Fri, Aug 14, 2009 at 4:46 PM, George Georgiev 
<<>> wrote:

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 [<>]
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 
<<>> 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
To unsubscribe from this group, send email to
For more options, visit this group at

Reply via email to