I would like to parse a data structure of closures (in this case a configuration file) with Groovy’s ConfigSlurper. The parsing result will be stored in an object hierarchy similar to the data structure. The only thing special about this data structure is the fact that some closure names repeat themselves within one closure e.g. apple and green. I looks like ConfigSlurper is using a Map internally that replaces already existing values. I was wondering if ConfigSlurper is actually able to handle these kinds of data structures. The Groovy version I use is 1.7.10. Alternatively, I tried to use GroovyShell to execute the closures but had some issues with the execution order.
String rules = """
fruits {
apples {
apple {
id = 11
colors {
green {
name = 'test1'
}
green {
name = 'test2'
}
}
}
apple {
id = 12
colors {
green {
name = 'test3'
}
green {
name = 'test4'
}
}
}
}
}
"""
ConfigSlurper configSlurper = new ConfigSlurper()
def config = configSlurper.parse(rules)
println config
I was able to parse these rules using
GroovyShell. Each closure of the rules needs to executed. The passed inBindingobject lets you retrieve the values. For the repetitive closures I had to change the closure’sresolveStrategytoClosure.DELEGATE_FIRST.