我正在尝试使用ConfigSlurper来解析将用作Java程序配置文件的Groovy文件。
我希望支持具有基本属性的层次化配置,如果需要,可以重写这些属性。我认为闭包是一个很好的表示方法,但我遇到了一个小问题。
通过将基闭包左移到子闭包上,我可以获得要传播到子配置的基键:
base {
baseKey = "All closures that compose from this closure will have this key"
}
config1 {
config1only = "value"
}
config1 << base
config2 {
config2only = "another value"
}
config2 << base当我漂亮地打印ConfigObject时,我得到了我所希望的:
base.baseKey='All closures that compose from this closure will have this key'
config1 {
config1only='value'
baseKey='All closures that compose from this closure will have this key'
}
config2 {
config2only='another value'
baseKey='All closures that compose from this closure will have this key'
}太棒了!但是,当我试图覆盖一个config闭包中的基键时,基闭包中的基键似乎是优先的,这不是我所期望的。这是消息来源:
base {
baseKey = "All closures that compose from this closure will have this key"
overiddenKey = "base"
}
config1 {
config1only = "value"
overiddenKey = "override1"
}
config1 << base
config2 {
config2only = "another value"
overiddenKey = "override2"
}
config2 << base以下是印刷精美的ConfigObject:
base {
baseKey='All closures that compose from this closure will have this key'
overiddenKey='base'
}
config1 {
config1only='value'
overiddenKey='base'
baseKey='All closures that compose from this closure will have this key'
}
config2 {
config2only='another value'
overiddenKey='base'
baseKey='All closures that compose from this closure will have this key'
}我试着把换班改为右班,但一直有个错误:
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: groovy.util.ConfigObject.leftShift() is applicable for argument types: (script15162997138121381677545$_run_closure1) values: [script15162997138121381677545$_run_closure1@3d246ea3]
Possible solutions: leftShift(java.util.Map), leftShift(java.util.Map$Entry)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:54)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at script15162997138121381677545.run(script15162997138121381677545.groovy:18)这可能是因为我对groovy闭包的理解有限,所以希望能提供任何帮助。
更新:
所以看起来我已经通过使用base.clone()实现了我的目标
base {
baseKey = "All closures that compose from this closure will have this key"
overiddenKey = "base"
}
config1 = base.clone()
config1 {
config1only = "value"
}
config2 = base.clone()
config2 {
config2only = "another value"
overiddenKey = "override2"
}这正好产生了我预期的结果:
base {
baseKey='All closures that compose from this closure will have this key'
overiddenKey='base'
}
config1 {
baseKey='All closures that compose from this closure will have this key'
overiddenKey='base'
config1only='value'
}
config2 {
baseKey='All closures that compose from this closure will have this key'
overiddenKey='override2'
config2only='another value'
}但是,在configN = base.clone()定义之前的整个configN似乎有点笨拙。有什么办法我能把它弄干净一点吗?还是使用groovy不是最好的选择?
发布于 2018-01-18 19:21:16
这是你想要的吗?
输入:
base {
baseKey = "All closures that compose from this closure will have this key"
overiddenKey = "base"
}
config1 << base
config1 {
config1only = "value"
overiddenKey = "override1"
}
config2 << base
config2 {
config2only = "another value"
overiddenKey = "override2"
}输出:
base {
baseKey='All closures that compose from this closure will have this key'
overiddenKey='base'
}
config1 {
baseKey='All closures that compose from this closure will have this key'
overiddenKey='override1'
config1only='value'
}
config2 {
baseKey='All closures that compose from this closure will have this key'
overiddenKey='override2'
config2only='another value'
}https://stackoverflow.com/questions/48328018
复制相似问题