Skip to content

Instantly share code, notes, and snippets.

@dgouyette
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save dgouyette/814dd4da109c6355d2a8 to your computer and use it in GitHub Desktop.

Select an option

Save dgouyette/814dd4da109c6355d2a8 to your computer and use it in GitHub Desktop.
filterTransform
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)
}
@dgouyette
Copy link
Copy Markdown
Author

Le top serait de ne pas avoir à faire readNullable[Review] mais de pouvoir faire une condition avec du jsPath

@gbougeard
Copy link
Copy Markdown

def isApproved(review:Option[Review]) = reviewt.isDefined && review.get.status == Status.approved 

plus simple déjà, non?

@dgouyette
Copy link
Copy Markdown
Author

Je n'ai pas encore réfactoré, j'essaye de faire fonctionner avant de simplifier

@mandubian
Copy link
Copy Markdown

T'as tenté ça?
(__ \ "contract").readNullable[JsArray] orElse Reads.pure(JsArray())

@dgouyette
Copy link
Copy Markdown
Author

contract est optional et il doit resortir dans le json si contract est présent et que son statut est approuvé

@dgouyette
Copy link
Copy Markdown
Author

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
}

@mandubian
Copy link
Copy Markdown

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