Note that there are some explanatory texts on larger screens.

plurals
  1. POScala: parsing an API parameter
    text
    copied!<p>My API currently take an optional parameter named gamedate. It is passed in as a string at which time I later parse it to a Date object using some utility code. The code looks like this:</p> <pre><code> val gdate:Option[String] = params.get("gamedate") val res = gdate match { case Some(s) =&gt; { val date:Option[DateTime] = gdate map { MyDateTime.parseDate _ } val dateOrDefault:DateTime = date.getOrElse((new DateTime).withTime(0, 0, 0, 0)) NBAScoreboard.findByDate(dateOrDefault) } case None =&gt; NBAScoreboard.getToday } </code></pre> <p>This works just fine. Now what I'm trying to solve is I'm allowing multiple gamedates get passed in via a comma delimited list. Originally you can pass a parameter like this:</p> <p><code>gamedate=20131211</code></p> <p>now I want to allow that OR:</p> <p><code>gamedate=20131211,20131212</code></p> <p>That requires modifying the code above to try to split the comma delimited string and parse each value into a Date and change the interface to findByDate to accept a Seq[DateTime] vs just DateTime. I tried running something like this, but apparently it's not the way to go about it:</p> <pre><code>val res = gdates match { case Some(s) =&gt; { val dates:Option[Seq[DateTime]] = gdates map { _.split(",").distinct.map(MyDateTime.parseDate _ )} val datesOrDefault:Seq[DateTime] = dates map { _.getOrElse((new DateTime).withTime(0, 0, 0, 0))} NBAScoreboard.findByDates(datesOrDefault) } case None =&gt; NBAScoreboard.getToday } </code></pre> <p>What's the best way to convert my first set of code to handle this use case? I'm probably fairly close in the second code example I provided, but I'm just not hitting it right.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload