Skip to content

Instantly share code, notes, and snippets.

@jroper
Created October 17, 2013 04:44
Show Gist options
  • Select an option

  • Save jroper/7019306 to your computer and use it in GitHub Desktop.

Select an option

Save jroper/7019306 to your computer and use it in GitHub Desktop.
Reads for recursive search paths
// Reads for recursive path
def recursiveSearchReads[T : Reads](path: JsPath) = Reads[Seq[T]] { json =>
path.apply(json).map(_.validate[T]).foldLeft[JsResult[Seq[T]]](JsSuccess(Nil)) {
case (JsError(a), JsError(b)) => JsError(a ++ b)
case (err: JsError, _) => err
case (_, err: JsError) => err
case (JsSuccess(ts, _), JsSuccess(t, _)) => JsSuccess(ts :+ t)
}.repath(path)
}
// Reads for recursive path of arrays, flattened
def recursiveSearchReadsFlatten[T : Reads](path: JsPath) = Reads[Seq[T]] { json =>
path.apply(json).map(_.validate[Seq[T]]).reduceLeft[JsResult[Seq[T]]] {
case (JsError(a), JsError(b)) => JsError(a ++ b)
case (err: JsError, _) => err
case (_, err: JsError) => err
case (JsSuccess(a, _), JsSuccess(b, _)) => JsSuccess(a ++ b)
}.repath(path)
}
@mandubian
Copy link
Copy Markdown

Actually we don't have it because nobody asked for it IMHO :D

The equivalent of Future.sequence for JsResult would be cool too.
I thought I had written it somewhere but I don't find it anymore!

I also believe some Reads based on JsZipper would be far more powerful...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment