diff options
Diffstat (limited to 'src/main/scala')
-rw-r--r-- | src/main/scala/cc/spray/json/lenses/Projection.scala | 7 | ||||
-rw-r--r-- | src/main/scala/cc/spray/json/lenses/ScalarLenses.scala | 21 | ||||
-rw-r--r-- | src/main/scala/cc/spray/json/lenses/UpdateLens.scala | 3 |
3 files changed, 14 insertions, 17 deletions
diff --git a/src/main/scala/cc/spray/json/lenses/Projection.scala b/src/main/scala/cc/spray/json/lenses/Projection.scala index fe44115..7f17886 100644 --- a/src/main/scala/cc/spray/json/lenses/Projection.scala +++ b/src/main/scala/cc/spray/json/lenses/Projection.scala @@ -4,9 +4,14 @@ package lenses /** * A projection combines read and update functions of UpdateLens and ReadLens into * combinable chunks. - * @tparam M + * + * A projection can either operate on a scalar value, or on an optional value, or on a + * sequence value. This is denoted by the `M[_]` type constructor. */ trait Projection[M[_]] extends UpdateLens with ReadLens[M] { + /** + * Combines two projections. + */ def /[M2[_], R[_]](next: Projection[M2])(implicit ev: Join[M2, M, R]): Projection[R] def toSeq: Projection[Seq] diff --git a/src/main/scala/cc/spray/json/lenses/ScalarLenses.scala b/src/main/scala/cc/spray/json/lenses/ScalarLenses.scala index 05d8657..12419ec 100644 --- a/src/main/scala/cc/spray/json/lenses/ScalarLenses.scala +++ b/src/main/scala/cc/spray/json/lenses/ScalarLenses.scala @@ -4,24 +4,17 @@ package lenses trait ScalarLenses { def field(name: String): ScalarProjection = new Proj[Id] { def updated(f: SafeJsValue => SafeJsValue)(parent: JsValue): SafeJsValue = - for { - res <- f(getField(parent)) - } - yield JsObject(fields = parent.asJsObject.fields + (name -> res)) + for (updatedValue <- f(retr(parent))) + // asJsObject is already guarded by getField above, FIXME: is it really? + yield JsObject(fields = parent.asJsObject.fields + (name -> updatedValue)) - def retr: JsValue => SafeJsValue = v => - getField(v) - - def getField(v: JsValue): SafeJsValue = asObj(v) flatMap { - o => - o.fields.get(name).getOrError("Expected field '%s' in '%s'" format(name, v)) + def retr: JsValue => SafeJsValue = v => asObj(v) flatMap { + _.fields.get(name).getOrError("Expected field '%s' in '%s'" format(name, v)) } def asObj(v: JsValue): Validated[JsObject] = v match { - case o: JsObject => - Right(o) - case e@_ => - unexpected("Not a json object: " + e) + case o: JsObject => Right(o) + case e@_ => unexpected("Not a json object: " + e) } } diff --git a/src/main/scala/cc/spray/json/lenses/UpdateLens.scala b/src/main/scala/cc/spray/json/lenses/UpdateLens.scala index c532b15..79c8ed2 100644 --- a/src/main/scala/cc/spray/json/lenses/UpdateLens.scala +++ b/src/main/scala/cc/spray/json/lenses/UpdateLens.scala @@ -1,8 +1,7 @@ package cc.spray.json package lenses -trait Update extends (JsValue => JsValue) { - outer => +trait Update extends (JsValue => JsValue) { outer => def apply(value: JsValue): JsValue def &&(next: Update): Update = new Update { |