Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance in Groovy configuration file

I'd need to define and read by ConfigSlurper several properties from a Groovy configuration file which will share some common fields and add just one specific field. Something like this:

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'
  }
}

I'm curios whether it is possible to achieve this somehow in a nice way. As I'm not very familiar with Groovy so my current solution is not ideal I'd say:

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

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

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

Thanks for any advice

like image 814
Stan Svec Avatar asked Nov 08 '22 16:11

Stan Svec


1 Answers

You can do:

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)
}

Is that the sort of thing you mean?

like image 136
tim_yates Avatar answered Nov 15 '22 05:11

tim_yates