defaultNullable in Validateable

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

defaultNullable in Validateable

longwa
Working on porting from Grails 2.4 to 3.0.9 and came across something confusing. The @Validateable annotation used to take nullable=true to indicate that the default behavior should be to default unspecified properties to nullable instead of non-nullable.

In Grails 3, the Validateable trait has a defaultNullable() method which returns a boolean. It seems the best option would be to override the method and return true instead of false. However, for some reason the method is static.

Is there some reason this method is static? 

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/7f22bff2-05bd-47fa-9b3b-3366d91ba69d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: defaultNullable in Validateable

Jeff Brown

> On Oct 20, 2015, at 11:32 PM, Aaron <[hidden email]> wrote:
>
> Working on porting from Grails 2.4 to 3.0.9 and came across something confusing. The @Validateable annotation used to take nullable=true to indicate that the default behavior should be to default unspecified properties to nullable instead of non-nullable.
>
> In Grails 3, the Validateable trait has a defaultNullable() method which returns a boolean. It seems the best option would be to override the method and return true instead of false. However, for some reason the method is static.
>
> Is there some reason this method is static?

I think it should be static unless there is a reason for it to be an instance method.  I don’t think it would ever make sense to have the default nullable behavior be bound to an instance.



JSB

Jeff Scott Brown
Principal Software Engineer
Grails Development Team
Object Computing Inc.
http://www.ociweb.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/081465C3-FDC6-4DC6-8642-E1A24596C243%40ociweb.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: defaultNullable in Validateable

longwa
I was thinking it needed to be non-static so I could override it in classes that implement the trait. However, playing around I see that implementing classes can apparently override static methods as well in the trait (I guess I was thinking about it too 'java').

If I had @Validateable(nullable = true) on a command object, would switching to implements Validateable and then adding static boolean defaultNullable() { return true } be the way to get the same behavior in Grails 3?

Thanks,
Aaron

On Wednesday, October 21, 2015 at 6:50:38 AM UTC-4, Jeff Scott Brown wrote:

> On Oct 20, 2015, at 11:32 PM, Aaron <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="yWkBt6-uCwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">lon...@...> wrote:
>
> Working on porting from Grails 2.4 to 3.0.9 and came across something confusing. The @Validateable annotation used to take nullable=true to indicate that the default behavior should be to default unspecified properties to nullable instead of non-nullable.
>
> In Grails 3, the Validateable trait has a defaultNullable() method which returns a boolean. It seems the best option would be to override the method and return true instead of false. However, for some reason the method is static.
>
> Is there some reason this method is static?

I think it should be static unless there is a reason for it to be an instance method.  I don’t think it would ever make sense to have the default nullable behavior be bound to an instance.



JSB

Jeff Scott Brown
Principal Software Engineer
Grails Development Team
Object Computing Inc.
<a href="http://www.ociweb.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fwww.ociweb.com%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNGmJq4FzWg40kVUyr9KFZMiSj7t1Q&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fwww.ociweb.com%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNGmJq4FzWg40kVUyr9KFZMiSj7t1Q&#39;;return true;">http://www.ociweb.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
<a href="http://www.autismspeaks.org/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fwww.autismspeaks.org%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNHLOilSQBYB1lzLN6Ms6K6DtQY5DQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fwww.autismspeaks.org%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNHLOilSQBYB1lzLN6Ms6K6DtQY5DQ&#39;;return true;">http://www.autismspeaks.org/

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/a778d889-b804-4c37-85bf-cf70a4475105%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: defaultNullable in Validateable

Jeff Brown

> On Oct 21, 2015, at 8:49 AM, Aaron <[hidden email]> wrote:
>
> I was thinking it needed to be non-static so I could override it in classes that implement the trait. However, playing around I see that implementing classes can apparently override static methods as well in the trait (I guess I was thinking about it too 'java').
>
> If I had @Validateable(nullable = true) on a command object, would switching to implements Validateable and then adding static boolean defaultNullable() { return true } be the way to get the same behavior in Grails 3?


It should, yes.

There is some test code at https://github.com/grails/grails-core/blob/v3.0.9/grails-test-suite-uber/src/test/groovy/grails/validation/ValidateableTraitSpec.groovy which includes testing a class which overrides defaultNullable().  There are also some docs at http://grails.github.io/grails-doc/3.0.9/guide/theWebLayer.html#commandObjects which shows an example.  The Validateable docs are a little fragmented right now.  The notes at http://grails.github.io/grails-doc/3.0.9/guide/validation.html#validationNonDomainAndCommandObjectClasses should be aligned with the command object notes I linked above.  Pull requests to https://github.com/grails/grails-doc are welcome.  :)



JSB

Jeff Scott Brown
Principal Software Engineer
Grails Development Team
Object Computing Inc.
http://www.ociweb.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/08FC4F99-FFD8-45B6-8CC2-05610949A10D%40ociweb.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: defaultNullable in Validateable

longwa
Thanks, I'll play around with it.

IMO, it still might be preferable to have the trait method not be static. The problem with overriding a static method in a trait is that the IDE has no idea what's going on with it. IntelliJ thinks at best the method is unused. If you try to add @Override, it just flat out hates it. I guess if someone wanted to have a per-instance definition of that, it wouldn't preclude it either (although I agree I can't see how that would be useful).

-Aaron

On Wednesday, October 21, 2015 at 10:30:36 AM UTC-4, Jeff Scott Brown wrote:

> On Oct 21, 2015, at 8:49 AM, Aaron <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="7GhogbC6CwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">lon...@...> wrote:
>
> I was thinking it needed to be non-static so I could override it in classes that implement the trait. However, playing around I see that implementing classes can apparently override static methods as well in the trait (I guess I was thinking about it too 'java').
>
> If I had @Validateable(nullable = true) on a command object, would switching to implements Validateable and then adding static boolean defaultNullable() { return true } be the way to get the same behavior in Grails 3?


It should, yes.

There is some test code at <a href="https://github.com/grails/grails-core/blob/v3.0.9/grails-test-suite-uber/src/test/groovy/grails/validation/ValidateableTraitSpec.groovy" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fgrails%2Fgrails-core%2Fblob%2Fv3.0.9%2Fgrails-test-suite-uber%2Fsrc%2Ftest%2Fgroovy%2Fgrails%2Fvalidation%2FValidateableTraitSpec.groovy\46sa\75D\46sntz\0751\46usg\75AFQjCNEnp513o9AXm65zm9vLf9y0yRo54Q&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fgrails%2Fgrails-core%2Fblob%2Fv3.0.9%2Fgrails-test-suite-uber%2Fsrc%2Ftest%2Fgroovy%2Fgrails%2Fvalidation%2FValidateableTraitSpec.groovy\46sa\75D\46sntz\0751\46usg\75AFQjCNEnp513o9AXm65zm9vLf9y0yRo54Q&#39;;return true;">https://github.com/grails/grails-core/blob/v3.0.9/grails-test-suite-uber/src/test/groovy/grails/validation/ValidateableTraitSpec.groovy which includes testing a class which overrides defaultNullable().  There are also some docs at <a href="http://grails.github.io/grails-doc/3.0.9/guide/theWebLayer.html#commandObjects" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fgrails.github.io%2Fgrails-doc%2F3.0.9%2Fguide%2FtheWebLayer.html%23commandObjects\46sa\75D\46sntz\0751\46usg\75AFQjCNGc8KBn2ZxahVRj1PUPNG8v1SjkxA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fgrails.github.io%2Fgrails-doc%2F3.0.9%2Fguide%2FtheWebLayer.html%23commandObjects\46sa\75D\46sntz\0751\46usg\75AFQjCNGc8KBn2ZxahVRj1PUPNG8v1SjkxA&#39;;return true;">http://grails.github.io/grails-doc/3.0.9/guide/theWebLayer.html#commandObjects which shows an example.  The Validateable docs are a little fragmented right now.  The notes at <a href="http://grails.github.io/grails-doc/3.0.9/guide/validation.html#validationNonDomainAndCommandObjectClasses" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fgrails.github.io%2Fgrails-doc%2F3.0.9%2Fguide%2Fvalidation.html%23validationNonDomainAndCommandObjectClasses\46sa\75D\46sntz\0751\46usg\75AFQjCNHsLNuxcUqWT-w1kHqhlnDGNegxdg&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fgrails.github.io%2Fgrails-doc%2F3.0.9%2Fguide%2Fvalidation.html%23validationNonDomainAndCommandObjectClasses\46sa\75D\46sntz\0751\46usg\75AFQjCNHsLNuxcUqWT-w1kHqhlnDGNegxdg&#39;;return true;">http://grails.github.io/grails-doc/3.0.9/guide/validation.html#validationNonDomainAndCommandObjectClasses should be aligned with the command object notes I linked above.  Pull requests to <a href="https://github.com/grails/grails-doc" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fgrails%2Fgrails-doc\46sa\75D\46sntz\0751\46usg\75AFQjCNEtUvBHwbsPcb2vsDK8vMqr2nUZVQ&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fgrails%2Fgrails-doc\46sa\75D\46sntz\0751\46usg\75AFQjCNEtUvBHwbsPcb2vsDK8vMqr2nUZVQ&#39;;return true;">https://github.com/grails/grails-doc are welcome.  :)



JSB

Jeff Scott Brown
Principal Software Engineer
Grails Development Team
Object Computing Inc.
<a href="http://www.ociweb.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fwww.ociweb.com%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNGmJq4FzWg40kVUyr9KFZMiSj7t1Q&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fwww.ociweb.com%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNGmJq4FzWg40kVUyr9KFZMiSj7t1Q&#39;;return true;">http://www.ociweb.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
<a href="http://www.autismspeaks.org/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fwww.autismspeaks.org%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNHLOilSQBYB1lzLN6Ms6K6DtQY5DQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fwww.autismspeaks.org%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNHLOilSQBYB1lzLN6Ms6K6DtQY5DQ&#39;;return true;">http://www.autismspeaks.org/

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/012df279-5062-4dcd-bffa-7b1b635a2d23%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: defaultNullable in Validateable

Jeff Brown

> On Oct 21, 2015, at 12:49 PM, Aaron <[hidden email]> wrote:
> The problem with overriding a static method in a trait is that the IDE has no idea what's going on with it.

It doesn’t surprise me that you can’t mark a static trait method with @Override.  A method marked with @Override must be overriding a method inherited from a parent class or from an implemented interface.  Instance methods in a Trait are represented in the corresponding interface.  Static methods aren’t.



JSB

Jeff Scott Brown
Principal Software Engineer
Grails Development Team
Object Computing Inc.
http://www.ociweb.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/B59C5645-CE98-4900-B15B-CAB7CA62EE2B%40ociweb.com.
For more options, visit https://groups.google.com/d/optout.