How exactly does groovy understand the static mapping in Grails?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

How exactly does groovy understand the static mapping in Grails?

yogesh.singh

I am new to Grails framework and I came across this code

static mapping = {
        cache true
    }

I know Grails is on Groovy, but I am not understanding how Groovy understands this? Is this a Closure and If yes, the this doesnt seem resemble Closure. As far as I know, if a method is passed a closure it is defined something like this:

def helloWorld = {
  println "Hello World"
}

But this static mapping doesnt seem to be Closure as well. How does Grails leverage Groovy to do this ORM mapping? I hope I am articulating my question well!

--
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/3f1b0376-9339-4d5f-a109-f1f5f4db5b77%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How exactly does groovy understand the static mapping in Grails?

Jeff Scott Brown-4
On 21 Jan 2019, at 14:14, [hidden email] wrote:

> I am new to Grails framework and I came across this code
>
> static mapping = {
>         cache true
>     }
>
> I know Grails is on Groovy, but I am not understanding how Groovy
> understands this? Is this a Closure and If yes, the this doesnt seem
> resemble Closure. As far as I know, if a method is passed a closure it
> is
> defined something like this:
>
> def helloWorld = {
>   println "Hello World"}
>
> But this static mapping doesnt seem to be Closure as well. How does
> Grails
> leverage Groovy to do this ORM mapping? I hope I am articulating my
> question well!
>

Yes, it is a closure.

static mapping = {
    // code here
}

def helloWorld = {
    // code here
}

Those are both closures.  The closure is an object.  The first one is a
variable being declared as a static member and the second is being
declared as an instance member.  Those are similar to the following:

static name = ‘Jeff’

def name = ‘Jeff’

In those, the object is a String, not a Closure.  The first one is a
static member and the second is an instance member.

Does that make sense?



JSB


--
Jeff Scott Brown
Partner and Practice Lead, Grails and Micronaut

Disruptive solutions for a connected world.™
http://objectcomputing.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/511E05ED-AF82-4007-A17B-D81586A292D4%40objectcomputing.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How exactly does groovy understand the static mapping in Grails?

yogesh.singh
Thanks JSB,
So How does Grail pick this mapping? where is this function defined by grails? there must be some place where this closure is ultimately passed? Can I see that so that its clear tp me. 
does it look something like this

def mapping{
}




On Monday, January 21, 2019 at 3:56:15 PM UTC-5, Jeff Scott Brown wrote:
On 21 Jan 2019, at 14:14, <a href="javascript:" target="_blank" gdf-obfuscated-mailto="9fw3VbwKEwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">yogesh...@... wrote:

> I am new to Grails framework and I came across this code
>
> static mapping = {
>         cache true
>     }
>
> I know Grails is on Groovy, but I am not understanding how Groovy
> understands this? Is this a Closure and If yes, the this doesnt seem
> resemble Closure. As far as I know, if a method is passed a closure it
> is
> defined something like this:
>
> def helloWorld = {
>   println "Hello World"}
>
> But this static mapping doesnt seem to be Closure as well. How does
> Grails
> leverage Groovy to do this ORM mapping? I hope I am articulating my
> question well!
>

Yes, it is a closure.

static mapping = {
    // code here
}

def helloWorld = {
    // code here
}

Those are both closures.  The closure is an object.  The first one is a
variable being declared as a static member and the second is being
declared as an instance member.  Those are similar to the following:

static name = ‘Jeff’

def name = ‘Jeff’

In those, the object is a String, not a Closure.  The first one is a
static member and the second is an instance member.

Does that make sense?



JSB


--
Jeff Scott Brown
Partner and Practice Lead, Grails and Micronaut

Disruptive solutions for a connected world.™
<a href="http://objectcomputing.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fobjectcomputing.com\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNF4l-VVLHJqOYP3KlZd-oxBJe6n3Q&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fobjectcomputing.com\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNF4l-VVLHJqOYP3KlZd-oxBJe6n3Q&#39;;return true;">http://objectcomputing.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\x3dhttp%3A%2F%2Fwww.autismspeaks.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHLOilSQBYB1lzLN6Ms6K6DtQY5DQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.autismspeaks.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHLOilSQBYB1lzLN6Ms6K6DtQY5DQ&#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/34a74b88-5132-4f16-bd8e-619e75e908b1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How exactly does groovy understand the static mapping in Grails?

Mihai Glonț


On 21/01/2019 21:21, [hidden email] wrote:
Thanks JSB,
So How does Grail pick this mapping? where is this function defined by grails? there must be some place where this closure is ultimately passed? Can I see that so that its clear tp me. 
does it look something like this

def mapping{
}

In very simple terms Grails considers URL mappings as one of its special kinds of artefacts, like domain classes or controllers and it has means of keeping an eye on the list of mappings for a given application.

When the UrlMappings plugin[1] is loaded it configures a number of beans[2], including UrlMappingsFactoryBean[3]. The latter uses the DefaultUrlMappingEvaluator[4] to interpret all those "mappings" closures and convert them into corresponding UrlMapping instances. You can see what url mappings are registered in an application by inspecting the grailsUrlMappingsHolderBean bean in a Grails console[5].

Hope this helps,

Mihai.

[1] https://docs.grails.org/latest/ref/Plug-ins/URL%20mappings.html
[2] https://github.com/grails/grails-core/blob/v3.3.9/grails-plugin-url-mappings/src/main/groovy/org/grails/plugins/web/mapping/UrlMappingsGrailsPlugin.groovy#L120
[3] https://github.com/grails/grails-core/blob/v3.3.9/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/UrlMappingsHolderFactoryBean.java#L90
[4] https://github.com/grails/grails-core/blob/v3.3.9/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/DefaultUrlMappingEvaluator.java#L194
[5] https://docs.grails.org/latest/ref/Command%20Line/console.html



On Monday, January 21, 2019 at 3:56:15 PM UTC-5, Jeff Scott Brown wrote:
On 21 Jan 2019, at 14:14, <a href="javascript:" target="_blank" gdf-obfuscated-mailto="9fw3VbwKEwAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;" moz-do-not-send="true">yogesh...@... wrote:

> I am new to Grails framework and I came across this code
>
> static mapping = {
>         cache true
>     }
>
> I know Grails is on Groovy, but I am not understanding how Groovy
> understands this? Is this a Closure and If yes, the this doesnt seem
> resemble Closure. As far as I know, if a method is passed a closure it
> is
> defined something like this:
>
> def helloWorld = {
>   println "Hello World"}
>
> But this static mapping doesnt seem to be Closure as well. How does
> Grails
> leverage Groovy to do this ORM mapping? I hope I am articulating my
> question well!
>

Yes, it is a closure.

static mapping = {
    // code here
}

def helloWorld = {
    // code here
}

Those are both closures.  The closure is an object.  The first one is a
variable being declared as a static member and the second is being
declared as an instance member.  Those are similar to the following:

static name = ‘Jeff’

def name = ‘Jeff’

In those, the object is a String, not a Closure.  The first one is a
static member and the second is an instance member.

Does that make sense?



JSB


--
Jeff Scott Brown
Partner and Practice Lead, Grails and Micronaut

Disruptive solutions for a connected world.™
<a href="http://objectcomputing.com" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fobjectcomputing.com\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNF4l-VVLHJqOYP3KlZd-oxBJe6n3Q';return true;" onclick="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fobjectcomputing.com\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNF4l-VVLHJqOYP3KlZd-oxBJe6n3Q';return true;" moz-do-not-send="true">http://objectcomputing.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='http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.autismspeaks.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHLOilSQBYB1lzLN6Ms6K6DtQY5DQ';return true;" onclick="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.autismspeaks.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHLOilSQBYB1lzLN6Ms6K6DtQY5DQ';return true;" moz-do-not-send="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/34a74b88-5132-4f16-bd8e-619e75e908b1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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/9c5f6b4e-f9ab-8638-6821-acb4321a3ae3%40ebi.ac.uk.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How exactly does groovy understand the static mapping in Grails?

Jeff Scott Brown-4
On 21 Jan 2019, at 16:09, Mihai Glonț wrote:

> On 21/01/2019 21:21, [hidden email] wrote:
>> Thanks JSB,
>> So How does Grail pick this mapping? where is this function defined
>> by
>> grails? there must be some place where this closure is ultimately
>> passed? Can I see that so that its clear tp me. 
>> does it look something like this
>>
>> |
>> defmapping{
>> }
>>
>> |
>
> In very simple terms Grails considers URL mappings as one of its
> special
> kinds of artefacts, like domain classes or controllers and it has
> means
> of keeping an eye on the list of mappings for a given application.
>
> When the UrlMappings plugin[1] is loaded it configures a number of
> beans[2], including UrlMappingsFactoryBean[3]. The latter uses the
> DefaultUrlMappingEvaluator[4] to interpret all those "mappings"
> closures
> and convert them into corresponding UrlMapping instances. You can see
> what url mappings are registered in an application by inspecting the
> grailsUrlMappingsHolderBean bean in a Grails console[5].
>
> Hope this helps,
>
> Mihai.
>

Judging from the sample code in the original question, I don’t think
the question is about URL mapping.  I think it is about domain mapping.



JSB

--
Jeff Scott Brown
Partner and Practice Lead, Grails and Micronaut

Disruptive solutions for a connected world.™
http://objectcomputing.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/C374D86D-AA7B-4EAA-9054-ABB4D5F37F14%40objectcomputing.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How exactly does groovy understand the static mapping in Grails?

yogesh.singh
Yes,
So Where can I see the implementation of this domain mapping?
I am still not clear how Groovy or Grails understands closures for property/field?

On Monday, January 21, 2019 at 9:22:10 PM UTC-5, Jeff Scott Brown wrote:
On 21 Jan 2019, at 16:09, Mihai Glonț wrote:

> On 21/01/2019 21:21, <a href="javascript:" target="_blank" gdf-obfuscated-mailto="_gEGLoUcEwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">yogesh...@... wrote:
>> Thanks JSB,
>> So How does Grail pick this mapping? where is this function defined
>> by
>> grails? there must be some place where this closure is ultimately
>> passed? Can I see that so that its clear tp me. 
>> does it look something like this
>>
>> |
>> defmapping{
>> }
>>
>> |
>
> In very simple terms Grails considers URL mappings as one of its
> special
> kinds of artefacts, like domain classes or controllers and it has
> means
> of keeping an eye on the list of mappings for a given application.
>
> When the UrlMappings plugin[1] is loaded it configures a number of
> beans[2], including UrlMappingsFactoryBean[3]. The latter uses the
> DefaultUrlMappingEvaluator[4] to interpret all those "mappings"
> closures
> and convert them into corresponding UrlMapping instances. You can see
> what url mappings are registered in an application by inspecting the
> grailsUrlMappingsHolderBean bean in a Grails console[5].
>
> Hope this helps,
>
> Mihai.
>

Judging from the sample code in the original question, I don’t think
the question is about URL mapping.  I think it is about domain mapping.



JSB

--
Jeff Scott Brown
Partner and Practice Lead, Grails and Micronaut

Disruptive solutions for a connected world.™
<a href="http://objectcomputing.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fobjectcomputing.com\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNF4l-VVLHJqOYP3KlZd-oxBJe6n3Q&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fobjectcomputing.com\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNF4l-VVLHJqOYP3KlZd-oxBJe6n3Q&#39;;return true;">http://objectcomputing.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\x3dhttp%3A%2F%2Fwww.autismspeaks.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHLOilSQBYB1lzLN6Ms6K6DtQY5DQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.autismspeaks.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHLOilSQBYB1lzLN6Ms6K6DtQY5DQ&#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/93457457-48ac-4853-9c7e-e7b3763bf25d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How exactly does groovy understand the static mapping in Grails?

Mihai Glonț

My bad.

In that case, you need to look at GrailsValidationApi and friends:

https://github.com/grails/grails-data-mapping/blob/master/grails-datastore-gorm/src/main/groovy/org/grails/datastore/gorm/GormValidationApi.groovy#L86

https://github.com/grails/grails-core/blob/v3.1.9/grails-validation/src/main/groovy/org/grails/validation/GrailsDomainClassValidator.java#L76

https://github.com/grails/grails-data-mapping/blob/master/grails-datastore-gorm-validation/src/main/groovy/grails/gorm/validation/DefaultConstrainedProperty.groovy#L669

In short, a dedicated validation bean is instantiated for every type of constraint you define in a domain class.

Does this help?

On 22/01/2019 02:39, [hidden email] wrote:
Yes,
So Where can I see the implementation of this domain mapping?
I am still not clear how Groovy or Grails understands closures for property/field?

On Monday, January 21, 2019 at 9:22:10 PM UTC-5, Jeff Scott Brown wrote:
On 21 Jan 2019, at 16:09, Mihai Glonț wrote:

> On 21/01/2019 21:21, <a href="javascript:" target="_blank" gdf-obfuscated-mailto="_gEGLoUcEwAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;" moz-do-not-send="true">yogesh...@... wrote:
>> Thanks JSB,
>> So How does Grail pick this mapping? where is this function defined
>> by
>> grails? there must be some place where this closure is ultimately
>> passed? Can I see that so that its clear tp me. 
>> does it look something like this
>>
>> |
>> defmapping{
>> }
>>
>> |
>
> In very simple terms Grails considers URL mappings as one of its
> special
> kinds of artefacts, like domain classes or controllers and it has
> means
> of keeping an eye on the list of mappings for a given application.
>
> When the UrlMappings plugin[1] is loaded it configures a number of
> beans[2], including UrlMappingsFactoryBean[3]. The latter uses the
> DefaultUrlMappingEvaluator[4] to interpret all those "mappings"
> closures
> and convert them into corresponding UrlMapping instances. You can see
> what url mappings are registered in an application by inspecting the
> grailsUrlMappingsHolderBean bean in a Grails console[5].
>
> Hope this helps,
>
> Mihai.
>

Judging from the sample code in the original question, I don’t think
the question is about URL mapping.  I think it is about domain mapping.



JSB

--
Jeff Scott Brown
Partner and Practice Lead, Grails and Micronaut

Disruptive solutions for a connected world.™
<a href="http://objectcomputing.com" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fobjectcomputing.com\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNF4l-VVLHJqOYP3KlZd-oxBJe6n3Q';return true;" onclick="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fobjectcomputing.com\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNF4l-VVLHJqOYP3KlZd-oxBJe6n3Q';return true;" moz-do-not-send="true">http://objectcomputing.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='http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.autismspeaks.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHLOilSQBYB1lzLN6Ms6K6DtQY5DQ';return true;" onclick="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.autismspeaks.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHLOilSQBYB1lzLN6Ms6K6DtQY5DQ';return true;" moz-do-not-send="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/93457457-48ac-4853-9c7e-e7b3763bf25d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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/fce58038-c12c-5536-0db6-903dee45e0f3%40ebi.ac.uk.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How exactly does groovy understand the static mapping in Grails?

Jeff Scott Brown-4
On 25 Jan 2019, at 16:25, Mihai Glonț wrote:

I think the original question is referencing the domain mapping api, not
the validation api.

https://github.com/grails/gorm-hibernate5/blob/master/grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/cfg/HibernateMappingBuilder.groovy#L371



JSB

--
Jeff Scott Brown
Partner and Practice Lead, Grails and Micronaut

Disruptive solutions for a connected world.™
http://objectcomputing.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/DD962477-4CD2-4D34-ACA0-0B2295C8868D%40objectcomputing.com.
For more options, visit https://groups.google.com/d/optout.