|
I created a service to return JSON to a client. The code works fine, but I'm trying to see if I can clean up some ugly code.
def capitalTypeList = []
if (params.capital_type) {
if (params.capital_type instanceof String) {
capitalTypeList << params.capital_type
} else {
capitalTypeList.addAll(params.capital_type)
}
}
In this code chunk, I create a list into which I will insert the values returned in the named parameter. The problem I run into is that a single param is of type String, whereas multiple params show up as a collection. As you can see from the code, I test for String and insert, otherwise I have to use an addAll method to add the collection. Does somebody know of an easier way to force single AND multiple value params into a list?
|
|
You could use the params.list() feature : if (params.capital_type){ capitalTypelist.addAll(params.list("capital_type") } Envoyé de mon iPhone
|
|
In reply to this post by mjohnsonaz74
How about this:
capitalTypeList += params.capital_type -Hans ----- Original Message ----- From: "mjohnsonaz74" <[hidden email]> To: [hidden email] Sent: Friday, January 21, 2011 2:31:22 PM GMT -06:00 US/Canada Central Subject: [grails-user] List creation question I created a service to return JSON to a client. The code works fine, but I'm trying to see if I can clean up some ugly code. def capitalTypeList = [] if (params.capital_type) { if (params.capital_type instanceof String) { capitalTypeList << params.capital_type } else { capitalTypeList.addAll(params.capital_type) } } In this code chunk, I create a list into which I will insert the values returned in the named parameter. The problem I run into is that a single param is of type String, whereas multiple params show up as a collection. As you can see from the code, I test for String and insert, otherwise I have to use an addAll method to add the collection. Does somebody know of an easier way to force single AND multiple value params into a list? View this message in context: List creation question Sent from the Grails - user mailing list archive at Nabble.com. --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by Sébastien Blanc
Sébastien,
Thank you! That was exactly what I was looking for. I was hoping there might be something simple like that available. MJ
|
| Powered by Nabble | Edit this page |
