【发布时间】:2019-05-04 20:34:20
【问题描述】:
我有以下地图:
configs = [
common : [
foo : '123',
bar : '456'
],
dev : [
foo : '789',
bar : '012'
],
test : null
]
当我将dev 添加到common 时,效果很好 - 来自common 的值被来自dev 的值覆盖。正是我想要的。
dev = configs['common'] + configs['dev']
println dev
// --> [foo:789, bar:012]
但是,如果我尝试对 test 进行相同操作,则会收到以下错误:
groovy.lang.GroovyRuntimeException:方法 java.util.LinkedHashMap#plus 的方法重载不明确。 由于以下之间的原型重叠,无法解析为 [null] 调用哪个方法: [接口 java.util.Collection] [接口java.util.Map]
我可以通过执行以下操作来使其工作:
test = [:]
test = configs['common']==null ? test : test + configs['common'] // First add common bits
test = configs['test']==null ? test : test + configs['test'] // Then override with environment specific bits
println test
// --> [foo:123, bar:456]
但这看起来又丑又臃肿。
有更好的 Groovy-fu 的人可以告诉我一个更好的方法吗?谢谢!
【问题讨论】:
标签: dictionary groovy idioms