This question is best to be asked in protobuf's thread 
<https://groups.google.com/g/protobuf>. 

To switch between setting data A or B in message C, you just use setters 
for A or B from message C's builder. That last call wins. (FWIW, internally 
calling the setter for fields in oneof data sets a *specifier* pointing to 
it. When you use a getter to read fields in oneof data, the getter always 
checks the specifier first and only return your value if the specifier 
points to that field. Otherwise, default value is returned (remember, every 
field has a default value)).

You *cannot* set both A and B. Even if you set both of them on the server 
side, only the one you set lastly will be transmitted to the client side 
and the client can only read that field's value. This is the semantics of 
*oneof*, if you have use case of setting both, don't use oneof, just 
flatten fields in the message.

For more information of oneof, please see protobuf's documentation 
<https://developers.google.com/protocol-buffers/docs/proto3#oneof>.
On Thursday, June 18, 2020 at 9:10:14 AM UTC-7 rohit wrote:

> Hello All
>
> I know the functionality of  *oneof *in protofile to switch  but don't 
> know exactly how to use it . 
> I have a problem with similar structure.
>
> //proto 3
>
> //Client side
>
> message A
> {
> }
>
> message B
> {
> }
>
>
> message C
> {
>   oneof Data
> {
>     A data1 =20;
>     B data2 = 30;
> }
> }
>
>
> Now at the server side I will sending one data A or B to be received by 
> Client. Which is the best method to switch between the usage of the data.
> How to handle the situation when I have to send both A & B to be received 
> by Client. Any alternative of *oneof *Because oneof will clear the other 
> message.
>
> Can anyone provide links for the the examples highlighting how to switch 
> between the messages, shall be very helpful,
>
> Thanks and Regards
> Rohit
>

-- 
You received this message because you are subscribed to the Google Groups 
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/grpc-io/858f68ff-21d7-4484-8e78-29650fcc225cn%40googlegroups.com.

Reply via email to