Make RouterFunctionDsl extensible

Issue: SPR-16897
This commit is contained in:
Sebastien Deleuze
2018-06-04 15:33:49 +02:00
parent f1871f9e3f
commit f2ee18493f

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -50,7 +50,7 @@ import reactor.core.publisher.Mono
* @see RouterFunctionDsl
* @since 5.0
*/
fun router(routes: RouterFunctionDsl.() -> Unit) = RouterFunctionDsl().apply(routes).router()
fun router(routes: RouterFunctionDsl.() -> Unit) = RouterFunctionDsl(routes).invoke()
/**
* Provide a [RouterFunction] Kotlin DSL in order to be able to write idiomatic Kotlin code.
@@ -60,7 +60,7 @@ fun router(routes: RouterFunctionDsl.() -> Unit) = RouterFunctionDsl().apply(rou
* @since 5.0
* @see <a href="https://youtrack.jetbrains.com/issue/KT-15667">Kotlin issue about supporting ::foo for member functions</a>
*/
open class RouterFunctionDsl {
open class RouterFunctionDsl(private val init: RouterFunctionDsl.() -> Unit) : () -> RouterFunction<ServerResponse> {
private val routes = mutableListOf<RouterFunction<ServerResponse>>()
@@ -131,8 +131,8 @@ open class RouterFunctionDsl {
* common path (prefix), header, or other request predicate.
* @see RouterFunctions.nest
*/
fun RequestPredicate.nest(r: RouterFunctionDsl.() -> Unit) {
routes += RouterFunctions.nest(this, RouterFunctionDsl().apply(r).router())
fun RequestPredicate.nest(init: RouterFunctionDsl.() -> Unit) {
routes += RouterFunctions.nest(this, RouterFunctionDsl(init).invoke())
}
/**
@@ -143,8 +143,8 @@ open class RouterFunctionDsl {
* @see RouterFunctions.nest
* @see RequestPredicates.path
*/
fun String.nest(r: RouterFunctionDsl.() -> Unit) {
routes += RouterFunctions.nest(path(this), RouterFunctionDsl().apply(r).router())
fun String.nest(init: RouterFunctionDsl.() -> Unit) {
routes += RouterFunctions.nest(path(this), RouterFunctionDsl(init).invoke())
}
/**
@@ -423,9 +423,11 @@ open class RouterFunctionDsl {
/**
* Return a composed routing function created from all the registered routes.
* @since 5.1
*/
internal fun router(): RouterFunction<ServerResponse> {
override fun invoke(): RouterFunction<ServerResponse> {
init()
return routes.reduce(RouterFunction<ServerResponse>::and)
}
}
}