Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple groovy operator question: Math.min(params.max ? params.int('max') : 10, 100)

Tags:

grails

groovy

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

like image 533
Ray Avatar asked Jul 31 '11 15:07

Ray


2 Answers

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.

like image 60
Tomasz Nurkiewicz Avatar answered Sep 28 '22 03:09

Tomasz Nurkiewicz


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.

like image 29
cHao Avatar answered Sep 28 '22 03:09

cHao