首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用另一个闭包扩展闭包

用另一个闭包扩展闭包
EN

Stack Overflow用户
提问于 2018-01-18 18:28:54
回答 1查看 559关注 0票数 2

我正在尝试使用ConfigSlurper来解析将用作Java程序配置文件的Groovy文件。

我希望支持具有基本属性的层次化配置,如果需要,可以重写这些属性。我认为闭包是一个很好的表示方法,但我遇到了一个小问题。

通过将基闭包左移到子闭包上,我可以获得要传播到子配置的基键:

代码语言:javascript
复制
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时,我得到了我所希望的:

代码语言:javascript
复制
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闭包中的基键时,基闭包中的基键似乎是优先的,这不是我所期望的。这是消息来源:

代码语言:javascript
复制
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

代码语言:javascript
复制
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'
}

我试着把换班改为右班,但一直有个错误:

代码语言:javascript
复制
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()实现了我的目标

代码语言:javascript
复制
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"
}

这正好产生了我预期的结果:

代码语言:javascript
复制
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不是最好的选择?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-18 19:21:16

这是你想要的吗?

输入:

代码语言:javascript
复制
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"
}

输出:

代码语言:javascript
复制
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'
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48328018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档