diff --git a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/CoRouterFunctionDsl.kt b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/CoRouterFunctionDsl.kt index 3a861daf9d..5beed8e41c 100644 --- a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/CoRouterFunctionDsl.kt +++ b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/CoRouterFunctionDsl.kt @@ -26,13 +26,22 @@ import org.springframework.http.MediaType import org.springframework.web.reactive.function.server.RouterFunctions.nest import java.net.URI +/** + * Coroutines variant of [router]. + * + * @author Sebastien Deleuze + * @since 5.2 + */ +fun coRouter(routes: (CoRouterFunctionDsl.() -> Unit)) = + CoRouterFunctionDsl(routes).build() + /** * Coroutines variant of [RouterFunctionDsl]. * * @author Sebastien Deleuze * @since 5.2 */ -open class CoRouterFunctionDsl(private val init: (CoRouterFunctionDsl.() -> Unit)): () -> RouterFunction { +class CoRouterFunctionDsl(private val init: (CoRouterFunctionDsl.() -> Unit)) { private val builder = RouterFunctions.route() @@ -104,7 +113,7 @@ open class CoRouterFunctionDsl(private val init: (CoRouterFunctionDsl.() -> Unit * @see RouterFunctions.nest */ fun RequestPredicate.nest(r: (CoRouterFunctionDsl.() -> Unit)) { - builder.add(nest(this, CoRouterFunctionDsl(r).invoke())) + builder.add(nest(this, CoRouterFunctionDsl(r).build())) } @@ -396,10 +405,10 @@ open class CoRouterFunctionDsl(private val init: (CoRouterFunctionDsl.() -> Unit } } - /** - * Return a composed routing function created from all the registered routes. - */ - override fun invoke(): RouterFunction { + /** + * Return a composed routing function created from all the registered routes. + */ + internal fun build(): RouterFunction { init() return builder.build() } @@ -484,12 +493,3 @@ open class CoRouterFunctionDsl(private val init: (CoRouterFunctionDsl.() -> Unit */ operator fun RouterFunction.plus(other: RouterFunction) = this.and(other) - -/** - * Coroutines variant of [router]. - * - * @author Sebastien Deleuze - * @since 5.2 - */ -fun coRouter(routes: (CoRouterFunctionDsl.() -> Unit)) = - CoRouterFunctionDsl(routes).invoke() \ No newline at end of file diff --git a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDsl.kt b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDsl.kt index f2b5b7ba66..e85715c010 100644 --- a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDsl.kt +++ b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDsl.kt @@ -26,8 +26,8 @@ import java.net.URI import java.util.function.Supplier /** - * Allow to create easily a `RouterFunction` from a Kotlin router DSL based - * on the same building blocks as the Java one ([RouterFunction], [RequestPredicate], + * Allow to create easily a WebFlux.fn `RouterFunction` from a Kotlin + * router DSL leveraging WebFlux.fn Java API ([RouterFunction], [RequestPredicate], * [HandlerFunction]). * * Example: @@ -55,7 +55,7 @@ import java.util.function.Supplier * @see RouterFunctions.Builder * @since 5.0 */ -fun router(routes: RouterFunctionDsl.() -> Unit) = RouterFunctionDsl(routes).invoke() +fun router(routes: RouterFunctionDsl.() -> Unit) = RouterFunctionDsl(routes).build() /** * Provide a [RouterFunction] Kotlin DSL in order to be able to write idiomatic Kotlin code. @@ -64,7 +64,7 @@ fun router(routes: RouterFunctionDsl.() -> Unit) = RouterFunctionDsl(routes).inv * @author Yevhenii Melnyk * @since 5.0 */ -open class RouterFunctionDsl(private val init: RouterFunctionDsl.() -> Unit) : () -> RouterFunction { +class RouterFunctionDsl(private val init: RouterFunctionDsl.() -> Unit) { private val builder = RouterFunctions.route() @@ -136,7 +136,7 @@ open class RouterFunctionDsl(private val init: RouterFunctionDsl.() -> Unit) : ( * @see RouterFunctions.nest */ fun RequestPredicate.nest(init: RouterFunctionDsl.() -> Unit) { - builder.nest(this, Supplier { RouterFunctionDsl(init).invoke() }) + builder.nest(this, Supplier { RouterFunctionDsl(init).build() }) } /** @@ -148,7 +148,7 @@ open class RouterFunctionDsl(private val init: RouterFunctionDsl.() -> Unit) : ( * @see RequestPredicates.path */ fun String.nest(init: RouterFunctionDsl.() -> Unit) { - builder.path(this, Supplier { RouterFunctionDsl(init).invoke() }) + builder.path(this, Supplier { RouterFunctionDsl(init).build() }) } /** @@ -545,7 +545,7 @@ open class RouterFunctionDsl(private val init: RouterFunctionDsl.() -> Unit) : ( * Return a composed routing function created from all the registered routes. * @since 5.1 */ - override fun invoke(): RouterFunction { + internal fun build(): RouterFunction { init() return builder.build() }