Last active
August 29, 2015 14:09
-
-
Save dgouyette/814dd4da109c6355d2a8 to your computer and use it in GitHub Desktop.
filterTransform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val objTransformer = | |
(__ \ "_embedded" \ "contract").json.copyFrom( | |
(__ \ "contract").readNullable[JsArray].map(_.getOrElse(JsArray())) | |
) and | |
(__ \ "_embedded" \ "comment").json.copyFrom( | |
(__ \ "comment").readNullable[Comment].filter(reviewOpt => isApproved(reviewOpt)) | |
) | |
(__ \ "title").readNullable[String].map(_.fold(JsObject(Nil))(s => Json.obj("title" -> s))) | |
def isApproved(commentOpt : Option[Comment]) = { | |
(for { | |
comment <- commentOpt | |
status <- comment.status | |
} yield { | |
status==Status.approved | |
}).getOrElse(false) | |
} |
def isApproved(review:Option[Review]) = reviewt.isDefined && review.get.status == Status.approved
plus simple déjà, non?
Je n'ai pas encore réfactoré, j'essaye de faire fonctionner avant de simplifier
T'as tenté ça?
(__ \ "contract").readNullable[JsArray] orElse Reads.pure(JsArray())
contract est optional et il doit resortir dans le json si contract est présent et que son statut est approuvé
J'ai tenté un truc comme çà également :
Reads.verifyingIf((obj: JsObject) => (obj \ "comment" \ "status").as[String].equals("approved"))(extractComment)
def extractComment: Reads[JsObject] = {
(__ \ "username").json.prune
(__ \ "timestamps").json.prune
}
mais ça ne marche pas avec le orElse
?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Le top serait de ne pas avoir à faire readNullable[Review] mais de pouvoir faire une condition avec du jsPath