Way to figure out the fields that are modified

7 views
Skip to first unread message

Ananth Arockia

unread,
Jun 21, 2010, 11:23:59 AM6/21/10
to Protocol Buffers
Hello,

I am exploring protobuf to use it in my project.

I would like to have a message defined in a .proto file. All the
fields in this message would be "optional" due to the fact that the
message producer in my project context would set just a subset of
fields. Can someone please clarify

1. Is there a way to figure out just the fields that are being set in
the message? I would require this after my message is deserialized.
2. does field_count() always return the total number of fields defined
in the message in .proto?
3. Is Reflection the only option to set fields directly/indirectly via
field numbers?

Thanks, Ananth

Jason Hsueh

unread,
Jun 21, 2010, 3:13:13 PM6/21/10
to Ananth Arockia, Protocol Buffers
On Mon, Jun 21, 2010 at 8:23 AM, Ananth Arockia <ananth....@gmail.com> wrote:
Hello,

I am exploring protobuf to use it in my project.

I would like to have a message defined in a .proto file. All the
fields in this message would be "optional" due to the fact that the
message producer in my project context would set just a subset of
fields. Can someone please clarify

1. Is there a way to figure out just the fields that are being set in
the message? I would require this after my message is deserialized.

You can check the has() bits (see the tutorial: http://code.google.com/apis/protocolbuffers/docs/tutorials.html) Alternatively, you can define your protocol such that if the field is set, it is not equal to the default value. Then instead of testing has_foo(), you could test foo() != 0 (or whatever the default is).
 
2. does field_count() always return the total number of fields defined
in the message in .proto?

Assuming you mean Descriptor::field_count(), yes.
 
3. Is Reflection the only option to set fields directly/indirectly via
field numbers?

You could also generate the serialized format by hand and then merge that into your message...but Reflection is friendlier :-)


Thanks, Ananth

--
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.


Reply all
Reply to author
Forward
0 new messages