3

I've done some research but I haven't found a working code for my case. I have two variables named test and test2 and I want to put them in a map in the format [test:valueof(test), test2:valueof(test2)]

My piece of code is the following:

def test="HELLO"
def test2="WORLD"
def queryText = "\$\$test\$\$ \$\$test2\$\$ this is my test"

def list = queryText.findAll(/\$\$(.*?)\$\$/)

def map = [:]
list.each{
    it = it.replace("\$\$", "")
    map.putAt(it, it)
}

queryText = queryText.replaceAll(/\$\$(.*?)\$\$/) { k -> map[k[1]] ?: k[0] }

System.out.println(map)
System.out.println(queryText)

Output:

output

Desired output:

"HELLO WORLD this is my test"

I think I need something like map.putAt(it, eval(it))

EDIT

This is the way I get my inputs. the code goes into the 'test' script

The ones on the right are the variable names inside the script, the left column are the values (that will later be dynamic)

enter image description here enter image description here

3
  • GiLA3, have you got chance to check the solution? Commented Apr 20, 2017 at 9:13
  • What is the data in RTFTemplates and Message1? Is your current groovy script in test? Commented Apr 20, 2017 at 12:17
  • Data in RTF Templates is irrelevant, the query attribute is a String with $$placeholders$$, message1 is just an empty box where I will return the result queryText processed by the script. Yes the script is in test Commented Apr 20, 2017 at 12:19

3 Answers 3

4

You are almost there.
The solution is instead of putting the values into separate variables put them into the script binding.

Add this at the beginning ( remove the variables test and test2):

def test="HELLO"
def test2="WORLD"
binding.setProperty('test', test)     
binding.setProperty('test2', test2)  

and change this:

{ k -> map[k[1]] ?: k[0] }

to this:

{ k ->  evaluate(k[1]) }
Sign up to request clarification or add additional context in comments.

4 Comments

Variables are already set prior to this piece of code, the def part was to make an example, my variables are retrieved as follows: #input String queryText,test,test2 since I'm using WebRatio. test already contains "HELLO" and test2 already contains "WORLD", same goes for queryText, so is there a way to adapt the binding.setProperty to my situation? Something like binding.setProperty('test',test) maybe?
{ k -> evaluate(k[1]) } was all I needed! binding is not needed in my case, even if I don't really understand why. Thanks a lot!
May be you should accept @dsharew 's answer instead.
I did it now, is it okay? :)
1

It should be very simple if you could use TemplateEngine.

def text = '$test $test2 this is my test'
def binding = [test:'HELLO', test2:'WORLD']
def engine = new groovy.text.SimpleTemplateEngine() 
def template = engine.createTemplate(text).make(binding)
def result = 'HELLO WORLD this is my test'
assert result == template.toString()

You can test quickly online Demo

3 Comments

In my requirement I must use my variable names instead of 'HELLO' and 'WORLD' since those values change on every execution of the code. I need something like def binding = [test:valueof(test), test2:valueof(test2)]
What is the exact input that you receive? some where there should be mapping / binding of variable and its value, right?
Edited my question with the information you requested :)
1

Final working code, thanks to all, in particular to dsharew who helped me a lot!

#input String queryText,test,test2,test3

def list = queryText.findAll(/\$\$(.*?)\$\$/)

def map = [:]
list.each{
    it = it.replace("\$\$", "")
    map.putAt(it, it)
}

queryText = queryText.replaceAll(/\$\$(.*?)\$\$/) { k ->  evaluate(k[1]) }

return queryText

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.