Eval.x(v) should just work as well.
Regarding resources / performance, it means spinning the parser and compiler, so it's not for free. Doing your own string parsing logic might be more efficient.
Depends on how frequently you have to do that.Le 20 avr. 2016 07:57, "Gerald Wiltse" <email@example.com> a écrit :Wow, i just wrote that exact code basically... and started responding to your email, but there were various drawbacks to this approach as I don't want to have to define handling of every property by name... Then... it hit me...def v = '1..10'assert new GroovyShell().parse(v).run() == [1,2,3,4,5]It works!!!Something about the Eval works just a little bit differently than GroovyShell i guess. Perhaps Dierk can explain.Last question, how expensive is this invocation of groovyshell and parse and all that (resources wise)? So-so?On Wed, Apr 20, 2016 at 1:48 AM, Guillaume Laforge <firstname.lastname@example.org> wrote:If you know it's a range when parsing that string, you can do this, with the toInteger() method:def rangeString = "123..455"
def (String min, String max) = rangeString.tokenize("..")
def range = min.toInteger()..max.toInteger()On Wed, Apr 20, 2016 at 7:26 AM, Gerald Wiltse <email@example.com> wrote:I don't see how that works in my case, maybe i'm missing something.I will clarify:I define a variable in web to represent the range: 14502..14520The web converts this to a string, and passes it into my code.My code then has to receive this string, and then construct a list from it.I could do:String rangeString = passedInVar(String min, String max) = rangeString.tokenize("..")Range range = min..maxBut i was hoping for a universal "caster" loop which can detect and cast the common types from strings:Integers, lists, ranges, maps, booleans..12345["this", "is", "Sample", "List"]14502..14520["key":"value","for":"maps"]trueI think eval works for all but ranges.On Wed, Apr 20, 2016 at 1:15 AM, Guillaume Laforge <firstname.lastname@example.org> wrote:Hi,You can just replace the bounds with variables.def a = 1def b = 10def r = a..bIsn't that what you're looking for?Guillaume--
Le mercredi 20 avril 2016, Gerald Wiltse <email@example.com> a écrit :I can find no examples of different ways to create a range. There's a plethora of examples on what you can do when you start by creating a range like so: "1..10"But, how does one create a range when the min and max values are stored in variables? There's no range constructor. I see that it's a form of a list, but I see no helper methods for dynamically creating ranges given a min and max value.I even tried to get really fancy, but this evaluates to a string.def v = "10..15"
assert Eval.x(v, "return x").getClass().name == "java.lang.String"My use case is this. I populate a bunch of form fields with variable definitions... but they all get passed to my code as strings. But I want to pass port ranges and lists and maps. So, the Eval() method is exactly what I needed.. it just isn't working for ranges.Regards,