Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy properties/config with multiple words

Tags:

groovy

I would like my groovy config to look something like:

plans {
    'Plan 1'='123'
    'Plan 2'='456'
}

How can I parse this with groovy so that I can access it similar to:

def config = new ConfigSlurper().parse(data)
assert config.plans.'Plan 1' == '123'
assert config.plans.'Plan 2' == '456'

Unfortunately I get the error:

[Plan 1] is a constant expression, but it should be a variable expression at line...

I'm not fixed on ConfigSlurper or the format of the data, but I would like to refer to each as Strings with multiple words and potentially special characters such as *, ^, etc. (thus causing potential regexp issues if regexp was used).

like image 563
Steven Avatar asked Jan 27 '26 16:01

Steven


1 Answers

You can assign those things in the config file if you use the "full" expressions instead of nesting the plans definitions:

plans.'Plan 1' = '123'
plans.'Plan 2' = '456'
plans.'Plan *' = '789'

Not very pretty, but then you can reference them:

def config = new ConfigSlurper().parse(data)
assert config.plans.'Plan 1' == '123'
assert config.plans.'Plan 2' == '456'
assert config.plans.'Plan *' == '789'
like image 151
epidemian Avatar answered Jan 29 '26 13:01

epidemian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!