Groovy配置文件中的继承

我需要通过ConfigSlurper定义和读取Groovy配置文件中的几个属性,这些属性将共享一些公共字段并仅添加一个特定字段.像这样的东西:

config {
  // this is something like abstract property
  common {
    field1 = 'value1'
    field2 = 'value2'
  }

  property1 {
    // include fields from common here
    customField = 'prop1value'
  }

  property2 {
    // include fields from common here
    customField = 'prop2value'
  }
}

我很好奇是否有可能以某种方式实现这一目标.因为我对Groovy不是很熟悉,所以我目前的解决方案并不理想,我会说:

config {
  common {
    field1 = 'value1'
    field2 = 'value2'
  }

  property1 = common.clone()
  property1 {
    customField = 'value'
  }

  property2 = common.clone()
  property2 {
    customField = 'value'
  }
}
config.remove('common')

谢谢你的建议

解决方法

你可以做:

config {
    // A common map of values
    def common = [
        field1: 'value1',field2: 'value2'
    ] as ConfigObject

    property1 {
        customField = 'value'
    }

    property2 {
        customField = 'value'
    }

    property1.merge(common)
    property2.merge(common)
}

这是你的意思吗?

相关文章

背景:    8月29日,凌晨4点左右,某服务告警,其中一个...
https://support.smartbear.comeadyapi/docs/soapui/steps/g...
有几个选项可用于执行自定义JMeter脚本并扩展基线JMeter功能...
Scala和Java为静态语言,Groovy为动态语言Scala:函数式编程,...
出处:https://www.jianshu.com/p/ce6f8a1f66f4一、一些内部...
在运行groovy的junit方法时,报了这个错误:java.lang.Excep...