summaryrefslogtreecommitdiff
path: root/src/main/scala
diff options
context:
space:
mode:
authorJohannes Rudolph <johannes.rudolph@gmail.com>2013-05-29 15:07:54 +0200
committerJohannes Rudolph <johannes.rudolph@gmail.com>2013-05-29 15:07:54 +0200
commitf0a2d917db8b0bd1ad27421b72b28984be7d2cc3 (patch)
treef75a4fb1789b9d52953c423901a52253a3df636f /src/main/scala
parent4279fcebd188987e9f74aa9aa046a1b8d9bdfbd4 (diff)
downloadspray-json-f0a2d917db8b0bd1ad27421b72b28984be7d2cc3.tar.gz
spray-json-f0a2d917db8b0bd1ad27421b72b28984be7d2cc3.tar.bz2
spray-json-f0a2d917db8b0bd1ad27421b72b28984be7d2cc3.zip
refactoring: move OptioalFieldOperations into their own file
Diffstat (limited to 'src/main/scala')
-rw-r--r--src/main/scala/spray/json/lenses/Operations.scala39
-rw-r--r--src/main/scala/spray/json/lenses/OptionalFieldOperations.scala43
2 files changed, 44 insertions, 38 deletions
diff --git a/src/main/scala/spray/json/lenses/Operations.scala b/src/main/scala/spray/json/lenses/Operations.scala
index f6cb0f8..0ec07ff 100644
--- a/src/main/scala/spray/json/lenses/Operations.scala
+++ b/src/main/scala/spray/json/lenses/Operations.scala
@@ -4,7 +4,7 @@ package lenses
/**
* Defines a set of operations to update Json values.
*/
-trait Operations { _: ExtraImplicits =>
+trait Operations extends OptionalFieldOperations { _: ExtraImplicits =>
/**
* The set operation sets or creates a value.
*/
@@ -31,43 +31,6 @@ trait Operations { _: ExtraImplicits =>
value.as[T] map (v => f(v).toJson)
}
- /**
- * The `setOrUpdateField` operation sets or updates an optionalField.
- */
- def setOrUpdateField[T: Reader : JsonWriter](f: Option[T] => T): Operation =
- updateOptionalField[T](f andThen Some.apply)
-
- /**
- * The `setOrUpdateField` operation sets or updates an optionalField.
- */
- def setOrUpdateField[T: Reader : JsonWriter](default: => T)(f: T => T): Operation =
- updateOptionalField[T](_.map(f).orElse(Some(default)))
-
- /**
- * The `modifyOrDeleteField` operation works together with the `optionalField` lens.
- * The passed function is called for every existing field. If the function returns
- * `Some(value)`, this will become the new value. If the function returns `None` the
- * field will be deleted.
- */
- def modifyOrDeleteField[T: Reader : JsonWriter](f: T => Option[T]): Operation =
- updateOptionalField[T](_.flatMap(f))
-
- /**
- * The `updateOptionalField` operation works together with the `optionalField` lens. It allows
- * to a) create a previously missing field b) update an existing field value c) remove an existing
- * field d) ignore a missing field.
- */
- def updateOptionalField[T: Reader : JsonWriter](f: Option[T] => Option[T]): Operation = new Operation {
- def apply(value: SafeJsValue): SafeJsValue = {
- val oldValue = value.flatMap(_.as[T]) match {
- case Right(v) => Right(Some(v))
- case OptionLenses.FieldMissing => Right(None)
- case l: Left[Exception, Option[T]] @unchecked => l
- }
- oldValue flatMap (v => f(v).map(x => Right(x.toJson)).getOrElse(OptionLenses.FieldMissing))
- }
- }
-
def append(update: Update): Operation = ???
def update(update: Update): Operation = ???
def extract[M[_], T](value: Lens[M])(f: M[T] => Update): Operation = ???
diff --git a/src/main/scala/spray/json/lenses/OptionalFieldOperations.scala b/src/main/scala/spray/json/lenses/OptionalFieldOperations.scala
new file mode 100644
index 0000000..0728bc0
--- /dev/null
+++ b/src/main/scala/spray/json/lenses/OptionalFieldOperations.scala
@@ -0,0 +1,43 @@
+package spray.json
+package lenses
+
+trait OptionalFieldOperations {
+ import ExtraImplicits._
+
+ /**
+ * The `setOrUpdateField` operation sets or updates an optionalField.
+ */
+ def setOrUpdateField[T: Reader : JsonWriter](f: Option[T] => T): Operation =
+ updateOptionalField[T](f andThen Some.apply)
+
+ /**
+ * The `setOrUpdateField` operation sets or updates an optionalField.
+ */
+ def setOrUpdateField[T: Reader : JsonWriter](default: => T)(f: T => T): Operation =
+ updateOptionalField[T](_.map(f).orElse(Some(default)))
+
+ /**
+ * The `modifyOrDeleteField` operation works together with the `optionalField` lens.
+ * The passed function is called for every existing field. If the function returns
+ * `Some(value)`, this will become the new value. If the function returns `None` the
+ * field will be deleted.
+ */
+ def modifyOrDeleteField[T: Reader : JsonWriter](f: T => Option[T]): Operation =
+ updateOptionalField[T](_.flatMap(f))
+
+ /**
+ * The `updateOptionalField` operation works together with the `optionalField` lens. It allows
+ * to a) create a previously missing field b) update an existing field value c) remove an existing
+ * field d) ignore a missing field.
+ */
+ def updateOptionalField[T: Reader : JsonWriter](f: Option[T] => Option[T]): Operation = new Operation {
+ def apply(value: SafeJsValue): SafeJsValue = {
+ val oldValue = value.flatMap(_.as[T]) match {
+ case Right(v) => Right(Some(v))
+ case OptionLenses.FieldMissing => Right(None)
+ case l: Left[Exception, Option[T]] @unchecked => l
+ }
+ oldValue flatMap (v => f(v).map(x => Right(x.toJson)).getOrElse(OptionLenses.FieldMissing))
+ }
+ }
+}