Groovy 用另一个闭包扩展闭包
我正在尝试使用Groovy 用另一个闭包扩展闭包,groovy,Groovy,我正在尝试使用ConfigSlurper解析Groovy文件,这些文件将用作Java程序的配置文件 我希望支持具有基本属性的分层配置,如果需要,可以覆盖这些基本属性。我认为闭包是一种很好的表示方法,但我遇到了一个小问题 通过将基本闭包左移到子闭包上,我可以将基本密钥传播到子配置: base { baseKey = "All closures that compose from this closure will have this key" } config1 { config
ConfigSlurper
解析Groovy文件,这些文件将用作Java程序的配置文件
我希望支持具有基本属性的分层配置,如果需要,可以覆盖这些基本属性。我认为闭包是一种很好的表示方法,但我遇到了一个小问题
通过将基本闭包左移到子闭包上,我可以将基本密钥传播到子配置:
base {
baseKey = "All closures that compose from this closure will have this key"
}
config1 {
config1only = "value"
}
config1 << base
config2 {
config2only = "another value"
}
config2 << base
太好了!但是,当我试图覆盖一个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
我试着换到右班,但我总是出错:
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 {
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不是最好的选择吗?这就是您想要的吗
输入:
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"
}
“有什么方法可以让我稍微整理一下吗?”-为什么不干脆config1是的,哈哈,我真不敢相信我没有想到这一点。
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'
}
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'
}