You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Narahari Lakshminarayana <it...@gmail.com> on 2019/03/20 22:23:23 UTC
groovyscript and binding, what is wrong in this code
Friends:
Thank you in advance for your help.
I have the following groovyscript code.
def zebra_test1_list = [ "zebra1", "zebra2" ]
def zebra_test2_list = [ "zebra3", "zebra4" ]
def data="test1"
def zebra = groovy.util.Eval.me("zebra_${data}_list")
println zebra
I get the error
groovy> def zebra_test1_list = [ "zebra1", "zebra2" ]
groovy> def zebra_test2_list = [ "zebra3", "zebra4" ]
groovy> def data="test1"
groovy> def zebra = groovy.util.Eval.me("zebra_${data}_list")
groovy> println zebra
Exception thrown
groovy.lang.MissingPropertyException: No such property: zebra_test1_list
for class: Script1
at Script1.run(Script1.groovy:1)
at ConsoleScript2.run(ConsoleScript2:5)
Please help as to what I might be doing wrong.
-N
Re: groovyscript and binding, what is wrong in this code
Posted by Narahari Lakshminarayana <it...@gmail.com>.
Thank you Paul for the help and explanation.
Couple of questions.
If I tried to use the Eval.me (stubborn me), it did not work and failed.
You mentioned that Eval.me is for full binding.
Should I infer that if I want to Eval.me, I have to use binding and set the
variables in there so they come in scope ? (A sample would be helpful)
Is that @Field annotation new and can you share the link to that
documentation ?
Regards and thanks again,
-N
On Wed, Mar 20, 2019 at 8:01 PM Paul King <pa...@asert.com.au> wrote:
> You probably want to use the binding and just normal script evaluate rather
> than
> Eval.me which is designed for when you don't want the full binding.
>
> zebra_test1_list = [ 'zebra1', 'zebra2' ]
> zebra_test2_list = [ 'zebra3', 'zebra4' ]
>
> def zebras = (1..2).collect {
> evaluate("zebra_${'test' + it}_list")
> }
>
> assert zebras == [['zebra1', 'zebra2'],
> ['zebra3', 'zebra4']]
>
> Note that there is no def for the first two lines, otherwise
> you are defining local variables which are normally visible
> but not within the new context created when using evaluate.
>
> Cheers, Paul.
>
>
> On Thu, Mar 21, 2019 at 8:23 AM Narahari Lakshminarayana <
> itsme.narahari@gmail.com> wrote:
>
>> Friends:
>>
>> Thank you in advance for your help.
>>
>> I have the following groovyscript code.
>>
>> def zebra_test1_list = [ "zebra1", "zebra2" ]
>> def zebra_test2_list = [ "zebra3", "zebra4" ]
>>
>> def data="test1"
>> def zebra = groovy.util.Eval.me("zebra_${data}_list")
>>
>> println zebra
>>
>>
>> I get the error
>>
>> groovy> def zebra_test1_list = [ "zebra1", "zebra2" ]
>> groovy> def zebra_test2_list = [ "zebra3", "zebra4" ]
>> groovy> def data="test1"
>> groovy> def zebra = groovy.util.Eval.me("zebra_${data}_list")
>> groovy> println zebra
>>
>> Exception thrown
>>
>> groovy.lang.MissingPropertyException: No such property: zebra_test1_list
>> for class: Script1
>> at Script1.run(Script1.groovy:1)
>> at ConsoleScript2.run(ConsoleScript2:5)
>>
>>
>> Please help as to what I might be doing wrong.
>>
>> -N
>>
>
Re: groovyscript and binding, what is wrong in this code
Posted by Paul King <pa...@asert.com.au>.
I should have also mentioned that you don't need to even use evaluate if
you don't want. I'd suggest using @Field in that case:
import groovy.transform.Field
@Field zebra_test1_list = [ "zebra1", "zebra2" ]
@Field zebra_test2_list = [ "zebra3", "zebra4" ]
def zebras = (1..2).collect {
this."zebra_${'test' + it}_list"
}
assert zebras == [['zebra1', 'zebra2'],
['zebra3', 'zebra4']]
Cheers, Paul.
On Thu, Mar 21, 2019 at 10:00 AM Paul King <pa...@asert.com.au> wrote:
> You probably want to use the binding and just normal script evaluate rather
> than
> Eval.me which is designed for when you don't want the full binding.
>
> zebra_test1_list = [ 'zebra1', 'zebra2' ]
> zebra_test2_list = [ 'zebra3', 'zebra4' ]
>
> def zebras = (1..2).collect {
> evaluate("zebra_${'test' + it}_list")
> }
>
> assert zebras == [['zebra1', 'zebra2'],
> ['zebra3', 'zebra4']]
>
> Note that there is no def for the first two lines, otherwise
> you are defining local variables which are normally visible
> but not within the new context created when using evaluate.
>
> Cheers, Paul.
>
>
> On Thu, Mar 21, 2019 at 8:23 AM Narahari Lakshminarayana <
> itsme.narahari@gmail.com> wrote:
>
>> Friends:
>>
>> Thank you in advance for your help.
>>
>> I have the following groovyscript code.
>>
>> def zebra_test1_list = [ "zebra1", "zebra2" ]
>> def zebra_test2_list = [ "zebra3", "zebra4" ]
>>
>> def data="test1"
>> def zebra = groovy.util.Eval.me("zebra_${data}_list")
>>
>> println zebra
>>
>>
>> I get the error
>>
>> groovy> def zebra_test1_list = [ "zebra1", "zebra2" ]
>> groovy> def zebra_test2_list = [ "zebra3", "zebra4" ]
>> groovy> def data="test1"
>> groovy> def zebra = groovy.util.Eval.me("zebra_${data}_list")
>> groovy> println zebra
>>
>> Exception thrown
>>
>> groovy.lang.MissingPropertyException: No such property: zebra_test1_list
>> for class: Script1
>> at Script1.run(Script1.groovy:1)
>> at ConsoleScript2.run(ConsoleScript2:5)
>>
>>
>> Please help as to what I might be doing wrong.
>>
>> -N
>>
>
Re: groovyscript and binding, what is wrong in this code
Posted by Paul King <pa...@asert.com.au>.
You probably want to use the binding and just normal script evaluate rather
than
Eval.me which is designed for when you don't want the full binding.
zebra_test1_list = [ 'zebra1', 'zebra2' ]
zebra_test2_list = [ 'zebra3', 'zebra4' ]
def zebras = (1..2).collect {
evaluate("zebra_${'test' + it}_list")
}
assert zebras == [['zebra1', 'zebra2'],
['zebra3', 'zebra4']]
Note that there is no def for the first two lines, otherwise
you are defining local variables which are normally visible
but not within the new context created when using evaluate.
Cheers, Paul.
On Thu, Mar 21, 2019 at 8:23 AM Narahari Lakshminarayana <
itsme.narahari@gmail.com> wrote:
> Friends:
>
> Thank you in advance for your help.
>
> I have the following groovyscript code.
>
> def zebra_test1_list = [ "zebra1", "zebra2" ]
> def zebra_test2_list = [ "zebra3", "zebra4" ]
>
> def data="test1"
> def zebra = groovy.util.Eval.me("zebra_${data}_list")
>
> println zebra
>
>
> I get the error
>
> groovy> def zebra_test1_list = [ "zebra1", "zebra2" ]
> groovy> def zebra_test2_list = [ "zebra3", "zebra4" ]
> groovy> def data="test1"
> groovy> def zebra = groovy.util.Eval.me("zebra_${data}_list")
> groovy> println zebra
>
> Exception thrown
>
> groovy.lang.MissingPropertyException: No such property: zebra_test1_list
> for class: Script1
> at Script1.run(Script1.groovy:1)
> at ConsoleScript2.run(ConsoleScript2:5)
>
>
> Please help as to what I might be doing wrong.
>
> -N
>