You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by "Howard M. Lewis Ship (JIRA)" <de...@tapestry.apache.org> on 2007/12/21 00:03:43 UTC

[jira] Commented: (TAPESTRY-1928) Coercion from null to Boolean yields false instead of null

    [ https://issues.apache.org/jira/browse/TAPESTRY-1928?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12553814 ] 

Howard M. Lewis Ship commented on TAPESTRY-1928:
------------------------------------------------

This is predicated on the idea that the best way to represent a yes/no/maybe is with a Boolean, and I disagree on that.  Nulls are a dangerous thing that should be avoided when possible.  I think that null handling is a complex issue and not one size fits all and I'm noodling about that ... I suspect that there will be a new parameter on most fields to specify how to handle nulls.  Some of the other code in 5.0.7 (the new events for printing, parsing and validating) may also be useful for these cases.



> Coercion from null to Boolean yields false instead of null
> ----------------------------------------------------------
>
>                 Key: TAPESTRY-1928
>                 URL: https://issues.apache.org/jira/browse/TAPESTRY-1928
>             Project: Tapestry
>          Issue Type: Bug
>          Components: tapestry-ioc
>    Affects Versions: 5.0.6
>            Reporter: Geoff Callender
>
> Boolean has 3 states: null, true, false, but the coercer coerces null to false.  While this is correct behaviour for the boolean primitive (which has only 2 states), it is not correct for Boolean.
> The same argument applies here as in TAPESTRY-1648 which dealt with null BigDecimal and Integer.
> A practical consequence is that a BooleanValueEncoder that is 3-value aware becomes useless because the coercer will override it.  
> For example I combined a BooleanValueEncoder with Select to allow a user to choose between "", "true", and "false".  But when the user chooses "", they unexpectedly get the same result as choosing "false".  Here's the template code:
>    <select t:id="active" t:type="Select" value="active" model="actives" encoder="booleanValueEncoder"/>
> Here's the relevant java:
>    static private final Boolean[] ACTIVES = { null, Boolean.TRUE, Boolean.FALSE };
>    static private final BooleanValueEncoder BOOLEAN_VALUE_ENCODER = new BooleanValueEncoder();
>    public Boolean getActive() {
>       return _active;
>    }
>    public  void setActive(Boolean active) {
>       _active = active;
>    }
>    public Boolean[] getActives() {
>       return ACTIVES;
>    }
> 	
>    public BooleanValueEncoder getBooleanValueEncoder() {
>       return BOOLEAN_VALUE_ENCODER;
>    }
> Here's a BooleanValueEncoder:
>    public class BooleanValueEncoder implements ValueEncoder<Boolean> {
>       public String toClient(Boolean value) {
>          return value == null ? "" : value.toString();
>       }
>       public Boolean toValue(String clientValue) {
>          return clientValue.equals("") ? null : Boolean.valueOf(clientValue);
>       }
>    }
> When the user chooses "", Tapestry calls setActive(Boolean.FALSE) instead of setActive(null).

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tapestry.apache.org
For additional commands, e-mail: dev-help@tapestry.apache.org