Steven
Steven

Reputation: 3894

Groovy properties/config with multiple words

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

Upvotes: 3

Views: 2279

Answers (1)

epidemian
epidemian

Reputation: 19219

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'

Upvotes: 3

Related Questions