Inside a script, you can do:
def mymap = evaluate("[key1:value1,key2:value2]")
And in a class, you can instantiate a GroovyShell and do this:
def mymap = new GroovyShell().evaluate("[key1:value1,key2:value2]")
--
Guillaume Laforge
Groovy Project Manager
G2One, Inc. Vice-President Technology
http://www.g2one.com
---------------------------------------------------------------------
To unsubscribe from this list please visit:
Paul.
> <mailto:glaf...@gmail.com>> wrote:
>
> On Jan 13, 2008 9:46 PM, Kallin Nagelberg
Of course, you have to replace the dummy values with real values!
On Jan 13, 2008 10:01 PM, Kallin Nagelberg <kallin.n...@gmail.com > wrote:
> Thanks for the quick response!
>
> I've tried implementing it exactly as you put it, but I'm getting the
> following exception:
>
> Tests run: 2, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.485 sec
> <<< FAILURE!
> testValidCommands(CommandRunnerTest) Time elapsed: 0.437 sec <<< ERROR!
> groovy.lang.MissingPropertyException: No such property: value1 for class:
> Script1
> at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java :888)
> at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:2077)
> at
> groovy.lang.GroovyObjectSupport.getProperty (GroovyObjectSupport.java:65)
> at groovy.lang.Script.getProperty(Script.java:85)
> at
> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getGroovyObjectProperty(ScriptBytecodeAdapter.java:560)
> at Script1.run(Script1.groovy)
> at groovy.lang.GroovyShell.evaluate(GroovyShell.java:484)
> at groovy.lang.GroovyShell.evaluate(GroovyShell.java:459)
> at gjdk.groovy.lang.GroovyShell_GroovyReflector.invoke (Unknown Source)
> at groovy.lang.MetaMethod.invoke(MetaMethod.java:115)
> at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke
> (MetaClassHelper.java:713)
> at groovy.lang.MetaClassImpl.invokeMethod (MetaClassImpl.java:560)
> at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:450)
> at org.codehaus.groovy.runtime.Invoker.invokeMethod (Invoker.java:131)
> at
> org.codehaus.groovy.runtime.InvokerHelper.invokeMethod (InvokerHelper.java:111)
def mymap = new GroovyShell().evaluate("[key1: 'value1', key2: 'value2']")
Keys don't need to be quoted.
Kallin Nagelberg wrote:
> Ahh, I'm working with strictly string values here so I thought perhaps I
> could avoid quoting them. When I changed it to:
>
> def mymap = new
> GroovyShell().evaluate("[\"key1\":\"value1\",\"key2\":\"value2\"]")
> it works great !
>
>
> On Jan 13, 2008 4:07 PM, Guillaume Laforge <glaf...@gmail.com
> <mailto:glaf...@gmail.com>> wrote:
>
> Of course, you have to replace the dummy values with real values!
>
> On Jan 13, 2008 10:01 PM, Kallin Nagelberg
> <mailto:glaf...@gmail.com>> wrote:
> > >
> > >
> > >
> > >
> > >
> > >
> > > On Jan 13, 2008 9:46 PM, Kallin Nagelberg <
> kallin.n...@gmail.com <mailto:kallin.n...@gmail.com>>
Hmm. This should work:
println
Eval.me('[key1:"value1",key2:"value2",key3:[nestedkey1:"nestedvalue1",nestedkey2:"nestedvalue2"]]')
I have sorrounded the values with doublequotes, because of they are not
properties but Strings...
And I am not sure how the performance of Eval.me is...
Thorsten
--
Thorsten Kamann
Software-Architect, Consultant, Coaching
Germany, NRW
thorste...@googlemail.com
http://www.thorsten-kamann.de/
callto://thorque
Fornax-Platform - Platform for developing MDSD-related Tools and components
http://www.fornax-platform.org/
Instead of using regex, you can also set a delegate that resolves the identifiers to strings. In the end, the whole thing is a micro-DSL anyway.