/
PredefinedFromStringUnmarshallers.scala
executable file
·77 lines (58 loc) · 3.21 KB
/
PredefinedFromStringUnmarshallers.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
*/
package akka.http.scaladsl.unmarshalling
import scala.collection.immutable
import akka.http.scaladsl.util.FastFuture
import akka.util.ByteString
trait PredefinedFromStringUnmarshallers {
implicit def _fromStringUnmarshallerFromByteStringUnmarshaller[T](implicit bsum: FromByteStringUnmarshaller[T]): Unmarshaller[String, T] = {
val bs = Unmarshaller.strict[String, ByteString](s ⇒ ByteString(s))
bs.flatMap(implicit ec ⇒ implicit mat ⇒ bsum(_))
}
implicit val byteFromStringUnmarshaller: Unmarshaller[String, Byte] =
numberUnmarshaller(_.toByte, "8-bit signed integer")
implicit val shortFromStringUnmarshaller: Unmarshaller[String, Short] =
numberUnmarshaller(_.toShort, "16-bit signed integer")
implicit val intFromStringUnmarshaller: Unmarshaller[String, Int] =
numberUnmarshaller(_.toInt, "32-bit signed integer")
implicit val longFromStringUnmarshaller: Unmarshaller[String, Long] =
numberUnmarshaller(_.toLong, "64-bit signed integer")
implicit val floatFromStringUnmarshaller: Unmarshaller[String, Float] =
numberUnmarshaller(_.toFloat, "32-bit floating point")
implicit val doubleFromStringUnmarshaller: Unmarshaller[String, Double] =
numberUnmarshaller(_.toDouble, "64-bit floating point")
implicit val booleanFromStringUnmarshaller: Unmarshaller[String, Boolean] =
Unmarshaller.strict[String, Boolean] { string ⇒
string.toLowerCase match {
case "true" | "yes" | "on" | "1" ⇒ true
case "false" | "no" | "off" | "0" ⇒ false
case "" ⇒ throw Unmarshaller.NoContentException
case x ⇒ throw new IllegalArgumentException(s"'$x' is not a valid Boolean value")
}
}
implicit def CsvSeq[T](implicit unmarshaller: Unmarshaller[String, T]): Unmarshaller[String, immutable.Seq[T]] =
Unmarshaller.strict[String, immutable.Seq[String]] { string ⇒
string.split(",").toList
} flatMap { implicit ec ⇒ implicit mat ⇒ strings ⇒
FastFuture.sequence(strings.map(unmarshaller(_)))
}
val HexByte: Unmarshaller[String, Byte] =
numberUnmarshaller(java.lang.Byte.parseByte(_, 16), "8-bit hexadecimal integer")
val HexShort: Unmarshaller[String, Short] =
numberUnmarshaller(java.lang.Short.parseShort(_, 16), "16-bit hexadecimal integer")
val HexInt: Unmarshaller[String, Int] =
numberUnmarshaller(java.lang.Integer.parseInt(_, 16), "32-bit hexadecimal integer")
val HexLong: Unmarshaller[String, Long] =
numberUnmarshaller(java.lang.Long.parseLong(_, 16), "64-bit hexadecimal integer")
private def numberUnmarshaller[T](f: String ⇒ T, target: String): Unmarshaller[String, T] =
Unmarshaller.strict[String, T] { string ⇒
try f(string)
catch numberFormatError(string, target)
}
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)
}
}
object PredefinedFromStringUnmarshallers extends PredefinedFromStringUnmarshallers