简体   繁体   中英

How to pass variable name while reading from Groovy Config file?

I am new to groovy and here is my question

def config = new ConfigSlurper().
parse(new File('RegionConfig.groovy').toURI().toURL())

Now i need something like

for(String name : listOfNames){
println(config.name)
}

How can i achieve this ?

Like this?

config.groovy:

user.name='koji'
user.nation='japan'
a.b.c='foo'

test code:

def config = new ConfigSlurper().parse(new File('config.groovy').toURL())
assert ['user.name', 'user.nation', 'a.b.c'] == config.flatten().keySet().collect {it as String}

also you can write like follows:

for (String name: config.flatten().keySet()) {
    println name
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM