Soumya Simanta
Soumya Simanta

Reputation: 11751

Route URL Pattern for a method signature that takes an optional list of Strings

In my Play 2 controller (Scala) I've a method that looks like the following.

start(id:String, keywords:Option[List[String]]) 

Basically I want to get pass a list of string as keywords where it's optional.

The following doesn't work and gives me a compile error.

GET        /start         start(id:String,options:Option[List[String]])

The error makes sense because even if this route compiled I'm not sure how I would pass a list of Strings in my GET URL.

I'm looking for suggestions to resolve this.

Upvotes: 1

Views: 240

Answers (1)

Michael Zajac
Michael Zajac

Reputation: 55569

Since you're just using keywords, how about comma-separated values in the query string?

GET     /start/:id                    controllers.Sample.start(id: String, options: Option[String])

/start/1233?options=key,word,test

Then in your controller convert to Option[List[String]]:

def start(id: String, options: Option[String]) = Action {
    val opts: Option[List[String]] = options.map(_.split(',').filter(_.nonEmpty))

    ...
}

Upvotes: 3

Related Questions