CHJani / google-api-java-client

Automatically exported from code.google.com/p/google-api-java-client
0 stars 0 forks source link

Generate an isX() method for boolean parameters #387

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
External references, such as a standards document, or specification?

https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest

Java environments (e.g. Java 6, Android 2.3, App Engine, or All)?

All

Please describe the feature requested.

For example, in Calendar v3 there is a "hidden" boolean parameter:

CalendarListEntry: {
id: "CalendarListEntry",
type: "object",
properties: {
hidden: {
type: "boolean",
description: "Whether the calendar has been hidden from the list. Optional. The 
default is False."
},
}

We generate Boolean getHidden() and we can write code like this:

if (entry.getHidden()) {
 ...
}

This works correctly if getHidden() is Boolean.TRUE and Boolean.FALSE.  
Unfortunately if getHidden() is null, this will throw a NullPointerException.  
If getHidden() is Data.NULL_BOOLEAN it will be true.  Neither of these two 
latter cases is intuitive.

So it would be convenient if we also generated an isHidden() method that 
conceptually looked like:

public boolean isHidden() {
  return hidden != null && hidden != Data.NULL_BOOLEAN && hidden.booleanValue();
}

So the above if-statement will behave correctly.  Note that the default for 
"hidden" is false so it works here, but it will not behave as expected for 
Booleans whose default is true.  So if we pretended that the default for hidden 
were true, we would want it implemented as:

public boolean isHidden() {
  return hidden == null || hidden != Data.NULL_BOOLEAN && hidden.booleanValue();
}

Original issue reported on code.google.com by yan...@google.com on 12 Jan 2012 at 4:04

GoogleCodeExporter commented 9 years ago
There is an important error in my assumptions above: we cannot actually assume 
that the default is "true" if the field is null.  The default might be "false". 
 The only way to know would be to use the "default" key, but currently none of 
the Google APIs are using it.  I would recommend we shelve this feature request 
until this "default" key is implemented.

Another error: we should treat null and Data.NULL_BOOLEAN as the same case, 
since the latter is just a way to specify the intention of "deleting" the field 
which means to reset it to the default.

Original comment by yan...@google.com on 6 Feb 2012 at 3:01

GoogleCodeExporter commented 9 years ago

Original comment by rmis...@google.com on 21 May 2012 at 2:47

GoogleCodeExporter commented 9 years ago
Added the method to the generated libraries.

Original comment by rmis...@google.com on 22 May 2012 at 5:32