Can you tell me how the expression
Math.min(params.max ? params.int('max') : 10, 100)
works? It doesn't fit the groovy ternary if, so what special operator is this?
Thanks
Is it clear now?
def max = params.max? params.int('max') : 10
Math.min(max, 100)
BTW this is a nice idiom popular in Grails - if the parameter max
exists, read it, but if it exceeds given value (100
by default), truncate it to 100
. This way an attacker or malignant user won't make your application to return arbitrary big amount of data from the database.
Perhaps breaking it up into two expressions would help. params.max ? params.int('max') : 10
is the ternary expression...the result of which ends up being the first arg to Math.min
(with 100
being the other arg).
Looks like the end result is an integer that's limited to being at most 100, and defaults to 10.
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