diff options
Diffstat (limited to 'src/main/scala/cc/spray/json')
-rw-r--r-- | src/main/scala/cc/spray/json/lenses/ExtraImplicits.scala | 8 | ||||
-rw-r--r-- | src/main/scala/cc/spray/json/lenses/JsonLenses.scala | 4 | ||||
-rw-r--r-- | src/main/scala/cc/spray/json/lenses/JsonPathIntegration.scala | 12 | ||||
-rw-r--r-- | src/main/scala/cc/spray/json/lenses/Lens.scala (renamed from src/main/scala/cc/spray/json/lenses/Projection.scala) | 24 | ||||
-rw-r--r-- | src/main/scala/cc/spray/json/lenses/Operations.scala | 2 | ||||
-rw-r--r-- | src/main/scala/cc/spray/json/lenses/Ops.scala | 2 | ||||
-rw-r--r-- | src/main/scala/cc/spray/json/lenses/OptionLenses.scala | 2 | ||||
-rw-r--r-- | src/main/scala/cc/spray/json/lenses/ReadLens.scala | 3 | ||||
-rw-r--r-- | src/main/scala/cc/spray/json/lenses/ScalarLenses.scala | 8 | ||||
-rw-r--r-- | src/main/scala/cc/spray/json/lenses/SeqLenses.scala | 8 | ||||
-rw-r--r-- | src/main/scala/cc/spray/json/lenses/UpdateLens.scala | 2 | ||||
-rw-r--r-- | src/main/scala/cc/spray/json/lenses/package.scala | 6 |
12 files changed, 38 insertions, 43 deletions
diff --git a/src/main/scala/cc/spray/json/lenses/ExtraImplicits.scala b/src/main/scala/cc/spray/json/lenses/ExtraImplicits.scala index 9951c94..216a63e 100644 --- a/src/main/scala/cc/spray/json/lenses/ExtraImplicits.scala +++ b/src/main/scala/cc/spray/json/lenses/ExtraImplicits.scala @@ -11,14 +11,14 @@ trait ExtraImplicits { lens ! Operations.set(pValue) apply value // This can't be simplified because we don't want the type constructor - // of projection to appear in the type paramater list. - def extract[T: Reader](p: Projection[Id]): T = + // for Lens[M] to appear in the type paramater list. + def extract[T: Reader](p: Lens[Id]): T = p.get[T](value) - def extract[T: Reader](p: Projection[Option]): Option[T] = + def extract[T: Reader](p: Lens[Option]): Option[T] = p.get[T](value) - def extract[T: Reader](p: Projection[Seq]): Seq[T] = + def extract[T: Reader](p: Lens[Seq]): Seq[T] = p.get[T](value) def as[T: Reader]: Validated[T] = diff --git a/src/main/scala/cc/spray/json/lenses/JsonLenses.scala b/src/main/scala/cc/spray/json/lenses/JsonLenses.scala index 8ebc011..08a3312 100644 --- a/src/main/scala/cc/spray/json/lenses/JsonLenses.scala +++ b/src/main/scala/cc/spray/json/lenses/JsonLenses.scala @@ -13,6 +13,6 @@ object JsonLenses extends JsonPathIntegration with ExtraImplicits { - implicit def strToField(name: String): ScalarProjection = field(name) - implicit def symbolToField(sym: Symbol): ScalarProjection = field(sym.name) + implicit def strToField(name: String): ScalarLens = field(name) + implicit def symbolToField(sym: Symbol): ScalarLens = field(sym.name) }
\ No newline at end of file diff --git a/src/main/scala/cc/spray/json/lenses/JsonPathIntegration.scala b/src/main/scala/cc/spray/json/lenses/JsonPathIntegration.scala index 29e960c..311669b 100644 --- a/src/main/scala/cc/spray/json/lenses/JsonPathIntegration.scala +++ b/src/main/scala/cc/spray/json/lenses/JsonPathIntegration.scala @@ -3,17 +3,17 @@ package lenses trait JsonPathIntegration { self: ScalarLenses with SeqLenses => /** - * Create a projection from a json-path expression. + * Create a Lens from a json-path expression. */ - def fromPath(path: String): Projection[Seq] = + def fromPath(path: String): Lens[Seq] = fromPath(JsonPathParser(path)) - def fromPath(ast: JsonPath.Path): Projection[Seq] = { - def convertPath(path: JsonPath.Path): Projection[Seq] = path match { + def fromPath(ast: JsonPath.Path): Lens[Seq] = { + def convertPath(path: JsonPath.Path): Lens[Seq] = path match { case JsonPath.Root => value.toSeq - case JsonPath.Selection(inner, proj) => convertPath(inner) / convertProjection(proj) + case JsonPath.Selection(inner, proj) => convertPath(inner) / convertLens(proj) } - def convertProjection(proj: JsonPath.Projection): Projection[Seq] = + def convertLens(proj: JsonPath.Projection): Lens[Seq] = proj match { case JsonPath.ByField(name) => field(name).toSeq case JsonPath.ByIndex(i) => element(i).toSeq diff --git a/src/main/scala/cc/spray/json/lenses/Projection.scala b/src/main/scala/cc/spray/json/lenses/Lens.scala index 0db5b88..4ae89c0 100644 --- a/src/main/scala/cc/spray/json/lenses/Projection.scala +++ b/src/main/scala/cc/spray/json/lenses/Lens.scala @@ -2,28 +2,28 @@ package cc.spray.json package lenses /** - * A projection combines read and update functions of UpdateLens and ReadLens into + * A Lens combines read and update functions of UpdateLens and ReadLens into * combinable chunks. * - * A projection can either operate on a scalar value, or on an optional value, or on a + * A lens 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] { +trait Lens[M[_]] extends UpdateLens with ReadLens[M] { /** - * Combines two projections. + * Combines two lenses. */ - def /[M2[_], R[_]](next: Projection[M2])(implicit ev: Join[M2, M, R]): Projection[R] + def /[M2[_], R[_]](next: Lens[M2])(implicit ev: Join[M2, M, R]): Lens[R] - def toSeq: Projection[Seq] + def toSeq: Lens[Seq] def ops: Ops[M] } /** - * This implements most of the methods of `Projection`. Implementors of a new type of projection + * This implements most of the methods of `Lens`. Implementors of a new type of lens * must implement `retr` for the read side of the lens and `updated` for the update side of the lens. */ -trait ProjectionImpl[M[_]] extends Projection[M] { outer => +abstract class LensImpl[M[_]](implicit val ops: Ops[M]) extends Lens[M] { outer => import ExtraImplicits.richValue def tryGet[T: Reader](p: JsValue): Validated[M[T]] = @@ -40,9 +40,7 @@ trait ProjectionImpl[M[_]] extends Projection[M] { outer => def is[U: Reader](f: U => Boolean): JsPred = value => tryGet[U](value) exists (x => ops.map(x)(f).forall(identity)) - def /[M2[_], R[_]](next: Projection[M2])(implicit ev: Join[M2, M, R]): Projection[R] = new ProjectionImpl[R] { - val ops: Ops[R] = ev.get(next.ops, outer.ops) - + def /[M2[_], R[_]](next: Lens[M2])(implicit ev: Join[M2, M, R]): Lens[R] = new LensImpl[R]()(ev.get(next.ops, outer.ops)) { def retr: JsValue => Validated[R[JsValue]] = parent => for { outerV <- outer.retr(parent) @@ -53,10 +51,8 @@ trait ProjectionImpl[M[_]] extends Projection[M] { outer => outer.updated(_.flatMap(next.updated(f)))(parent) } - def toSeq: Projection[Seq] = this / SeqLenses.asSeq + def toSeq: Lens[Seq] = this / SeqLenses.asSeq private[this] def mapValue[T](value: M[JsValue])(f: JsValue => Validated[T]): Validated[M[T]] = ops.allRight(ops.map(value)(f)) } - -abstract class Proj[M[_]](implicit val ops: Ops[M]) extends ProjectionImpl[M]
\ No newline at end of file diff --git a/src/main/scala/cc/spray/json/lenses/Operations.scala b/src/main/scala/cc/spray/json/lenses/Operations.scala index 20e6572..5e3c790 100644 --- a/src/main/scala/cc/spray/json/lenses/Operations.scala +++ b/src/main/scala/cc/spray/json/lenses/Operations.scala @@ -33,7 +33,7 @@ trait Operations { _: ExtraImplicits => def append(update: Update): Operation = ??? def update(update: Update): Operation = ??? - def extract[M[_], T](value: Projection[M])(f: M[T] => Update): Operation = ??? + def extract[M[_], T](value: Lens[M])(f: M[T] => Update): Operation = ??? } object Operations extends Operations with ExtraImplicits
\ No newline at end of file diff --git a/src/main/scala/cc/spray/json/lenses/Ops.scala b/src/main/scala/cc/spray/json/lenses/Ops.scala index dd3522b..1511921 100644 --- a/src/main/scala/cc/spray/json/lenses/Ops.scala +++ b/src/main/scala/cc/spray/json/lenses/Ops.scala @@ -7,7 +7,7 @@ package cc.spray.json.lenses * * This could probably made more general but the methods defined here comprise * exactly the set of operations needed to allow combining different kinds of - * projections. + * lenses. */ trait Ops[M[_]] { def flatMap[T, U](els: M[T])(f: T => Seq[U]): Seq[U] diff --git a/src/main/scala/cc/spray/json/lenses/OptionLenses.scala b/src/main/scala/cc/spray/json/lenses/OptionLenses.scala index d358310..19fdd8d 100644 --- a/src/main/scala/cc/spray/json/lenses/OptionLenses.scala +++ b/src/main/scala/cc/spray/json/lenses/OptionLenses.scala @@ -5,7 +5,7 @@ trait OptionLenses { /** * Operates on the first element of an JsArray which matches the predicate. */ - def find(pred: JsPred): OptProjection = new Proj[Option] { + def find(pred: JsPred): OptLens = new LensImpl[Option] { def updated(f: SafeJsValue => SafeJsValue)(parent: JsValue): SafeJsValue = parent match { case JsArray(elements) => elements.span(x => !pred(x)) match { diff --git a/src/main/scala/cc/spray/json/lenses/ReadLens.scala b/src/main/scala/cc/spray/json/lenses/ReadLens.scala index c43c37c..a320237 100644 --- a/src/main/scala/cc/spray/json/lenses/ReadLens.scala +++ b/src/main/scala/cc/spray/json/lenses/ReadLens.scala @@ -10,8 +10,7 @@ package lenses trait ReadLens[M[_]] { /** * Given a parent JsValue, tries to extract the child value. - * @return `Right(value)` if the projection succeeds. `Left(error)` if the projection - * fails. + * @return `Right(value)` if the lens read succeeds. `Left(error)` if the lens read fails. */ def retr: JsValue => Validated[M[JsValue]] diff --git a/src/main/scala/cc/spray/json/lenses/ScalarLenses.scala b/src/main/scala/cc/spray/json/lenses/ScalarLenses.scala index 6b19abe..348a815 100644 --- a/src/main/scala/cc/spray/json/lenses/ScalarLenses.scala +++ b/src/main/scala/cc/spray/json/lenses/ScalarLenses.scala @@ -5,7 +5,7 @@ trait ScalarLenses { /** * Accesses a field of a JsObject. */ - def field(name: String): ScalarProjection = new Proj[Id] { + def field(name: String): ScalarLens = new LensImpl[Id] { def updated(f: SafeJsValue => SafeJsValue)(parent: JsValue): SafeJsValue = for (updatedValue <- f(retr(parent))) // asJsObject is already guarded by getField above, FIXME: is it really? @@ -24,7 +24,7 @@ trait ScalarLenses { /** * Accesses an element of a JsArray. */ - def element(idx: Int): ScalarProjection = new Proj[Id] { + def element(idx: Int): ScalarLens = new LensImpl[Id] { def updated(f: SafeJsValue => SafeJsValue)(parent: JsValue): SafeJsValue = parent match { case JsArray(elements) => if (idx < elements.size) { @@ -47,9 +47,9 @@ trait ScalarLenses { } /** - * The identity projection which operates on the current element itself + * The identity lens which operates on the current element itself */ - val value: ScalarProjection = new Proj[Id] { + val value: ScalarLens = new LensImpl[Id] { def updated(f: SafeJsValue => SafeJsValue)(parent: JsValue): SafeJsValue = f(Right(parent)) diff --git a/src/main/scala/cc/spray/json/lenses/SeqLenses.scala b/src/main/scala/cc/spray/json/lenses/SeqLenses.scala index 6d627f2..8f1da60 100644 --- a/src/main/scala/cc/spray/json/lenses/SeqLenses.scala +++ b/src/main/scala/cc/spray/json/lenses/SeqLenses.scala @@ -3,10 +3,10 @@ package lenses trait SeqLenses { /** - * The projection which just converts another Projection into one of a + * The lens which just converts another Lens into one of a * Seq value. */ - val asSeq: SeqProjection = new Proj[Seq] { + val asSeq: SeqLens = new LensImpl[Seq] { def updated(f: Operation)(parent: JsValue): SafeJsValue = f(Right(parent)) @@ -16,7 +16,7 @@ trait SeqLenses { /** * All the elements of a JsArray. */ - val elements: SeqProjection = new Proj[Seq] { + val elements: SeqLens = new LensImpl[Seq] { def updated(f: SafeJsValue => SafeJsValue)(parent: JsValue): SafeJsValue = parent match { case JsArray(elements) => ops.allRight(elements.map(x => f(Right(x)))).map(JsArray(_: _*)) @@ -35,7 +35,7 @@ trait SeqLenses { /** * All the values of a JsArray which match the predicate. */ - def filter(pred: JsPred): SeqProjection = new Proj[Seq] { + def filter(pred: JsPred): SeqLens = new LensImpl[Seq] { def updated(f: SafeJsValue => SafeJsValue)(parent: JsValue): SafeJsValue = parent match { case JsArray(elements) => ops.allRight(elements.map(x => if (pred(x)) f(Right(x)) else Right(x))).map(JsArray(_: _*)) diff --git a/src/main/scala/cc/spray/json/lenses/UpdateLens.scala b/src/main/scala/cc/spray/json/lenses/UpdateLens.scala index abe5a92..13eeea4 100644 --- a/src/main/scala/cc/spray/json/lenses/UpdateLens.scala +++ b/src/main/scala/cc/spray/json/lenses/UpdateLens.scala @@ -26,7 +26,7 @@ trait UpdateLens { * `Left(error)` in case the operation fails. * * `updated` returns `Left(error)` if the update operation or any of any intermediate - * projections fail. + * lens fails. */ def updated(f: Operation)(parent: JsValue): SafeJsValue diff --git a/src/main/scala/cc/spray/json/lenses/package.scala b/src/main/scala/cc/spray/json/lenses/package.scala index 685a52b..acade9b 100644 --- a/src/main/scala/cc/spray/json/lenses/package.scala +++ b/src/main/scala/cc/spray/json/lenses/package.scala @@ -8,9 +8,9 @@ package object lenses { type Operation = SafeJsValue => SafeJsValue - type ScalarProjection = Projection[Id] - type OptProjection = Projection[Option] - type SeqProjection = Projection[Seq] + type ScalarLens = Lens[Id] + type OptLens = Lens[Option] + type SeqLens = Lens[Seq] def ??? = sys.error("NYI") def unexpected(message: String) = Left(new RuntimeException(message)) |