for ( boldParam in [para1, para2, para2, para4, para5] ) {
if(/* boldParam exists in params */)
ilike(boldParam,'%' + params.boldParam + '%')
}
}
I would like to write something like above. I'm trying to avoid the following multiple if statements:
if (params.para1)
ilike('para1','%' + params.para1+ '%')
if (params.para2)
ilike('para2','%' +params.para2+ '%')
if (params.para3)
ilike('para3','%' + params.para3+ '%')
params
is a Map
, so you can use containsKey()
:
for (boldParam in [para1, para2, para2, para4, para5]) {
if (params.containsKey(boldParam)) {
ilike(boldParam, '%' + params.boldParam + '%')
}
}
Trying to retrieve property that does not exist will give you null
. That means you can simply do
if (!params.missing) {
println("missing parameter is not present within request.")
}
In different use case you could also give a try to safe dereference operator ?.
. For example:
params.email?.encodeAsHTML()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With