Quantcast

List creation question

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

List creation question

mjohnsonaz74
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?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: List creation question

Sébastien Blanc
You could use the params.list() feature :
if (params.capital_type){
capitalTypelist.addAll(params.list("capital_type")
}
Envoyé de mon iPhone

Le Jan 21, 2011 à 21:31, mjohnsonaz74 <[hidden email]> a écrit :

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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: List creation question

Hans Loeblich
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


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: List creation question

mjohnsonaz74
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  
Loading...