[protobuf] Re: Missing has method for optional fields in proto3 ?

2017-09-15 Thread 次华李
If you need a field which is "an int32 value, or not set" you can use the 
google.protobuf.Int32Value message type.
 In proto  file,Which field type I can use?

在 2015年7月17日星期五 UTC+8下午9:10:29,Jon Skeet写道:
>
> On Friday, 17 July 2015 02:34:21 UTC+1, Wu XIANG wrote:
>>
>>
>>   Is it true that has methods for optional fields in proto3 are removed 
>> from generated message ?
>>
>
> For primitive types, yes.
>  
>
>>   If so, how can I decide if this field has been set or not ?
>>
>
> You can't. An int32 field that's been set to 0 is exactly the same as an 
> int32 field that hasn't been set at all.
>
> (For oneofs, you could still tell that it was the int32 field that was 
> set, so that's slightly difference.)
>
> From the documentation:
>
> > Note that for scalar message fields, once a message is parsed there's no 
> way of telling whether a field was explicitly set to the default value (for 
> example whether a boolean was set to false) or just not set at all: you 
> should bear this in mind when defining your message types.
>
> If you need a field which is "an int32 value, or not set" you can use the 
> google.protobuf.Int32Value message type.
>
> Jon
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.


[protobuf] Re: Missing has method for optional fields in proto3 ?

2015-07-17 Thread Jon Skeet
On Friday, 17 July 2015 02:34:21 UTC+1, Wu XIANG wrote:


   Is it true that has methods for optional fields in proto3 are removed 
 from generated message ?


For primitive types, yes.
 

   If so, how can I decide if this field has been set or not ?


You can't. An int32 field that's been set to 0 is exactly the same as an 
int32 field that hasn't been set at all.

(For oneofs, you could still tell that it was the int32 field that was set, 
so that's slightly difference.)

From the documentation:

 Note that for scalar message fields, once a message is parsed there's no 
way of telling whether a field was explicitly set to the default value (for 
example whether a boolean was set to false) or just not set at all: you 
should bear this in mind when defining your message types.

If you need a field which is an int32 value, or not set you can use the 
google.protobuf.Int32Value message type.

Jon

-- 
You received this message because you are subscribed to the Google Groups 
Protocol Buffers group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.