Skip to content

Commit 98c1292

Browse files
committed
add support to convert Map[String, Seq[String]] to data map
1 parent 9e07b8c commit 98c1292

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

integrations/playframework/SampleController.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import play.api.mvc._
22
import com.github.tminglei.bind.simple._
33

44
object SampleController extends Controller with MyFormBindSupport {
5+
///--
6+
import scala.collection.convert.WrapAsScala._
7+
def parameterMap(request: HttpServletRequest) = request.getParameterMap.map { case (key, values) => (key, values.toSeq) }.toMap
8+
///--
59

610
def find() = Action { implicit request =>
711
val mappings = tmapping(
812
"cond" -> text()
913
)
10-
binder.bind(mappings, params).fold(
14+
binder.bind(mappings, data(parameterMap(request))).fold(
1115
errors => status(400, errors),
1216
{ case (cond) =>
1317
ok(repos.features.find(cond))

integrations/scalatra/SampleServlet.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class SampleServlet extends ScalatraServlet with MyFormBindSupport {
77
val mappings = tmapping(
88
"id" -> long()
99
)
10-
binder.bind(mappings, params).fold(
10+
binder.bind(mappings, data(multiParams)).fold(
1111
errors => holt(400, errors),
1212
{ case (id) =>
1313
Ok(toJson(repos.features.get(id)))

src/main/scala/com/github/tminglei/bind/package.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,19 @@ package object bind {
2929
object simple extends Mappings with Constraints with Processors {
3030
type FormBinder[R] = com.github.tminglei.bind.FormBinder[R]
3131
val FormBinder = com.github.tminglei.bind.FormBinder
32+
33+
///--
34+
def data(params: Map[String, Seq[String]]): Map[String, String] = {
35+
params.map { case (key, values) =>
36+
if (values == null || values.length == 0) Nil
37+
else if (values.length == 1 && ! key.endsWith("[]")) Seq((key, values(0)))
38+
else {
39+
for(i <- 0 until values.length) yield {
40+
val cleanKey = key.replaceAll("\\[\\]$", "")
41+
(s"$cleanKey[$i]", values(i))
42+
}
43+
}
44+
}.flatten.toMap
45+
}
3246
}
3347
}

0 commit comments

Comments
 (0)