-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
JsLookup.scala
190 lines (170 loc) · 5.8 KB
/
JsLookup.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
/*
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
*/
package play.api.libs.json
/**
* A value representing the value at a particular JSON path, either an actual JSON node or undefined.
*/
case class JsLookup(result: JsLookupResult) extends AnyVal {
/**
* Access the head of this array.
*/
def head: JsLookupResult = result match {
case JsDefined(JsArray(head +: tail)) => JsDefined(head)
case JsDefined(arr @ JsArray(_)) => JsUndefined(s"Cannot get head of $arr")
case JsDefined(o) => JsUndefined(s"$o is not an array")
case undef => undef
}
/**
* Access the tail of this array.
*/
def tail: JsLookupResult = result match {
case JsDefined(JsArray(head +: tail)) => JsDefined(JsArray(tail))
case JsDefined(arr @ JsArray(_)) => JsUndefined(s"Cannot get tail of $arr")
case JsDefined(o) => JsUndefined(s"$o is not an array")
case undef => undef
}
/**
* Access the last element of this array.
*/
def last: JsLookupResult = result match {
case JsDefined(JsArray(values)) if values.nonEmpty => JsDefined(values.last)
case JsDefined(arr @ JsArray(_)) => JsUndefined(s"Cannot get last element of $arr")
case JsDefined(o) => JsUndefined(s"$o is not an array")
case undef => undef
}
/**
* Access a value of this array.
*
* @param index Element index.
*/
def apply(index: Int): JsValue = result match {
case JsDefined(x) => x match {
case arr: JsArray => arr.value.lift(index) match {
case Some(x) => x
case None => throw new IndexOutOfBoundsException(String.valueOf(index))
}
case _ =>
throw new Exception(x + " is not a JsArray")
}
case x: JsUndefined =>
throw new Exception(String.valueOf(x.error))
}
/**
* Access a value of this array.
*
* @param fieldName Element index.
*/
def apply(fieldName: String): JsValue = result match {
case JsDefined(x) => x match {
case arr: JsObject => arr.value.lift(fieldName) match {
case Some(x) => x
case None => throw new NoSuchElementException(String.valueOf(fieldName))
}
case _ =>
throw new Exception(x + " is not a JsObject")
}
case x: JsUndefined =>
throw new Exception(String.valueOf(x.error))
}
/**
* Access a value of this array.
*
* @param index Element index
*/
def \(index: Int): JsLookupResult = result match {
case JsDefined(arr: JsArray) =>
arr.value.lift(index).map(JsDefined.apply).getOrElse(JsUndefined(s"Array index out of bounds in $arr"))
case JsDefined(o) =>
JsUndefined(s"$o is not an array")
case undef => undef
}
/**
* Return the property corresponding to the fieldName, supposing we have a JsObject.
*
* @param fieldName the name of the property to look up
* @return the resulting JsValue wrapped in a JsLookup. If the current node is not a JsObject or doesn't have the
* property, a JsUndefined will be returned.
*/
def \(fieldName: String): JsLookupResult = result match {
case JsDefined(obj @ JsObject(_)) =>
obj.value.get(fieldName).map(JsDefined.apply)
.getOrElse(JsUndefined(s"'$fieldName' is undefined on object: $obj"))
case JsDefined(o) =>
JsUndefined(s"$o is not an object")
case undef => undef
}
/**
* Look up fieldName in the current object and all descendants.
*
* @return the list of matching nodes
*/
def \\(fieldName: String): collection.Seq[JsValue] = result match {
case JsDefined(obj: JsObject) =>
obj.value.foldLeft(Seq[JsValue]())((o, pair) => pair match {
case (key, value) if key == fieldName => o ++ (value +: (value \\ fieldName))
case (_, value) => o ++ (value \\ fieldName)
})
case JsDefined(arr: JsArray) =>
arr.value.flatMap(_ \\ fieldName)
case _ => Seq.empty
}
}
sealed trait JsLookupResult extends Any with JsReadable {
/**
* Tries to convert the node into a JsValue
*/
def toOption: Option[JsValue] = this match {
case JsDefined(v) => Some(v)
case _ => None
}
def toEither: Either[JsonValidationError, JsValue] = this match {
case JsDefined(v) => Right(v)
case undef: JsUndefined => Left(undef.validationError)
}
def get: JsValue = toOption.get
def getOrElse(v: => JsValue): JsValue = toOption.getOrElse(v)
def isEmpty: Boolean = this match {
case JsUndefined() => true
case JsDefined(_) => false
}
def isDefined: Boolean = !isEmpty
/**
* If this result is defined return `this`. Otherwise return `alternative`.
*/
def orElse(alternative: => JsLookupResult): JsLookupResult =
if (isDefined) this else alternative
def validate[A](implicit rds: Reads[A]): JsResult[A] = this match {
case JsDefined(v) => v.validate[A]
case undef: JsUndefined => JsError(undef.validationError)
}
/**
* If this result contains `JsNull` or is undefined, returns `JsSuccess(None)`.
* Otherwise returns the result of validating as an `A` and wrapping the result in a `Some`.
*/
def validateOpt[A](implicit rds: Reads[A]): JsResult[Option[A]] =
this match {
case JsDefined(a) => Reads.optionWithNull(rds).reads(a)
case _ => JsSuccess(None)
}
}
object JsLookupResult {
import scala.language.implicitConversions
implicit def jsLookupResultToJsLookup(value: JsLookupResult): JsLookup = JsLookup(value)
}
/**
* Wrapper for JsValue to represent an existing Json value.
*/
case class JsDefined(value: JsValue) extends AnyVal with JsLookupResult
/**
* Represent a missing Json value.
*/
final class JsUndefined(err: => String) extends JsLookupResult {
def error = err
def validationError = JsonValidationError(error)
override def toString = s"JsUndefined($err)"
}
object JsUndefined {
def apply(err: => String) = new JsUndefined(err)
def unapply(o: Object): Boolean = o.isInstanceOf[JsUndefined]
}