-
Notifications
You must be signed in to change notification settings - Fork 6
/
RW.scala
executable file
·148 lines (128 loc) · 5.32 KB
/
RW.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* 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._
import fabric.define.DefType
/**
* RW provides a single class representation of a Reader and Writer for the
* same type
*/
trait RW[T] extends Reader[T] with Writer[T] {
def definition: DefType
def withPreWrite(f: Json => Json): RW[T] = EnhancedRW[T](this, preWrite = List(f))
def withPostRead(f: (T, Json) => Json): RW[T] = EnhancedRW[T](this, postRead = List(f))
}
object RW extends CompileRW {
def from[T](r: T => Json, w: Json => T, d: => DefType): RW[T] = new RW[T] {
override def write(value: Json): T = w(value)
override def read(t: T): Json = r(t)
override def definition: DefType = d
}
def enumeration[T](
list: List[T],
asString: T => String = (t: T) => defaultClassNameMapping(t.getClass.getName),
caseSensitive: Boolean = false
): RW[T] = new RW[T] {
private def fixString(s: String): String = if (caseSensitive) s else s.toLowerCase
private lazy val map = list.map(t => fixString(asString(t)) -> t).toMap
override def write(value: Json): T = map(fixString(value.asString))
override def read(t: T): Json = str(asString(t))
override val definition: DefType = DefType.Enum(list.map(t => Str(asString(t))))
}
def string[T](asString: T => String, fromString: String => T): RW[T] = new RW[T] {
override def write(value: Json): T = fromString(value.asString)
override def read(t: T): Json = str(asString(t))
override def definition: DefType = DefType.Str
}
def wrapped[T](key: String, asJson: T => Json, fromJson: Json => T, definition: DefType = DefType.Json): RW[T] =
RW.from(
r = t => obj(key -> asJson(t)),
w = j => fromJson(j(key)),
d = DefType.Obj(Some(key), key -> definition)
)
/**
* Convenience functionality to provide a static / singleton value that
* represents that type
*
* @param value
* the singleton value to use
*/
def static[T](value: T): RW[T] =
from(_ => obj(), _ => value, DefType.Obj(Some(cleanClassName(value.getClass.getName))))
/**
* Convenience functionality for working with polymorphic types
*
* @param fieldName
* the field name stored in the value (defaults to "type")
* @param getType
* a function to determine the field value from an instance (defaults to
* the class name with the first character lowercase - defaultGetType)
* @param types
* a list of tuples with the type names associated with their RW
*/
def poly[P](fieldName: String = "type", classNameMapping: String => String = defaultClassNameMapping)(
types: RW[_ <: P]*
): RW[P] = {
def typeName(rw: RW[_ <: P]): String = {
val className = rw.definition.className.getOrElse(throw new RuntimeException(s"No className defined for $rw"))
classNameMapping(className)
}
val typeMap = Map(types.map(rw => typeName(rw).toLowerCase -> rw): _*)
from(
r = (p: P) => {
val `type` = classNameMapping(p.getClass.getName)
typeMap.get(`type`.toLowerCase) match {
case Some(rw) => rw.asInstanceOf[RW[P]].read(p).merge(obj("type" -> `type`))
case None => throw new RuntimeException(
s"Type not found [${`type`}] converting from value $p. Available types are: [${typeMap.keySet.mkString(", ")}]"
)
}
},
w = (json: Json) => {
val `type` = json(fieldName).asString.toLowerCase
typeMap.get(`type`) match {
case Some(rw) => rw.write(json)
case None => throw new RuntimeException(
s"Type not found [${`type`}] converting from value $json. Available types are: [${typeMap.keySet.mkString(", ")}]"
)
}
},
d = DefType.Poly(types.map { rw =>
val obj = rw.definition.asInstanceOf[DefType.Obj]
val key = typeName(rw)
key -> obj
}.toMap)
)
}
def cleanClassName(className: String): String = className.replace("$u0020", " ").replace("$", ".") match {
case s if s.endsWith(".") => s.substring(0, s.length - 1)
case s => s
}
def defaultClassNameMapping(className: String): String = {
val cn = cleanClassName(className)
val index = cn.lastIndexOf('.')
if (index != -1) {
cn.substring(index + 1)
} else {
cn
}
}.replace("$", "")
}