Created
October 17, 2013 04:44
-
-
Save jroper/7019306 to your computer and use it in GitHub Desktop.
Reads for recursive search paths
This file contains 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
// 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) | |
} |
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
Reads for recursive paths... perhaps we should add these methods to JsPath, currently, you can't:
But with this you can:
Something else that would be good is a simple way to
Seq[JsResult[T]] => JsResult[Seq[T]]
, in case the above was not powerful enough for you, you could use that method to implement the above in far less code.