Conditionally take out elements on Groovy Closure -


i using groovy library call ws-lite web service testing. way works takes closure , generate xml , send web service end point.

see below simple example of closure looks like:

def bookxml = {     books {         book(available: "20", id: "1") {             title("don xijote")             author(id: "1", "manuel de cervantes")         }         book(available: "14", id: "2") {             title("catcher in rye")             author(id: "2", "jd salinger")         }         book(available: "13", id: "3") {             title("alice in wonderland")             author(id: "3", "lewis carroll")         }     } } 

will generate xml in request below:

<?xml version="1.0" encoding="utf-8"?> <books>    <book available="20" id="1">       <title>don xijote</title>       <author id="1">manuel de cervantes</author>    </book>    <book available="14" id="2">       <title>catcher in rye</title>       <author id="2">jd salinger</author>    </book>    <book available="13" id="3">       <title>alice in wonderland</title>       <author id="3">lewis carroll</author>    </book> </books> 

in order make clients more flexible, pass data structure test client map:

def bookmap = [     books: [[                     id       : "1",                     available: "20",                     title    : "don xijote",                     author   : [                             id  : "1",                             name: "manuel de cervantes"                     ]             ], [                     id       : "2",                     available: "14",                     title    : "catcher in rye",                     author   : [                             id  : "2",                             name: "jd salinger"                     ]             ], [                     id       : "3",                     available: "13",                     title    : "alice in wonderland",                     author   : [                             id  : "3",                             name: "lewis carroll"                     ]             ]     ] 

]

this how client looks now:

def bookxml = {     books {         bookmap.books.book.each {             book(available: it.available, id: it.id) {                 title(it.available.title)                 author(id: it.author.id, it.author.name)             }         }     } } 

one thing want in bookxml closure, there way can take out tag, if value in data structure null?

for example, if title of first book null in map, in closure, won't create tag title book one.

i know how can done in groovy collection using collectentries map , collect list, don't know transforming closure.

can please share insight me?

thanks.

i not have knowledge of builders, seems question how ignore keys null values in map.

this can achieved using each() method two-arg closure. 2 arguments passed closure in case each entry's key , value.

to demonstrate -

def book = [     id       : "1",     available: "20",     title    : null ]  book.each {key, value->     if (value) {         println "$key->$value"     } } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -