Missing has method for optional fields in proto3 ?

1,842 views
Skip to first unread message

Wu XIANG

unread,
Jul 16, 2015, 9:34:21 PM7/16/15
to prot...@googlegroups.com
Hi,

  Is it true that has methods for optional fields in proto3 are removed from generated message ?
  If so, how can I decide if this field has been set or not ?
  
syntax ="proto3";

message
Foo{

  int32 bar
= 1;

}


// generated Foo.java

class Foo ... {

 
int getBar();

 
// the following method is missing
 
// bool hasBar();

}


thanks 
wu

Jon Skeet

unread,
Jul 17, 2015, 9:10:29 AM7/17/15
to prot...@googlegroups.com
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

次华李

unread,
Sep 15, 2017, 3:37:48 AM9/15/17
to Protocol Buffers
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写道:
Reply all
Reply to author
Forward
0 new messages