From be45de0571fbcd2340d864988b9351df3628f471 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Tue, 17 Jan 2017 23:04:37 +0100 Subject: [PATCH] Polish Kotlin Web functional DSL Issue: SPR-15065 --- .../server/RouterFunctionExtensions.kt | 62 +++++++++---------- .../server/RouterFunctionExtensionsTests.kt | 30 +++++---- 2 files changed, 45 insertions(+), 47 deletions(-) diff --git a/spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensions.kt b/spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensions.kt index 5f54f916903..825c5256944 100644 --- a/spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensions.kt +++ b/spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensions.kt @@ -13,12 +13,12 @@ import reactor.core.publisher.Mono * fun route(request: ServerRequest) = route(request) { * accept(TEXT_HTML).apply { * (GET("/user/") or GET("/users/")) { findAllView() } - * GET("/user/{login}") { findViewById() } + * GET("/user/{login}", ::findViewById) * } * accept(APPLICATION_JSON).apply { * (GET("/api/user/") or GET("/api/users/")) { findAll() } - * POST("/api/user/") { create() } - * POST("/api/user/{login}") { findOne() } + * POST("/api/user/", ::create) + * POST("/api/user/{login}", ::findOne) * } * } * ``` @@ -35,62 +35,62 @@ class RouterDsl { val children = mutableListOf() val routes = mutableListOf>() - operator fun RequestPredicate.invoke(f: () -> HandlerFunction) { - routes += RouterFunctions.route(this, f()) - } - infix fun RequestPredicate.and(other: RequestPredicate): RequestPredicate = this.and(other) infix fun RequestPredicate.or(other: RequestPredicate): RequestPredicate = this.or(other) operator fun RequestPredicate.not(): RequestPredicate = this.negate() - fun GET(pattern: String, f: () -> HandlerFunction) { - routes += RouterFunctions.route(RequestPredicates.GET(pattern), f()) + operator fun RequestPredicate.invoke(f: (ServerRequest) -> Mono) { + routes += RouterFunctions.route(this, HandlerFunction { f(it) }) + } + + fun GET(pattern: String, f: (ServerRequest) -> Mono) { + routes += RouterFunctions.route(RequestPredicates.GET(pattern), HandlerFunction { f(it) } ) } - fun HEAD(pattern: String, f: () -> HandlerFunction) { - routes += RouterFunctions.route(RequestPredicates.HEAD(pattern), f()) + fun HEAD(pattern: String, f: (ServerRequest) -> Mono) { + routes += RouterFunctions.route(RequestPredicates.HEAD(pattern), HandlerFunction { f(it) }) } - fun POST(pattern: String, f: () -> HandlerFunction) { - routes += RouterFunctions.route(RequestPredicates.POST(pattern), f()) + fun POST(pattern: String, f: (ServerRequest) -> Mono) { + routes += RouterFunctions.route(RequestPredicates.POST(pattern), HandlerFunction { f(it) }) } - fun PUT(pattern: String, f: () -> HandlerFunction) { - routes += RouterFunctions.route(RequestPredicates.PUT(pattern), f()) + fun PUT(pattern: String, f: (ServerRequest) -> Mono) { + routes += RouterFunctions.route(RequestPredicates.PUT(pattern), HandlerFunction { f(it) }) } - fun PATCH(pattern: String, f: () -> HandlerFunction) { - routes += RouterFunctions.route(RequestPredicates.PATCH(pattern), f()) + fun PATCH(pattern: String, f: (ServerRequest) -> Mono) { + routes += RouterFunctions.route(RequestPredicates.PATCH(pattern), HandlerFunction { f(it) }) } - fun DELETE(pattern: String, f: () -> HandlerFunction) { - routes += RouterFunctions.route(RequestPredicates.DELETE(pattern), f()) + fun DELETE(pattern: String, f: (ServerRequest) -> Mono) { + routes += RouterFunctions.route(RequestPredicates.DELETE(pattern), HandlerFunction { f(it) }) } - fun OPTIONS(pattern: String, f: () -> HandlerFunction) { - routes += RouterFunctions.route(RequestPredicates.OPTIONS(pattern), f()) + fun OPTIONS(pattern: String, f: (ServerRequest) -> Mono) { + routes += RouterFunctions.route(RequestPredicates.OPTIONS(pattern), HandlerFunction { f(it) }) } - fun accept(vararg mediaType: MediaType, f: () -> HandlerFunction) { - routes += RouterFunctions.route(RequestPredicates.accept(*mediaType), f()) + fun accept(mediaType: MediaType, f: (ServerRequest) -> Mono) { + routes += RouterFunctions.route(RequestPredicates.accept(mediaType), HandlerFunction { f(it) }) } - fun contentType(vararg mediaType: MediaType, f: () -> HandlerFunction) { - routes += RouterFunctions.route(RequestPredicates.contentType(*mediaType), f()) + fun contentType(mediaType: MediaType, f: (ServerRequest) -> Mono) { + routes += RouterFunctions.route(RequestPredicates.contentType(mediaType), HandlerFunction { f(it) }) } - fun headers(headerPredicate: (ServerRequest.Headers)->Boolean, f: () -> HandlerFunction) { - routes += RouterFunctions.route(RequestPredicates.headers(headerPredicate), f()) + fun headers(headerPredicate: (ServerRequest.Headers)->Boolean, f: (ServerRequest) -> Mono) { + routes += RouterFunctions.route(RequestPredicates.headers(headerPredicate), HandlerFunction { f(it) }) } - fun method(httpMethod: HttpMethod, f: () -> HandlerFunction) { - routes += RouterFunctions.route(RequestPredicates.method(httpMethod), f()) + fun method(httpMethod: HttpMethod, f: (ServerRequest) -> Mono) { + routes += RouterFunctions.route(RequestPredicates.method(httpMethod), HandlerFunction { f(it) }) } - fun path(pattern: String, f: () -> HandlerFunction) { - routes += RouterFunctions.route(RequestPredicates.path(pattern), f()) + fun path(pattern: String, f: (ServerRequest) -> Mono) { + routes += RouterFunctions.route(RequestPredicates.path(pattern), HandlerFunction { f(it) }) } fun resources(path: String, location: Resource) { diff --git a/spring-web-reactive/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt b/spring-web-reactive/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt index 0c226c558ce..f8995fc5e2e 100644 --- a/spring-web-reactive/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt +++ b/spring-web-reactive/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt @@ -92,23 +92,22 @@ class RouterFunctionExtensionsTests { .verifyComplete() } - class FooController : RouterFunction { - override fun route(request: ServerRequest) = route(request) { - (GET("/foo/") or GET("/foos/")) { handle() } + override fun route(req: ServerRequest) = route(req) { + (GET("/foo/") or GET("/foos/")) { handle(req) } accept(APPLICATION_JSON).apply { - POST("/api/foo/") { handle() } - PUT("/api/foo/") { handle() } - DELETE("/api/foo/") { handle() } + POST("/api/foo/", ::handle) + PUT("/api/foo/", ::handle) + DELETE("/api/foo/", ::handle) } - accept(APPLICATION_ATOM_XML) { handle() } - contentType(APPLICATION_OCTET_STREAM) { handle() } - method(HttpMethod.PATCH) { handle() } + accept(APPLICATION_ATOM_XML, ::handle) + contentType(APPLICATION_OCTET_STREAM) { handle(req) } + method(HttpMethod.PATCH) { handle(req) } headers({ it.accept().contains(APPLICATION_JSON) }).apply { - GET("/api/foo/") { handle() } + GET("/api/foo/", ::handle) } - headers({ it.header("bar").isNotEmpty() }) { handle() } + headers({ it.header("bar").isNotEmpty() }, ::handle) resources("/org/springframework/web/reactive/function/**", ClassPathResource("/org/springframework/web/reactive/function/response.txt")) resources { @@ -119,11 +118,10 @@ class RouterFunctionExtensionsTests { Mono.empty() } } - path("/baz") { handle() } + path("/baz") { handle(req) } } - - fun handle() = HandlerFunction { ok().build() } - } - } + +private fun handle(req: ServerRequest) = ok().build() +