我正在尝试使用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不是最佳选择?
答案 0 :(得分:1)
这是你想要的吗?
输入:
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'
}