Add Kotlin extensions for function Web API

Issue: SPR-15054
This commit is contained in:
Sebastien Deleuze
2016-12-26 17:43:34 +01:00
parent bb94ba6e3f
commit 3626a1c7f9
5 changed files with 111 additions and 2 deletions

View File

@@ -0,0 +1,39 @@
package org.springframework.web.reactive.function
import org.springframework.http.ReactiveHttpInputMessage
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import kotlin.reflect.KClass
/**
* Extension for [BodyExtactors] providing [KClass] based API and avoiding specifying
* a class parameter when possible thanks to Kotlin reified type parameters.
*
* @since 5.0
*/
object BodyExtractorsExtension {
/**
* @see BodyExtactors.toMono
*/
inline fun <reified T : Any> toMono() : BodyExtractor<Mono<T>, ReactiveHttpInputMessage> =
BodyExtractors.toMono(T::class.java)
/**
* @see BodyExtactors.toMono
*/
fun <T : Any> toMono(elementClass: KClass<T>) : BodyExtractor<Mono<T>, ReactiveHttpInputMessage> =
BodyExtractors.toMono(elementClass.java)
/**
* @see BodyExtactors.toFlux
*/
inline fun <reified T : Any> toFlux() : BodyExtractor<Flux<T>, ReactiveHttpInputMessage> =
BodyExtractors.toFlux(T::class.java)
/**
* @see BodyExtactors.toFlux
*/
fun <T : Any> toFlux(elementClass: KClass<T>) : BodyExtractor<Flux<T>, ReactiveHttpInputMessage> =
BodyExtractors.toFlux(elementClass.java)
}

View File

@@ -0,0 +1,26 @@
package org.springframework.web.reactive.function
import org.reactivestreams.Publisher
import org.springframework.http.ReactiveHttpOutputMessage
import org.springframework.http.server.reactive.ServerHttpResponse
/**
* Extension for [BodyInserters] providing [KClass] based API and avoiding specifying
* a class parameter when possible thanks to Kotlin reified type parameters.
*
* @since 5.0
*/
object BodyInsertersExtension {
/**
* @see BodyInserters.fromPublisher
*/
inline fun <reified T : Publisher<S>, reified S : Any> fromPublisher(publisher: T) : BodyInserter<T, ReactiveHttpOutputMessage> =
BodyInserters.fromPublisher(publisher, S::class.java)
/**
* @see BodyInserters.fromServerSentEvents
*/
inline fun <reified T : Publisher<S>, reified S : Any> fromServerSentEvents(publisher: T) : BodyInserter<T, ServerHttpResponse> =
BodyInserters.fromServerSentEvents(publisher, S::class.java)
}

View File

@@ -0,0 +1,23 @@
package org.springframework.web.reactive.function.client
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import kotlin.reflect.KClass
/**
* Extension for [ClientResponse] providing [KClass] based API.
*
* @since 5.0
*/
object ClientResponseExtension {
/**
* @see ClientResponse.bodyToFlux
*/
fun <T : Any> ClientResponse.bodyToFlux(type: KClass<T>) : Flux<T> = bodyToFlux(type.java)
/**
* @see ClientResponse.bodyToMono
*/
fun <T : Any> ClientResponse.bodyToMono(type: KClass<T>) : Mono<T> = bodyToMono(type.java)
}

View File

@@ -0,0 +1,21 @@
package org.springframework.web.reactive.function.server
import kotlin.reflect.KClass
/**
* Extension for [ServerRequest] providing [KClass] based API.
*
* @since 5.0
*/
object ServerRequestExtension {
/**
* @see ServerRequest.bodyToMono
*/
fun <T : Any> ServerRequest.bodyToMono(type: KClass<T>) = bodyToMono(type.java)
/**
* @see ServerRequest.bodyToFlux
*/
fun <T : Any> ServerRequest.bodyToFlux(type: KClass<T>) = bodyToFlux(type.java)
}