Groovy:评估一个带有变量的属性

时间:2013-05-15 04:11:08

标签: groovy

对于groovy有点新,我试图将变量字符串与使用ConfigSlurper从文件中提取的属性进行匹配。我有一个slurper部分工作正常,但似乎无法找出正确的方法来评估一个变量的属性。当我找到evaluating-code-dynamically-in-groovy时,我觉得自己变得温暖,但我并不完全确定。

//properties.groovy
jobs {
  foo {
    email="foo@email.com"
  }
}

//myscript.groovy
def config = new ConfigSlurper().parse(new File('properties.groovy').toURI().toURL())

List jobs = (ArrayList) BazAPI.getArtifacts(bucket) // list of objects, foo is one
ListIterator jobIterator = jobs.listIterator();

while (jobIterator.hasNext()) {
    Object j = jobIterator.next();
    job_name = "${j.name}" //
    email = config.jobs."${job_name}".email /* NEED TO FIGURE OUT HOW TO EVAL */
    foo_email = config.jobs.foo.email //evaluates to the correct property in properties.groovy

    //these values get fed to a DSL but to illustrate
    println "${job_name}"    // prints foo
    println "${email}"       // prints [:]
    println "${foo_email}"   // prints foo@email.com
} 

1 个答案:

答案 0 :(得分:1)

你试过吗

config.jobs[ j.name ].email