我想用Groovy的ConfigSlurper解析闭包的数据结构(在本例中是一个配置文件)。解析结果将存储在与数据结构类似的对象层次结构中。这种数据结构唯一的特别之处在于,一些闭包名称在一个闭包中重复出现,例如apple和green。我看起来像是ConfigSlurper内部使用了一个Map来替换已经存在的值。我想知道ConfigSlurper是否真的能够处理这种数据结构。我使用的Groovy版本是1.7.10。或者,我尝试使用GroovyShell来执行闭包,但是在执行顺序上有一些问题。
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发布于 2011-12-08 23:26:57
我能够使用GroovyShell解析这些规则。规则的每个闭包都需要执行。传入的Binding对象允许您检索值。对于重复的闭包,我必须将闭包的resolveStrategy更改为Closure.DELEGATE_FIRST。
https://stackoverflow.com/questions/8394763
复制相似问题