Browse Source

Polish Kotlin Web functional DSL

Issue: SPR-15065
pull/1288/merge
Sebastien Deleuze 9 years ago
parent
commit
be45de0571
  1. 62
      spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensions.kt
  2. 30
      spring-web-reactive/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt

62
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) { * fun route(request: ServerRequest) = route(request) {
* accept(TEXT_HTML).apply { * accept(TEXT_HTML).apply {
* (GET("/user/") or GET("/users/")) { findAllView() } * (GET("/user/") or GET("/users/")) { findAllView() }
* GET("/user/{login}") { findViewById() } * GET("/user/{login}", ::findViewById)
* } * }
* accept(APPLICATION_JSON).apply { * accept(APPLICATION_JSON).apply {
* (GET("/api/user/") or GET("/api/users/")) { findAll() } * (GET("/api/user/") or GET("/api/users/")) { findAll() }
* POST("/api/user/") { create() } * POST("/api/user/", ::create)
* POST("/api/user/{login}") { findOne() } * POST("/api/user/{login}", ::findOne)
* } * }
* } * }
* ``` * ```
@ -35,62 +35,62 @@ class RouterDsl {
val children = mutableListOf<RouterDsl>() val children = mutableListOf<RouterDsl>()
val routes = mutableListOf<RouterFunction<ServerResponse>>() val routes = mutableListOf<RouterFunction<ServerResponse>>()
operator fun RequestPredicate.invoke(f: () -> HandlerFunction<ServerResponse>) {
routes += RouterFunctions.route(this, f())
}
infix fun RequestPredicate.and(other: RequestPredicate): RequestPredicate = this.and(other) infix fun RequestPredicate.and(other: RequestPredicate): RequestPredicate = this.and(other)
infix fun RequestPredicate.or(other: RequestPredicate): RequestPredicate = this.or(other) infix fun RequestPredicate.or(other: RequestPredicate): RequestPredicate = this.or(other)
operator fun RequestPredicate.not(): RequestPredicate = this.negate() operator fun RequestPredicate.not(): RequestPredicate = this.negate()
fun GET(pattern: String, f: () -> HandlerFunction<ServerResponse>) { operator fun RequestPredicate.invoke(f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.GET(pattern), f()) routes += RouterFunctions.route(this, HandlerFunction { f(it) })
}
fun GET(pattern: String, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.GET(pattern), HandlerFunction { f(it) } )
} }
fun HEAD(pattern: String, f: () -> HandlerFunction<ServerResponse>) { fun HEAD(pattern: String, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.HEAD(pattern), f()) routes += RouterFunctions.route(RequestPredicates.HEAD(pattern), HandlerFunction { f(it) })
} }
fun POST(pattern: String, f: () -> HandlerFunction<ServerResponse>) { fun POST(pattern: String, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.POST(pattern), f()) routes += RouterFunctions.route(RequestPredicates.POST(pattern), HandlerFunction { f(it) })
} }
fun PUT(pattern: String, f: () -> HandlerFunction<ServerResponse>) { fun PUT(pattern: String, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.PUT(pattern), f()) routes += RouterFunctions.route(RequestPredicates.PUT(pattern), HandlerFunction { f(it) })
} }
fun PATCH(pattern: String, f: () -> HandlerFunction<ServerResponse>) { fun PATCH(pattern: String, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.PATCH(pattern), f()) routes += RouterFunctions.route(RequestPredicates.PATCH(pattern), HandlerFunction { f(it) })
} }
fun DELETE(pattern: String, f: () -> HandlerFunction<ServerResponse>) { fun DELETE(pattern: String, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.DELETE(pattern), f()) routes += RouterFunctions.route(RequestPredicates.DELETE(pattern), HandlerFunction { f(it) })
} }
fun OPTIONS(pattern: String, f: () -> HandlerFunction<ServerResponse>) { fun OPTIONS(pattern: String, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.OPTIONS(pattern), f()) routes += RouterFunctions.route(RequestPredicates.OPTIONS(pattern), HandlerFunction { f(it) })
} }
fun accept(vararg mediaType: MediaType, f: () -> HandlerFunction<ServerResponse>) { fun accept(mediaType: MediaType, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.accept(*mediaType), f()) routes += RouterFunctions.route(RequestPredicates.accept(mediaType), HandlerFunction { f(it) })
} }
fun contentType(vararg mediaType: MediaType, f: () -> HandlerFunction<ServerResponse>) { fun contentType(mediaType: MediaType, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.contentType(*mediaType), f()) routes += RouterFunctions.route(RequestPredicates.contentType(mediaType), HandlerFunction { f(it) })
} }
fun headers(headerPredicate: (ServerRequest.Headers)->Boolean, f: () -> HandlerFunction<ServerResponse>) { fun headers(headerPredicate: (ServerRequest.Headers)->Boolean, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.headers(headerPredicate), f()) routes += RouterFunctions.route(RequestPredicates.headers(headerPredicate), HandlerFunction { f(it) })
} }
fun method(httpMethod: HttpMethod, f: () -> HandlerFunction<ServerResponse>) { fun method(httpMethod: HttpMethod, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.method(httpMethod), f()) routes += RouterFunctions.route(RequestPredicates.method(httpMethod), HandlerFunction { f(it) })
} }
fun path(pattern: String, f: () -> HandlerFunction<ServerResponse>) { fun path(pattern: String, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.path(pattern), f()) routes += RouterFunctions.route(RequestPredicates.path(pattern), HandlerFunction { f(it) })
} }
fun resources(path: String, location: Resource) { fun resources(path: String, location: Resource) {

30
spring-web-reactive/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt

@ -92,23 +92,22 @@ class RouterFunctionExtensionsTests {
.verifyComplete() .verifyComplete()
} }
class FooController : RouterFunction<ServerResponse> { class FooController : RouterFunction<ServerResponse> {
override fun route(request: ServerRequest) = route(request) { override fun route(req: ServerRequest) = route(req) {
(GET("/foo/") or GET("/foos/")) { handle() } (GET("/foo/") or GET("/foos/")) { handle(req) }
accept(APPLICATION_JSON).apply { accept(APPLICATION_JSON).apply {
POST("/api/foo/") { handle() } POST("/api/foo/", ::handle)
PUT("/api/foo/") { handle() } PUT("/api/foo/", ::handle)
DELETE("/api/foo/") { handle() } DELETE("/api/foo/", ::handle)
} }
accept(APPLICATION_ATOM_XML) { handle() } accept(APPLICATION_ATOM_XML, ::handle)
contentType(APPLICATION_OCTET_STREAM) { handle() } contentType(APPLICATION_OCTET_STREAM) { handle(req) }
method(HttpMethod.PATCH) { handle() } method(HttpMethod.PATCH) { handle(req) }
headers({ it.accept().contains(APPLICATION_JSON) }).apply { 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/**", resources("/org/springframework/web/reactive/function/**",
ClassPathResource("/org/springframework/web/reactive/function/response.txt")) ClassPathResource("/org/springframework/web/reactive/function/response.txt"))
resources { resources {
@ -119,11 +118,10 @@ class RouterFunctionExtensionsTests {
Mono.empty() Mono.empty()
} }
} }
path("/baz") { handle() } path("/baz") { handle(req) }
} }
fun handle() = HandlerFunction { ok().build() }
} }
} }
private fun handle(req: ServerRequest) = ok().build()

Loading…
Cancel
Save