ConfigSlurper with dynamic property name from ConfigurationHolder config Object

I spent the better part of my day trying to solve an issue where I construct a named configuration property, then I wanted to get that property from the config object.

Seemed simple enough… NOT FOR ME. Maybe it was just a Friday or something but…. Here is what I found

I was able to assert the correct values in many different ways except the one I needed

        def config = ConfigurationHolder.config
        String srcProperty = "${tagBundleName}.audio" + ((locale)? "_${locale}" : '')

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

I must have spent 4 hours on this and never found any answer until I ran across the closest thing to what I needed:

http://groovyconsole.appspot.com/script/102001

And this worked for trying to eval that property:

assert Eval.me( "config", config, "config.${txtProperty}" ) == "Text alternative for /en/someFileName.wav"

But I needed the value, not just an assertion and this did not work:

String audio = "config.${prop}"

Or something like it.

Then, I ran across the original posting that was made to get to his findings:
http://groups.google.com.sg/group/groovy-user/browse_thread/thread/61223560e97bf99c

println prop.tokenize( '.' ).inject( config ) { cfg, pr -> cfg[ pr ] } 

Then was finally able to close my very long week:

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

I am so glad this is solved!

VN:F [1.9.1_1087]
Rating: 5.3/10 (4 votes cast)
VN:F [1.9.1_1087]
Rating: +1 (from 3 votes)
ConfigSlurper with dynamic property name from ConfigurationHolder config Object, 5.3 out of 10 based on 4 ratings
  • Share/Bookmark

This entry was posted on Friday, April 16th, 2010 at 2:16 pm and is filed under Grails-Groovy. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “ConfigSlurper with dynamic property name from ConfigurationHolder config Object”

  1. Tweets that mention ConfigSlurper with dynamic property name from ConfigurationHolder config Object | BASE Logic, Inc. -- Topsy.com Says:

    [...] This post was mentioned on Twitter by Mick Knutson. Mick Knutson said: ConfigSlurper with dynamic property name from ConfigurationHolder config Object: I spent the better part of my day… http://bit.ly/b0D2e4 [...]

Leave a Reply

You must be logged in to post a comment.