Hi Johannes, here is some code to illustrate the task i was trying to achieve:
so i provide a list of lenses along with an identifier that is metadata for the "processing function"
I extract all of the strings that the lenses refer to and gather them into a list, process them (which returns back the processed strings as a list) and then want to replace the results back in the matching places i got them from.
now if there was a way to "iterate" through a Lens[Seq] i could just apply them in the order i have them, rather than this non ideal side effecting hack.
is it possible to do this?
cheers
Rich
def processLenses(sourceJson: String, metadata: List[(Lens[Seq], String)], processingFunction: List[(String, String)] => Future[String])(implicit ec: ExecutionContext): Future[String] = {
val jsonAst = sourceJson.parseJson
val tokenData = metadata.flatMap { case (lens, policy) => jsonAst.extract[String](lens) map { (_, policy)} }
processingFunction(tokenData).map { results =>
val mutableStack = mutable.Stack() ++ results
val sinkJson = metadata.foldLeft(jsonAst) { case (newJsonTree, (lens, result)) =>
newJsonTree.update(lens ! modify[String](_ => mutableStack.pop)).asJsObject
}
sinkJson.prettyPrint
}
}