What is the default value for a message used as a field type within another message?

4,530 views
Skip to first unread message

Jax

unread,
Jun 4, 2010, 12:32:20 PM6/4/10
to Protocol Buffers

This should be pretty straightforward, but I couldn't find it in any
of the documentation on default values.

A slight change from some example code in the google documentation:

message SearchResponse {
optional Result result = 1;
}

message Result {
required string url = 1;
optional string title = 2;
repeated string snippets = 3;
}

My question is, what is the default value for the Result in the above
code? Does it get set to null since (I presume) it will be compiled
into an object when the protocol buffer gets compiled into Java or C#?
Or do default values work differently for a nested message than for
base types?

Jason Hsueh

unread,
Jun 4, 2010, 12:45:05 PM6/4/10
to Jax, Protocol Buffers
No, protobuf accessors never return null. For message fields, you get the default instance of the message type.


--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
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.


Jax

unread,
Jun 4, 2010, 1:18:27 PM6/4/10
to Protocol Buffers

Thanks for the quick reply -- I noticed this in the documentation
after searching for default instance:

"For embedded messages, the default value is always the "default
instance" or "prototype" of the message, which has none of its fields
set." So, does this mean that even the required fields will be set to
their default values in this prototype object? Or is there some other
algorithm for the values of the fields within the prototype/default
instance?

For broader context, I'm trying to figure out how to determine if an
optional field was actually set in the transmitted protocol buffer or
whether its default value was filled in automatically.

In reference to the example below, if the title field had been set to
"Field of Dreams" and then the user set it to an empty string "", I
would want to actually update the value in my data store to be the
empty string. However, if the user didn't change the title field at
all, I will still see an empty string in the title field when I try
and get at the field through the accessor. I'm looking for a way to
setup my protocol buffer format so I can distinguish between those two
cases.

Thanks again.
> > protobuf+u...@googlegroups.com<protobuf%2Bunsubscribe@googlegroups.c om>
> > .

Kenton Varda

unread,
Jun 4, 2010, 1:27:18 PM6/4/10
to Jax, Protocol Buffers
Use the "has" accessors to determine whether the field was explicitly set.

Java:  message.hasFoo()
C++:  message.has_foo()
Python:  message.HasField("foo")  # (I think; check docs)

To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.

Jax

unread,
Jun 4, 2010, 1:34:33 PM6/4/10
to Protocol Buffers
Perfect, just what I was looking for. Thanks much.
> > <protobuf%2Bunsubscr...@googlegroups.c om>
Reply all
Reply to author
Forward
0 new messages