Oneof example

1,033 views
Skip to first unread message

John Rogers

unread,
May 3, 2017, 9:13:43 AM5/3/17
to nanopb
Hi,

I am planning on using a "oneof" message as my top level wrapper.

I have read the manual's partial example, but I am confused by this section:

> The user is expected to set the filed manually using the correct field tag::
>  MyMessage msg = MyMessage_init_zero;
>  msg.payload.msg2.value = true;
>  msg.which_payload = MyMessage_msg2_tag;
>
> Notice that neither ``which_payload`` field nor the unused fileds in ``payload``
> will consume any space in the resulting encoded message.

If the "which_payload" is not sent as part of the message, what is its purpose, and how does the decoder know which payload is used?

Is there a full example showing the encode and decode?

If not, please give me a few hints, and I will be very happy to share my example once its done.

Thanks!!!
- John

Petteri Aimonen

unread,
May 3, 2017, 9:16:41 AM5/3/17
to nan...@googlegroups.com
Hi,

> If the "which_payload" is not sent as part of the message, what is its
> purpose, and how does the decoder know which payload is used?

It is encoded as part of the tag number - the same way normal fields are
distinguished from eachother. I agree that the documentation is a bit
misleading on this part, but it is technically correct in that it
doesn't take any extra space compared to what every field takes anyway.

> Is there a full example showing the encode and decode?

This could help:
https://github.com/nanopb/nanopb/tree/master/tests/oneof

--
Petteri

John Rogers

unread,
May 3, 2017, 12:31:29 PM5/3/17
to nanopb
The tests/oneof looks excellent! I will check it out.

Question:
The "using unions" example uses all optional fields to implement a union...

"oneof" seems to be better, since optional fields don't seem to be forced to be mutually exclusive...

So no I am a bit confused about which should be used and when?

Or to put it another way... since "oneof" seems to be the more logical/appropriate choice... what circumstance would require or benefit from an optional field based "union"?

Thanks!!!

Petteri Aimonen

unread,
May 3, 2017, 1:26:17 PM5/3/17
to nan...@googlegroups.com
Hi,

> The "using unions" example uses all optional fields to implement a union...

I should probably remove that example in 0.4.0. It dates from 2013, back
when oneofs didn't exist yet.

--
Petteri

John Rogers

unread,
May 3, 2017, 9:18:14 PM5/3/17
to nanopb, j...@kapsi.fi
If it really is obsolete removing it makes sense, but if it still has some potential benefit, a comment in the readme would be enough.

But Thanks for clearing this up for me!

I figured one was the better way to go, and I thought that the 'oneof' was the preferred way, but very nice to know for sure!

Thanks again!!

-John

dvdve...@gmail.com

unread,
Jul 13, 2017, 5:58:53 PM7/13/17
to nanopb
so in proto 3 everything is "optional". Does this include the "one of"?

So it could more accurately be described as "one of or none" technically speaking.

Is this correct?

Petteri Aimonen

unread,
Jul 14, 2017, 3:52:27 AM7/14/17
to nan...@googlegroups.com
Hi,

> So it could more accurately be described as "one of or none" technically speaking.

Yes. Nanopb will set the tag field as 0 if the oneof field is missing
completely.

--
Petteri
Reply all
Reply to author
Forward
0 new messages