Last active
August 29, 2015 14:07
-
-
Save gbougeard/86ae26acf7841fb161fb to your computer and use it in GitHub Desktop.
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
def compare = Action.async { | |
implicit request => | |
play.Logger.debug("compare branches") | |
val future: Future[(List[String], List[String])] = for { | |
newRepo <- Gitweb.branches("reservit") | |
oldRepo <- Gitweb.branches("components") | |
} yield (Gitweb.extractBranches(newRepo.body), Gitweb.extractBranches(oldRepo.body)) | |
future.map { case (newRepo, oldRepo) => | |
val migrated: List[String] = newRepo.intersect(oldRepo) | |
val notMigrated: List[String] = oldRepo.diff(newRepo) | |
val onlyNew: List[String] = newRepo.diff(oldRepo) | |
val futures: List[Future[Res]] = notMigrated.map { branch => | |
for { | |
o <- Gerrit.open(branch) | |
m <- Gerrit.merged(branch) | |
} yield Res(branch, o.body, m.body) | |
} | |
Future.sequence(futures).map { | |
results => | |
// I want to return this result but I don't find how :( | |
play.Logger.debug(s"futRes $results") | |
Ok(views.html.git.compare("Comparaison", newRepo, oldRepo, migrated, onlyNew, notMigrated, results)) | |
} | |
// I wish I do not return this one | |
Ok(views.html.git.compare("Comparaison", newRepo, oldRepo, migrated, onlyNew, notMigrated, List())) | |
}.recover { | |
case e: Exception => | |
play.Logger.error(e.getMessage, e) | |
Ok(Json.obj("error" -> e.getMessage)) | |
} | |
} |
You can have a look at https://github.com/scala/async
In my experience, it is easier for dev to use Future with this library.
I explicitely add types but I do not see a Future[Future[xx]]
or maybe the Future.sequence
in the future.map
block is indeed a Future[Future[xx]]
The Future.sequence
should return a Future[Result].
But it is itself inside another Future.map function (https://gist.github.com/gbougeard/86ae26acf7841fb161fb#file-gistfile1-scala-L10)
The first Future.map should be a flatMap
Yes first map
should be flatMap
...
Concerning Async, having studied it a lot, I can say it works but has limitations and I'm not a so big fan of this kind of approach... it depends on your way of developing actually ;)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you have a Future[Future[Result]]. What you need is a Future[Result]
The first
future.map
should be afuture.flatMap