Skip to content

Instantly share code, notes, and snippets.

@gbougeard
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbougeard/86ae26acf7841fb161fb to your computer and use it in GitHub Desktop.
Save gbougeard/86ae26acf7841fb161fb to your computer and use it in GitHub Desktop.
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))
}
}
@yanns
Copy link

yanns commented Oct 10, 2014

I think you have a Future[Future[Result]]. What you need is a Future[Result]
The first future.map should be a future.flatMap

@yanns
Copy link

yanns commented Oct 10, 2014

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.

@gbougeard
Copy link
Author

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]]

@yanns
Copy link

yanns commented Oct 10, 2014

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

@mandubian
Copy link

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