-
Notifications
You must be signed in to change notification settings - Fork 6
/
DefType.scala
199 lines (184 loc) · 5.39 KB
/
DefType.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* 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.define
import fabric._
import fabric.rw.RW
import scala.collection.immutable.VectorMap
import scala.util.Try
sealed trait DefType {
def isOpt: Boolean = false
def isNull: Boolean = false
def validate(value: Json): Boolean =
Try(FabricDefinition(value).merge(this)).toOption.contains(this)
def opt: DefType = DefType.Opt(this)
def merge(that: DefType): DefType = if (this == that) {
this
} else if (this.isNull) {
that.opt
} else if (that.isNull) {
this.opt
} else if (this.opt == that) {
that
} else if (this == that.opt) {
this
} else {
throw new RuntimeException(s"Incompatible typed:\n$this\n\n$that")
}
}
object DefType {
implicit def rw: RW[DefType] = RW.from[DefType](
r = dt2V,
w = v2dt,
d = DefType.Null
)
private def dt2V(dt: DefType): Json = dt match {
case Obj(map) =>
obj(
"type" -> "object",
"values" -> fabric.Obj(map.map { case (key, dt) =>
key -> dt2V(dt)
})
)
case Arr(t) =>
obj(
"type" -> "array",
"value" -> dt2V(t)
)
case Opt(t) =>
obj(
"type" -> "optional",
"value" -> dt2V(t)
)
case Str =>
obj(
"type" -> "string"
)
case Int =>
obj(
"type" -> "numeric",
"precision" -> "integer"
)
case Dec =>
obj(
"type" -> "numeric",
"precision" -> "decimal"
)
case Bool =>
obj(
"type" -> "boolean"
)
case Enum(values) =>
obj(
"type" -> "enum",
"values" -> values
)
case Null =>
obj(
"type" -> "null"
)
}
private def v2dt(v: Json): DefType = {
val o = v.asObj
o.value("type").asString match {
case "object" =>
Obj(o.value("values").asMap.map { case (key, value) =>
key -> v2dt(value)
})
case "array" => Arr(v2dt(o.value("value")))
case "optional" => Opt(v2dt(o.value("value")))
case "string" => Str
case "numeric" =>
o.value("precision").asString match {
case "integer" => Int
case "decimal" => Dec
}
case "boolean" => Bool
case "enum" => Enum(o.value("values").asVector.toList)
case "null" => Null
}
}
case class Obj(map: Map[String, DefType]) extends DefType {
override def merge(that: DefType): DefType = that match {
case Obj(thatMap) => Obj(mergeMap(map, thatMap))
case Opt(Obj(thatMap)) => Opt(Obj(mergeMap(map, thatMap)))
case _ => super.merge(that)
}
private def mergeMap(
m1: Map[String, DefType],
m2: Map[String, DefType]
): Map[String, DefType] = {
val keys = m1.keySet ++ m2.keySet
VectorMap(keys.toList.map { key =>
key -> m1.getOrElse(key, Null).merge(m2.getOrElse(key, Null))
}: _*)
}
}
object Obj {
def apply(entries: (String, DefType)*): Obj = Obj(VectorMap(entries: _*))
}
case class Arr(t: DefType) extends DefType {
override def merge(that: DefType): DefType = that match {
case Arr(thatType) => Arr(t.merge(thatType))
case Null => this
case _ => super.merge(that)
}
}
case class Opt(t: DefType) extends DefType {
override def isOpt: Boolean = true
override def opt: DefType = this
override def merge(that: DefType): DefType = that match {
case Opt(thatOpt) =>
t.merge(thatOpt) match {
case o: Opt => o
case result => Opt(result)
}
case o: Obj =>
o.merge(t) match {
case o: Opt => o
case result => Opt(result)
}
case _ => super.merge(that)
}
}
case object Str extends DefType
case object Int extends DefType {
override def merge(that: DefType): DefType = that match {
case DefType.Dec => that
case _ => super.merge(that)
}
}
case object Dec extends DefType {
override def merge(that: DefType): DefType = that match {
case DefType.Int => this
case _ => super.merge(that)
}
}
case object Bool extends DefType
case class Enum(values: List[Json]) extends DefType
case object Null extends DefType {
override def isNull: Boolean = true
override def merge(that: DefType): DefType = that match {
case o: Opt => o
case Null => Null
case _ => that.merge(Null)
}
}
}