Skip to content

Commit

Permalink
Merge pull request #48 from agourlay/syntax-improvements
Browse files Browse the repository at this point in the history
Syntax improvements
  • Loading branch information
satabin committed Jan 20, 2017
2 parents 11f1a32 + 7a26ca0 commit f9705f5
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 59 deletions.
84 changes: 41 additions & 43 deletions circe/src/main/scala/gnieh/diffson/CirceInstance.scala
Expand Up @@ -40,49 +40,47 @@ class CirceInstance extends DiffsonInstance[Json] {
}

implicit val operationEncoder: Encoder[Operation] =
new Encoder[Operation] {
override def apply(op: Operation): Json = op match {
case Add(path, value) =>
Json.obj(
"op" -> Json.fromString("add"),
"path" -> Json.fromString(path.toString),
"value" -> value)
case Remove(path, None) =>
Json.obj(
"op" -> Json.fromString("remove"),
"path" -> Json.fromString(path.toString))
case Remove(path, Some(old)) =>
Json.obj(
"op" -> Json.fromString("remove"),
"path" -> Json.fromString(path.toString),
"old" -> old)
case Replace(path, value, None) =>
Json.obj(
"op" -> Json.fromString("replace"),
"path" -> Json.fromString(path.toString),
"value" -> value)
case Replace(path, value, Some(old)) =>
Json.obj(
"op" -> Json.fromString("replace"),
"path" -> Json.fromString(path.toString),
"value" -> value,
"old" -> old)
case Move(from, path) =>
Json.obj(
"op" -> Json.fromString("move"),
"from" -> Json.fromString(from.toString),
"path" -> Json.fromString(path.toString))
case Copy(from, path) =>
Json.obj(
"op" -> Json.fromString("copy"),
"from" -> Json.fromString(from.toString),
"path" -> Json.fromString(path.toString))
case Test(path, value) =>
Json.obj(
"op" -> Json.fromString("test"),
"path" -> Json.fromString(path.toString),
"value" -> value)
}
Encoder.instance[Operation] {
case Add(path, value) =>
Json.obj(
"op" -> Json.fromString("add"),
"path" -> Json.fromString(path.toString),
"value" -> value)
case Remove(path, None) =>
Json.obj(
"op" -> Json.fromString("remove"),
"path" -> Json.fromString(path.toString))
case Remove(path, Some(old)) =>
Json.obj(
"op" -> Json.fromString("remove"),
"path" -> Json.fromString(path.toString),
"old" -> old)
case Replace(path, value, None) =>
Json.obj(
"op" -> Json.fromString("replace"),
"path" -> Json.fromString(path.toString),
"value" -> value)
case Replace(path, value, Some(old)) =>
Json.obj(
"op" -> Json.fromString("replace"),
"path" -> Json.fromString(path.toString),
"value" -> value,
"old" -> old)
case Move(from, path) =>
Json.obj(
"op" -> Json.fromString("move"),
"from" -> Json.fromString(from.toString),
"path" -> Json.fromString(path.toString))
case Copy(from, path) =>
Json.obj(
"op" -> Json.fromString("copy"),
"from" -> Json.fromString(from.toString),
"path" -> Json.fromString(path.toString))
case Test(path, value) =>
Json.obj(
"op" -> Json.fromString("test"),
"path" -> Json.fromString(path.toString),
"value" -> value)
}

implicit def operationDecoder(implicit pointer: JsonPointer): Decoder[Operation] =
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/gnieh/diffson/JsonPointer.scala
Expand Up @@ -54,7 +54,7 @@ trait JsonPointerSupport[JsValue] {
val parts = input.split("/")
// the first element is always empty as the path starts with a '/'
.drop(1)
if (parts.size == 0) {
if (parts.length == 0) {
// the pointer was simply "/"
Path(Root, "")
} else {
Expand Down
6 changes: 2 additions & 4 deletions core/src/test/scala/gnieh/diffson/TestSerialization.scala
Expand Up @@ -78,17 +78,15 @@ abstract class TestSerialization[JsValue, Instance <: DiffsonInstance[JsValue]](
Add(Pointer("c"), marshall("test2")),
Test(Pointer("d"), marshall(false)),
Copy(Pointer("c"), Pointer("e")),
Move(Pointer("d"), Pointer("f", "g"))
)
Move(Pointer("d"), Pointer("f", "g")))

val jsonRemember = JsonPatch(
Replace(Pointer("a"), marshall(6), Some(marshall(5))),
Remove(Pointer("b"), Some(marshall("removed value"))),
Add(Pointer("c"), marshall("test2")),
Test(Pointer("d"), marshall(false)),
Copy(Pointer("c"), Pointer("e")),
Move(Pointer("d"), Pointer("f", "g"))
)
Move(Pointer("d"), Pointer("f", "g")))

"a patch json" should "be correctly deserialized from a Json object" in {
unmarshall[JsonPatch](parsed) should be(json)
Expand Down
Expand Up @@ -27,13 +27,15 @@ abstract class TestRfcConformance[JsValue, Instance <: DiffsonInstance[JsValue]]

trait ConformanceTest

case class SuccessConformanceTest(doc: JsValue,
case class SuccessConformanceTest(
doc: JsValue,
patch: JsArray,
expected: Option[JsValue],
comment: Option[String],
disabled: Option[Boolean]) extends ConformanceTest

case class ErrorConformanceTest(doc: JsValue,
case class ErrorConformanceTest(
doc: JsValue,
patch: JsArray,
error: String,
comment: Option[String],
Expand All @@ -43,7 +45,7 @@ abstract class TestRfcConformance[JsValue, Instance <: DiffsonInstance[JsValue]]

def load(path: String): List[ConformanceTest]

def scalatest(t: ConformanceTest) = t match {
def scalatest(t: ConformanceTest) = t match {
case SuccessConformanceTest(doc, patch, Some(expected), comment, Some(true)) =>
ignore(comment.getOrElse(f"$doc patched with $patch")) {
val p = JsonPatch(patch)
Expand Down
Expand Up @@ -33,16 +33,14 @@ class PlayConformance extends TestRfcConformance[JsValue, PlayJsonInstance](play
(JsPath \ "patch").read[JsArray] and
(JsPath \ "expected").readNullable[JsValue] and
(JsPath \ "comment").readNullable[String] and
(JsPath \ "disabled").readNullable[Boolean]
)(SuccessConformanceTest.apply _)
(JsPath \ "disabled").readNullable[Boolean])(SuccessConformanceTest.apply _)

implicit lazy val errorConformanceTestUnmarshaller: Reads[ErrorConformanceTest] = (
(JsPath \ "doc").read[JsValue] and
(JsPath \ "patch").read[JsArray] and
(JsPath \ "error").read[String] and
(JsPath \ "comment").readNullable[String] and
(JsPath \ "disabled").readNullable[Boolean]
)(ErrorConformanceTest.apply _)
(JsPath \ "disabled").readNullable[Boolean])(ErrorConformanceTest.apply _)

implicit lazy val commentConformanceTestUnMarshaller: Reads[CommentConformanceTest] =
(JsPath \ "comment").read[String].map(CommentConformanceTest)
Expand Down
Expand Up @@ -17,12 +17,10 @@ trait TestProtocol {
(JsPath \ "a").write[Int] and
(JsPath \ "b").write[Boolean] and
(JsPath \ "c").write[String] and
(JsPath \ "d").write[List[Int]]
)(unlift(test.Json.unapply))
(JsPath \ "d").write[List[Int]])(unlift(test.Json.unapply))
implicit def testJsonUnmarshaller: Reads[test.Json] = (
(JsPath \ "a").read[Int] and
(JsPath \ "b").read[Boolean] and
(JsPath \ "c").read[String] and
(JsPath \ "d").read[List[Int]]
)(test.Json.apply _)
(JsPath \ "d").read[List[Int]])(test.Json.apply _)
}

0 comments on commit f9705f5

Please sign in to comment.