diff options
author | Johannes Rudolph <johannes_rudolph@gmx.de> | 2012-05-31 16:25:14 +0200 |
---|---|---|
committer | Johannes Rudolph <johannes_rudolph@gmx.de> | 2012-05-31 16:25:14 +0200 |
commit | b3fa02c7315dd94da937de9857f41e9d15f1bcc4 (patch) | |
tree | 95b87f53acb46ad472ee3622c28a61777ef53018 /src/main/scala | |
parent | 80009f36f965e5237a0b2e3789da327658fec871 (diff) | |
download | spray-json-b3fa02c7315dd94da937de9857f41e9d15f1bcc4.tar.gz spray-json-b3fa02c7315dd94da937de9857f41e9d15f1bcc4.tar.bz2 spray-json-b3fa02c7315dd94da937de9857f41e9d15f1bcc4.zip |
smallish code cleanup and more documentation
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 { |