Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.