=htc #18518 make it simpler to unmarshal csv values from params
This commit is contained in:
parent
e713591e5f
commit
c9adfcfbc7
5 changed files with 49 additions and 3 deletions
|
|
@ -7,8 +7,9 @@ package directives
|
|||
|
||||
import akka.http.scaladsl.model._
|
||||
import akka.http.scaladsl.server.Route
|
||||
import akka.http.scaladsl.unmarshalling.PredefinedFromStringUnmarshallers
|
||||
|
||||
class ParameterDirectivesExamplesSpec extends RoutingSpec {
|
||||
class ParameterDirectivesExamplesSpec extends RoutingSpec with PredefinedFromStringUnmarshallers {
|
||||
"example-1" in {
|
||||
val route =
|
||||
parameter('color) { color =>
|
||||
|
|
@ -180,4 +181,17 @@ class ParameterDirectivesExamplesSpec extends RoutingSpec {
|
|||
responseAs[String] shouldEqual "The parameters are x = '1', x = '2'"
|
||||
}
|
||||
}
|
||||
"csv" in {
|
||||
val route =
|
||||
parameter("names".as(CsvString)) { names =>
|
||||
complete(s"The parameters are ${names.mkString(", ")}")
|
||||
}
|
||||
|
||||
Get("/?names=Caplin") ~> route ~> check {
|
||||
responseAs[String] shouldEqual "The parameters are Caplin"
|
||||
}
|
||||
Get("/?names=Caplin,John") ~> route ~> check {
|
||||
responseAs[String] shouldEqual "The parameters are Caplin, John"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -94,6 +94,12 @@ Repeated parameter
|
|||
.. includecode2:: ../../../../code/docs/http/scaladsl/server/directives/ParameterDirectivesExamplesSpec.scala
|
||||
:snippet: repeated
|
||||
|
||||
Csv valued parameter
|
||||
++++++++++++++++++++
|
||||
|
||||
.. includecode2:: ../../../../code/docs/http/scaladsl/server/directives/ParameterDirectivesExamplesSpec.scala
|
||||
:snippet: csv
|
||||
|
||||
Repeated, deserialized parameter
|
||||
++++++++++++++++++++++++++++++++
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ package directives
|
|||
|
||||
import org.scalatest.{ FreeSpec, Inside }
|
||||
import akka.http.scaladsl.unmarshalling.Unmarshaller.HexInt
|
||||
import akka.http.scaladsl.unmarshalling.Unmarshaller.CsvString
|
||||
|
||||
class ParameterDirectivesSpec extends FreeSpec with GenericRoutingSpec with Inside {
|
||||
"when used with 'as[Int]' the parameter directive should" - {
|
||||
|
|
@ -52,6 +53,24 @@ class ParameterDirectivesSpec extends FreeSpec with GenericRoutingSpec with Insi
|
|||
}
|
||||
}
|
||||
|
||||
"when used with 'as(CsvString)' the parameter directive should" - {
|
||||
val route =
|
||||
parameter("names".as(CsvString)) { names ⇒
|
||||
complete(s"The parameters are ${names.mkString(", ")}")
|
||||
}
|
||||
|
||||
"extract a single name" in {
|
||||
Get("/?names=Caplin") ~> route ~> check {
|
||||
responseAs[String] shouldEqual "The parameters are Caplin"
|
||||
}
|
||||
}
|
||||
"extract a number of names" in {
|
||||
Get("/?names=Caplin,John") ~> route ~> check {
|
||||
responseAs[String] shouldEqual "The parameters are Caplin, John"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
"when used with 'as(HexInt)' the parameter directive should" - {
|
||||
"extract parameter values as Int" in {
|
||||
Get("/?amount=1f") ~> {
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ package akka.http.scaladsl.common
|
|||
import akka.http.scaladsl.unmarshalling.{ FromStringUnmarshaller ⇒ FSU }
|
||||
|
||||
private[http] trait ToNameReceptacleEnhancements {
|
||||
implicit def symbol2NR(symbol: Symbol) = new NameReceptacle[String](symbol.name)
|
||||
implicit def string2NR(string: String) = new NameReceptacle[String](string)
|
||||
implicit def symbol2NR(symbol: Symbol): NameReceptacle[String] = new NameReceptacle[String](symbol.name)
|
||||
implicit def string2NR(string: String): NameReceptacle[String] = new NameReceptacle[String](string)
|
||||
}
|
||||
object ToNameReceptacleEnhancements extends ToNameReceptacleEnhancements
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
package akka.http.scaladsl.unmarshalling
|
||||
|
||||
import scala.collection.immutable
|
||||
|
||||
trait PredefinedFromStringUnmarshallers {
|
||||
|
||||
implicit val byteFromStringUnmarshaller: Unmarshaller[String, Byte] =
|
||||
|
|
@ -76,6 +78,11 @@ trait PredefinedFromStringUnmarshallers {
|
|||
}
|
||||
}
|
||||
|
||||
val CsvString: Unmarshaller[String, immutable.Seq[String]] =
|
||||
Unmarshaller.strict[String, immutable.Seq[String]] { string ⇒
|
||||
string.split(",").toList
|
||||
}
|
||||
|
||||
private def numberFormatError(value: String, target: String): PartialFunction[Throwable, Nothing] = {
|
||||
case e: NumberFormatException ⇒
|
||||
throw if (value.isEmpty) Unmarshaller.NoContentException else new IllegalArgumentException(s"'$value' is not a valid $target value", e)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue