+htp extract multiple occurrences in the parameters/formFields directives by suffixing with .*

This commit is contained in:
Alek Storm 2015-03-30 10:33:46 -07:00
parent 632868b868
commit d3742c577a
9 changed files with 171 additions and 21 deletions

View file

@ -95,6 +95,50 @@ class ParameterDirectivesExamplesSpec extends RoutingSpec {
responseAs[String] shouldEqual "The query parameter 'count' was malformed:\n'blub' is not a valid 32-bit signed integer value"
}
}
"repeated" in {
val route =
parameters('color, 'city.*) { (color, cities) =>
cities.toList match {
case Nil => complete(s"The color is '$color' and there are no cities.")
case city :: Nil => complete(s"The color is '$color' and the city is $city.")
case multiple => complete(s"The color is '$color' and the cities are ${multiple.mkString(", ")}.")
}
}
Get("/?color=blue") ~> route ~> check {
responseAs[String] === "The color is 'blue' and there are no cities."
}
Get("/?color=blue&city=Chicago") ~> Route.seal(route) ~> check {
responseAs[String] === "The color is 'blue' and the city is Chicago."
}
Get("/?color=blue&city=Chicago&city=Boston") ~> Route.seal(route) ~> check {
responseAs[String] === "The color is 'blue' and the cities are Chicago, Boston."
}
}
"mapped-repeated" in {
val route =
parameters('color, 'distance.as[Int].*) { (color, cities) =>
cities.toList match {
case Nil => complete(s"The color is '$color' and there are no distances.")
case distance :: Nil => complete(s"The color is '$color' and the distance is $distance.")
case multiple => complete(s"The color is '$color' and the distances are ${multiple.mkString(", ")}.")
}
}
Get("/?color=blue") ~> route ~> check {
responseAs[String] === "The color is 'blue' and there are no distances."
}
Get("/?color=blue&distance=5") ~> Route.seal(route) ~> check {
responseAs[String] === "The color is 'blue' and the distance is 5."
}
Get("/?color=blue&distance=5&distance=14") ~> Route.seal(route) ~> check {
responseAs[String] === "The color is 'blue' and the distances are 5, 14."
}
}
"parameterMap" in {
val route =
parameterMap { params =>

View file

@ -25,7 +25,7 @@ Description
Form fields can be either extracted as a String or can be converted to another type. The parameter name
can be supplied either as a String or as a Symbol. Form field extraction can be modified to mark a field
as required or optional or to filter requests where a form field has a certain value:
as required, optional, or repeated, or to filter requests where a form field has a certain value:
``"color"``
extract value of field "color" as ``String``
@ -40,6 +40,13 @@ as required or optional or to filter requests where a form field has a certain v
(see also :ref:`http-unmarshalling-scala`)
``"amount".as(deserializer)``
extract value of field "amount" with an explicit ``Deserializer``
``"distance".*``
extract multiple occurrences of field "distance" as ``Iterable[String]``
``"distance".as[Int].*``
extract multiple occurrences of field "distance" as ``Iterable[Int]``, you need a matching implicit ``Deserializer`` in scope for that to work
(see also :ref:`unmarshalling`)
``"distance".as(deserializer).*``
extract multiple occurrences of field "distance" with an explicit ``Deserializer``
You can use :ref:`Case Class Extraction` to group several extracted values together into a case-class
instance.

View file

@ -25,7 +25,7 @@ to use which shows properties of different parameter directives.
directive level ordering multi
========================== ====== ======== =====
:ref:`-parameter-` high no no
:ref:`-parameters-` high no no
:ref:`-parameters-` high no yes
:ref:`-parameterMap-` low no no
:ref:`-parameterMultiMap-` low no yes
:ref:`-parameterSeq-` low yes yes

View file

@ -24,7 +24,7 @@ Description
-----------
Query parameters can be either extracted as a String or can be converted to another type. The parameter name
can be supplied either as a String or as a Symbol. Parameter extraction can be modified to mark a query parameter
as required or optional or to filter requests where a parameter has a certain value:
as required, optional, or repeated, or to filter requests where a parameter has a certain value:
``"color"``
extract value of parameter "color" as ``String``
@ -39,6 +39,13 @@ as required or optional or to filter requests where a parameter has a certain va
(see also :ref:`http-unmarshalling-scala`)
``"amount".as(deserializer)``
extract value of parameter "amount" with an explicit ``Deserializer``
``"distance".*``
extract multiple occurrences of parameter "distance" as ``Iterable[String]``
``"distance".as[Int].*``
extract multiple occurrences of parameter "distance" as ``Iterable[Int]``, you need a matching ``Deserializer`` in scope for that to work
(see also :ref:`unmarshalling`)
``"distance".as(deserializer).*``
extract multiple occurrences of parameter "distance" with an explicit ``Deserializer``
You can use :ref:`Case Class Extraction` to group several extracted values together into a case-class
instance.
@ -80,3 +87,15 @@ Deserialized parameter
... includecode2:: ../../../../code/docs/http/scaladsl/server/directives/ParameterDirectivesExamplesSpec.scala
:snippet: mapped-value
Repeated parameter
+++++++++++++++++++++++++++++
... includecode2:: ../../../../code/docs/http/scaladsl/server/directives/ParameterDirectivesExamplesSpec.scala
:snippet: repeated
Repeated, deserialized parameter
++++++++++++++++++++++
... includecode2:: ../../../../code/docs/http/scaladsl/server/directives/ParameterDirectivesExamplesSpec.scala
:snippet: mapped-repeated