Skip to content

Instantly share code, notes, and snippets.

@aesteve
Last active May 24, 2016 07:02
Show Gist options
  • Save aesteve/d67de76e02a0be6f47d0d30832f026eb to your computer and use it in GitHub Desktop.
Save aesteve/d67de76e02a0be6f47d0d30832f026eb to your computer and use it in GitHub Desktop.
AST transform on AsyncResult methods
// Let's imagin you have such a service
class TodoMongoService {
void fetchById(String todoId, Handler<AsyncResult<Todo>> handler) {
mongo.find(id) { res ->
if(res.succeeded) {
handler.handle(Future.suceededFuture(res.result))
} else {
handler.handle(Future.failedFuture(res.cause))
}
}
}
}
// Through an AST transformation we could extend the class this way :
class TodoMongoService {
// same method as above here...
// plus a generated method ready to be used in a vertx-web context
Handler<RoutingContext> fetchById(String todoId) {
return { ctx ->
findById(todoId) { res ->
if (res.failed) {
ctx.fail(res.cause)
} else {
ctx.yield(res.result)
}
}
}
}
// Because that's a common pattern I found myself a lot of times.
// This way we could write something like that :
router.get('/todos/:id') >> fetchById(id)
// Which "looks like" currying even though that's not really currying
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment