tags:

views:

85

answers:

2

I have a dynamic config parameter I want to get like:

    String srcProperty = "${attrs ['src']}.audio" + ((attrs['locale'])? "_${attrs['locale']}" : '')

    assert srcProperty == "prompt.welcomeMessageOverrideGreeting.audio"

where my config has:

prompt{
    welcomeMessageOverrideGreeting {
        audio = "/en/someFileName.wav"
        txt = "Text alternative for /en/someFileName.wav"
        audio_es = "/es/promptFileName.wav"
        txt_es = "Texto alternativo para /es/someFileName.wav"
    }
}

While this works fine:

String audio = "${config.prompt.welcomeMessageOverrideGreeting.audio}"

and: assert "${config.prompt.welcomeMessageOverrideGreeting.audio}" == "/en/someFileName.wav"

I can not get this to work:

String audio = config.getProperty("prompt.welcomeMessageOverrideGreeting.audio")
A: 

They're not stored flat like that, they're stored hierarchically. "config.prompt.welcomeMessageOverrideGreeting.audio" is a shorthand to get "prompt" from config, then "welcomeMessageOverrideGreeting" from that, then "audio" from that. If you want to use dot notation just flatten the config:

String audio = config.flatten().getProperty("prompt.welcomeMessageOverrideGreeting.audio")
Burt Beckwith
A: 

SOLVED: This was REALLY tough, but here is what I found that worked to get a dynamic property:

String audio = srcProperty.tokenize( '.' ).inject( config ) { cfg, pr -> cfg[ pr ] }

I blogged about it: http://www.baselogic.com/blog/development/grails-groovy-development/configslurper-with-dynamic-property-name-from-configurationholder-config-object

Mick Knutson