Search code examples
groovyclosuresgroovyws

Dynamically generated closure


Dynamically generated closure

I've written soap request in groovy wslite :

def request = {
        envelopeAttributes('xmlns:art': 'http://url')
        body {
            'art:validate' {
                item(itemValue)
            }
        }
    }

It's working fine, but now I have to change this to list, so at the end it will be something like that:

def request = {
        envelopeAttributes('xmlns:art': 'http://url')
        body {
            'art:validate' {
                item(itemValue)
            item(itemValue2)
            item(itemValue3)
            }
        }
    }

But have know Idea how I can dynamically create this request from List. I've even extracted this to variable:

def items = {
                item(itemValue)
            item(itemValue2)
            item(itemValue3)
}

but I don't know how to add new items to this closure. Is there any easy way ?


Solution

  • With your given items Closure, this may work:

    def request = {
        envelopeAttributes('xmlns:art': 'http://url')
        body {
            'art:validate' {
                items.delegate = delegate
                items()
            }
        }
    }
    

    if you need other things inside art:validate