Skip to content
This repository has been archived by the owner on Jun 15, 2020. It is now read-only.

Commit

Permalink
Change (__ \ "path").format to match the .read and .write behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierBlanvillain committed Aug 13, 2015
1 parent 73c268f commit a40c528
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,11 @@ case class Formatter[IR, IW](path: Path = Path(Nil)) {
Format[IR, IW, O](Reader(path).read(subR), Writer(path).write(subW))
}

def format[J, O](subR: => RuleLike[J, O])(implicit r: Path => RuleLike[IR, J], w: Path => WriteLike[O, IW]): Format[IR, IW, O] =
format(subR, Write.zero[O])
def format[JJ, J, O](subF: => Format[JJ, J, O])(implicit r: Path => RuleLike[IR, JJ], w: Path => WriteLike[J, IW]): Format[IR, IW, O] =
format(subF, subF)

// def format[J, O](subR: => RuleLike[J, O])(implicit r: Path => RuleLike[IR, J], w: Path => WriteLike[O, IW]): Format[IR, IW, O] =
// format(subR, Write.zero[O])

// def format[JJ, O](subW: => WriteLike[O, JJ])(implicit r: Path => RuleLike[I, O], w: Path => WriteLike[JJ, I]): Format[I, O] =
// format(Rule.zero[O], subW)
Expand All @@ -165,4 +168,4 @@ case class Formatter[IR, IW](path: Path = Path(Nil)) {
def \(key: String): Formatter[IR, IW] = Formatter(path \ key)
def \(idx: Int): Formatter[IR, IW] = Formatter(path \ idx)
def \(child: PathNode): Formatter[IR, IW] = Formatter(path \ child)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ object FormatSpec extends Specification {
import Writes._

val f = Formatting[UrlFormEncoded, UrlFormEncoded] { __ =>
((__ \ "firstname").format(notEmpty) ~
(__ \ "lastname").format(notEmpty)).tupled
((__ \ "firstname").format(notEmpty, Write.zero[String]) ~
(__ \ "lastname").format(notEmpty, Write.zero[String])).tupled
}

val valid = Map(
Expand Down Expand Up @@ -300,7 +300,7 @@ object FormatSpec extends Specification {

Formatting[UrlFormEncoded, UrlFormEncoded] { __ => (__ \ "firstname").format[Seq[String]] }.validate(valid) mustEqual(Success(Seq("Julien")))
Formatting[UrlFormEncoded, UrlFormEncoded] { __ => (__ \ "foobar").format[Seq[String]] }.validate(valid) mustEqual(Success(Seq()))
Formatting[UrlFormEncoded, UrlFormEncoded] { __ => (__ \ "foobar").format(isNotEmpty[Seq[Int]]) }.validate(valid) mustEqual(Failure(Seq(Path \ "foobar" -> Seq(ValidationError("error.notEmpty")))))
Formatting[UrlFormEncoded, UrlFormEncoded] { __ => (__ \ "foobar").format(isNotEmpty[Seq[Int]], Write.zero[Seq[Int]]) }.validate(valid) mustEqual(Failure(Seq(Path \ "foobar" -> Seq(ValidationError("error.notEmpty")))))
}

"format recursive" in {
Expand Down Expand Up @@ -361,4 +361,4 @@ object FormatSpec extends Specification {

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ object FormatSpec extends Specification {
import Writes._

val f = Formatting[JsValue, JsObject] { __ =>
((__ \ "firstname").format(notEmpty) ~
(__ \ "lastname").format(notEmpty)).tupled
((__ \ "firstname").format(notEmpty, Write.zero[String]) ~
(__ \ "lastname").format(notEmpty, Write.zero[String])).tupled
}

val valid = Json.obj(
Expand Down Expand Up @@ -299,7 +299,7 @@ object FormatSpec extends Specification {

Formatting[JsValue, JsObject] { __ => (__ \ "firstname").format[Seq[String]] }.validate(valid) mustEqual(Success(Seq("Julien")))
Formatting[JsValue, JsObject] { __ => (__ \ "foobar").format[Seq[String]] }.validate(valid) mustEqual(Success(Seq()))
Formatting[JsValue, JsObject] { __ => (__ \ "foobar").format(isNotEmpty[Seq[Int]]) }.validate(valid) mustEqual(Failure(Seq(Path \ "foobar" -> Seq(ValidationError("error.notEmpty")))))
Formatting[JsValue, JsObject] { __ => (__ \ "foobar").format(isNotEmpty[Seq[Int]], Write.zero[Seq[Int]]) }.validate(valid) mustEqual(Failure(Seq(Path \ "foobar" -> Seq(ValidationError("error.notEmpty")))))
}

"format recursive" in {
Expand Down Expand Up @@ -382,7 +382,38 @@ object FormatSpec extends Specification {
}
win.writes(luigi) mustEqual(m2)
}

"be composable with other format" in {
import Rules._
import Writes._

val userF = Formatting[JsValue, JsObject] { __ =>
((__ \ "id").format[Long] ~
(__ \ "name").format[String])(User.apply _, unlift(User.unapply _))
}

case class Family(father: User, mother: User)

val familyF = Formatting[JsValue, JsObject] { __ =>
(
(__ \ "father").format(userF) ~
(__ \ "mother").format(userF)
)(Family.apply _, unlift(Family.unapply _))
}

val nintendo = Family(luigi, User(2, "peach"))
familyF.validate(familyF.writes(nintendo)) mustEqual(Success(nintendo))

case class UserAndFamily(user: User, family: Family)

val userAndFamilyF = Formatting[JsValue, JsObject] { __ =>
(userF ~ familyF)(UserAndFamily.apply _, unlift(UserAndFamily.unapply _))
}

val mario = UserAndFamily(User(3, "mario"), nintendo)
userAndFamilyF.validate(userAndFamilyF.writes(mario)) mustEqual(Success(mario))
}

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ object FormatSpec extends Specification {
import Writes._

val f = Formatting[JValue, JObject] { __ =>
((__ \ "firstname").format(notEmpty) ~
(__ \ "lastname").format(notEmpty)).tupled
((__ \ "firstname").format(notEmpty, Write.zero[String]) ~
(__ \ "lastname").format(notEmpty, Write.zero[String])).tupled
}

val valid = JObject(
Expand Down Expand Up @@ -300,7 +300,7 @@ object FormatSpec extends Specification {

Formatting[JValue, JObject] { __ => (__ \ "firstname").format[Seq[String]] }.validate(valid) mustEqual(Success(Seq("Julien")))
Formatting[JValue, JObject] { __ => (__ \ "foobar").format[Seq[String]] }.validate(valid) mustEqual(Success(Seq()))
Formatting[JValue, JObject] { __ => (__ \ "foobar").format(isNotEmpty[Seq[Int]]) }.validate(valid) mustEqual(Failure(Seq(Path \ "foobar" -> Seq(ValidationError("error.notEmpty")))))
Formatting[JValue, JObject] { __ => (__ \ "foobar").format(isNotEmpty[Seq[Int]], Write.zero[Seq[Int]]) }.validate(valid) mustEqual(Failure(Seq(Path \ "foobar" -> Seq(ValidationError("error.notEmpty")))))
}

"format recursive" in {
Expand Down Expand Up @@ -386,4 +386,4 @@ object FormatSpec extends Specification {

}

}
}

0 comments on commit a40c528

Please sign in to comment.