title: groovy: Have a field name, need to set value and don't want to use switch
body: <p>I have an object with several fields, </p>
  
  <pre><code>class TestObj { 
    def field1
    def field2
  }
  </code></pre>
  
  <p>I have a pair of values v1="field1" and v2="value2"  I would like to set v2 into the appropriate field based on the name of v1, but I'd prefer not to have to do it with a switch or if statements,  I keep thinking there has to be a much "groovier" way of achieving the result other than doing something like this:</p>
  
  <pre><code>setValues(def fieldName, def fieldVal) {
    if (fieldName.equals("field1")) {
      field1 = fieldVal
    }
    if (fieldName.equals("field2")) {
      field2 = fieldVal
    }
  }
  </code></pre>
  
  <p>I've tried doing this:</p>
  
  <pre><code>setValues(def fieldName, def fieldVal) {
    this['${fieldName}'] = fieldVal
  }
  </code></pre>
  
  <p>However that fails, saying there's no property ${fieldName}</p>
  
  <p>Thanks.</p>