Sam Taylor
Sam Taylor

Reputation: 345

Grails - access data within Config.groovy whilst using variable in path

I know that to access a variable from within the Config.groovy file,

grailsApplication.config.someValue.anotherValue

I want to use a variable within this path because I want to get a URL from the config file. The value foo is passed in as a parameter to the method which will be called a number of times using different variables based on other factors.

def foo = "anothervalue"
grailsApplication.config.someValue.${ foo }.

The actual path to the value I want in the config stays the same as in the first instance.

I have tried:

grailsApplication.config.someValue.${ foo }
grailsApplication.config.someValue${ foo }
grailsApplication.config.someValue + "${ foo }"
grailsApplication.config.someValue + ".${ foo }"
grailsApplication.config.someValue + ${ foo }

Upvotes: 2

Views: 313

Answers (2)

albciff
albciff

Reputation: 18507

grailsApplication.config.someValue."${ foo }" must works.

grailsApplication.config returns a groovy.util.ConfigObject like groovy.util.ConfigSlurper.parse() so you can see how it works in the follow example:

import groovy.util.ConfigSlurper

def configTxt = '''
prop1 {
    prop2 {
        person.name = 'paco'
    }
}
'''

def config = new ConfigSlurper().parse(configTxt)
def foo = "prop2"
println config.prop1."${foo}" // prints [person:[name:paco]]

Hope this helps,

Upvotes: 2

injecteer
injecteer

Reputation: 20699

more natural would be grailsApplication.config.someValue[ foo ]

Upvotes: 1

Related Questions