【发布时间】:2018-01-18 18:28:54
【问题描述】:
我正在尝试使用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 定义之前的整个configN = base.clone() 似乎有点笨拙。有什么办法可以稍微清理一下吗?或者也许使用 groovy 不是最好的选择?
【问题讨论】:
-
“有什么办法可以稍微清理一下吗?” - 为什么不在
config1 << base之前填充config1? -
是的,哈哈,我不敢相信我没有想到这一点。
标签: groovy