-
Notifications
You must be signed in to change notification settings - Fork 6
/
Writer.scala
executable file
·75 lines (66 loc) · 2.71 KB
/
Writer.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
/*
* Copyright (c) 2021 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package fabric.rw
import fabric._
/**
* Writable provides a simple Json => T wrapper functionality
*/
trait Writer[T] {
def write(value: Json): T
}
object Writer {
import RW._
implicit def unitW: Writer[Unit] = unitRW
implicit def valueW: Writer[Json] = valueRW
implicit def objW: Writer[Obj] = objRW
implicit def boolW: Writer[Boolean] = boolRW
implicit def byteR: Writer[Byte] = byteRW
implicit def shortR: Writer[Short] = shortRW
implicit def intW: Writer[Int] = intRW
implicit def longW: Writer[Long] = longRW
implicit def floatW: Writer[Float] = floatRW
implicit def doubleW: Writer[Double] = doubleRW
implicit def bigIntW: Writer[BigInt] = bigIntRW
implicit def bigDecimalW: Writer[BigDecimal] = bigDecimalRW
implicit def stringW: Writer[String] = stringRW
implicit def mapW[V: Writer]: Writer[Map[String, V]] = apply[Map[String, V]] {
v =>
v.asObj.value.map { case (key, value) =>
key -> value.as[V]
}
}
implicit def listW[V: Writer]: Writer[List[V]] =
apply[List[V]](v => v.asVector.map(_.as[V]).toList)
implicit def vectorW[V: Writer]: Writer[Vector[V]] =
apply[Vector[V]](v => v.asVector.map(_.as[V]))
implicit def setW[T](implicit w: Writer[T]): Writer[Set[T]] = apply[Set[T]] {
case Arr(vector) => vector.toSet.map(w.write)
case v => throw new RuntimeException(s"Unsupported set: $v")
}
implicit def optionW[T](implicit w: Writer[T]): Writer[Option[T]] =
apply[Option[T]] {
case Null => None
case v => Option(w.write(v))
}
def apply[T](f: Json => T): Writer[T] = new Writer[T] {
override def write(value: Json): T = f(value)
}
}