Skip to content

Commit 86804e5

Browse files
committed
Use expression-style declaration for route methods for less verbosity
1 parent 247bc08 commit 86804e5

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

jooby/src/main/kotlin/io/jooby/CoroutineRouter.kt

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package io.jooby
77

8+
import io.jooby.Router.*
89
import kotlinx.coroutines.*
910
import kotlin.coroutines.CoroutineContext
1011

@@ -22,56 +23,46 @@ class CoroutineRouter(val coroutineStart: CoroutineStart, val router: Router) {
2223
}
2324

2425
@RouterDsl
25-
fun get(pattern: String, handler: suspend HandlerContext.() -> Any): Route {
26-
return route(Router.GET, pattern, handler)
27-
}
26+
fun get(pattern: String, handler: suspend HandlerContext.() -> Any) =
27+
route(GET, pattern, handler)
2828

2929
@RouterDsl
30-
fun post(pattern: String, handler: suspend HandlerContext.() -> Any): Route {
31-
return route(Router.POST, pattern, handler)
32-
}
30+
fun post(pattern: String, handler: suspend HandlerContext.() -> Any) =
31+
route(POST, pattern, handler)
3332

3433
@RouterDsl
35-
fun put(pattern: String, handler: suspend HandlerContext.() -> Any): Route {
36-
return route(Router.PUT, pattern, handler)
37-
}
34+
fun put(pattern: String, handler: suspend HandlerContext.() -> Any) =
35+
route(PUT, pattern, handler)
3836

3937
@RouterDsl
40-
fun delete(pattern: String, handler: suspend HandlerContext.() -> Any): Route {
41-
return route(Router.DELETE, pattern, handler)
42-
}
38+
fun delete(pattern: String, handler: suspend HandlerContext.() -> Any) =
39+
route(DELETE, pattern, handler)
4340

4441
@RouterDsl
45-
fun patch(pattern: String, handler: suspend HandlerContext.() -> Any): Route {
46-
return route(Router.PATCH, pattern, handler)
47-
}
42+
fun patch(pattern: String, handler: suspend HandlerContext.() -> Any) =
43+
route(PATCH, pattern, handler)
4844

4945
@RouterDsl
50-
fun head(pattern: String, handler: suspend HandlerContext.() -> Any): Route {
51-
return route(Router.HEAD, pattern, handler)
52-
}
46+
fun head(pattern: String, handler: suspend HandlerContext.() -> Any) =
47+
route(HEAD, pattern, handler)
5348

5449
@RouterDsl
55-
fun trace(pattern: String, handler: suspend HandlerContext.() -> Any): Route {
56-
return route(Router.TRACE, pattern, handler)
57-
}
50+
fun trace(pattern: String, handler: suspend HandlerContext.() -> Any) =
51+
route(TRACE, pattern, handler)
5852

5953
@RouterDsl
60-
fun options(pattern: String, handler: suspend HandlerContext.() -> Any): Route {
61-
return route(Router.OPTIONS, pattern, handler)
62-
}
54+
fun options(pattern: String, handler: suspend HandlerContext.() -> Any) =
55+
route(OPTIONS, pattern, handler)
6356

64-
fun route(method: String, pattern: String, handler: suspend HandlerContext.() -> Any): Route {
65-
return router.route(method, pattern) { ctx ->
57+
fun route(method: String, pattern: String, handler: suspend HandlerContext.() -> Any): Route =
58+
router.route(method, pattern) { ctx ->
6659
launch(ctx) {
6760
val result = handler(HandlerContext(ctx))
6861
if (result != ctx) {
6962
ctx.render(result)
7063
}
7164
}
72-
}.setHandle(handler)
73-
.attribute("coroutine", true)
74-
}
65+
}.setHandle(handler).attribute("coroutine", true)
7566

7667
internal fun launch(ctx: Context, block: suspend CoroutineScope.() -> Unit) {
7768
val exceptionHandler = CoroutineExceptionHandler { _, x -> ctx.sendError(x) }

0 commit comments

Comments
 (0)