Groovy web console

subscribe to the feed Subscribe
to this
site
eval-groovy-string-to-gstring (via #groovywebconsole)
tweet this snippet Tweet
this
script

eval-groovy-string-to-gstring

Published 1 month ago by marcello.desales@gmail.com with tags eval
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def vals = [ name: "marcello", zip: 2323]

def context = [
  user: vals,
  newVal: '${context.unknown}'
]

context.unknown = "Here I am"


def processValues(root) { 
  def engine = new groovy.text.SimpleTemplateEngine()
  root.each{         
    if (it.value instanceof Map) {             
      processValues(it.value)         
    } else if (it.value instanceof String && it.value.startsWith("\${") && it.value.endsWith("}") ) {
      println "Before replacement ${it.value}"
      it.value = engine.createTemplate(it.value).make([context:root]).toString()
      println "after ${it.value}"
    }     
   } 
}

println context

println processValues(context)