@@ -19,6 +19,8 @@ package io.moia.router
1919import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
2020import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent.ProxyRequestContext
2121import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent
22+ import kotlin.reflect.KType
23+ import kotlin.reflect.typeOf
2224
2325typealias PredicateFactory = (String , String , Set <String >, Set <String >) -> RequestPredicate
2426
@@ -33,35 +35,35 @@ class Router(private val predicateFactory: PredicateFactory) {
3335
3436 var filter: Filter = Filter .NoOp
3537
36- fun <I , T > GET (
38+ inline fun <reified I , reified T > GET (
3739 pattern : String ,
38- handlerFunction : HandlerFunction <I , T >,
39- ) = defaultRequestPredicate(pattern, " GET" , handlerFunction, emptySet())
40+ crossinline handlerFunction : HandlerFunction <I , T >,
41+ ) = defaultRequestPredicate(pattern, " GET" , HandlerFunctionWrapper .invoke( handlerFunction) , emptySet())
4042
41- fun <I , T > POST (
43+ inline fun <reified I , reified T > POST (
4244 pattern : String ,
43- handlerFunction : HandlerFunction <I , T >,
44- ) = defaultRequestPredicate(pattern, " POST" , handlerFunction)
45+ crossinline handlerFunction : HandlerFunction <I , T >,
46+ ) = defaultRequestPredicate(pattern, " POST" , HandlerFunctionWrapper .invoke( handlerFunction) )
4547
46- fun <I , T > PUT (
48+ inline fun <reified I , reified T > PUT (
4749 pattern : String ,
48- handlerFunction : HandlerFunction <I , T >,
49- ) = defaultRequestPredicate(pattern, " PUT" , handlerFunction)
50+ crossinline handlerFunction : HandlerFunction <I , T >,
51+ ) = defaultRequestPredicate(pattern, " PUT" , HandlerFunctionWrapper .invoke( handlerFunction) )
5052
51- fun <I , T > DELETE (
53+ inline fun <reified I , reified T > DELETE (
5254 pattern : String ,
53- handlerFunction : HandlerFunction <I , T >,
54- ) = defaultRequestPredicate(pattern, " DELETE" , handlerFunction, emptySet())
55+ crossinline handlerFunction : HandlerFunction <I , T >,
56+ ) = defaultRequestPredicate(pattern, " DELETE" , HandlerFunctionWrapper .invoke( handlerFunction) , emptySet())
5557
56- fun <I , T > PATCH (
58+ inline fun <reified I , reified T > PATCH (
5759 pattern : String ,
58- handlerFunction : HandlerFunction <I , T >,
59- ) = defaultRequestPredicate(pattern, " PATCH" , handlerFunction)
60+ crossinline handlerFunction : HandlerFunction <I , T >,
61+ ) = defaultRequestPredicate(pattern, " PATCH" , HandlerFunctionWrapper .invoke( handlerFunction) )
6062
61- private fun <I , T > defaultRequestPredicate (
63+ fun <I , T > defaultRequestPredicate (
6264 pattern : String ,
6365 method : String ,
64- handlerFunction : HandlerFunction <I , T >,
66+ handlerFunction : HandlerFunctionWrapper <I , T >,
6567 consuming : Set <String > = defaultConsuming,
6668 ) = predicateFactory(method, pattern, consuming, defaultProducing)
6769 .also { routes + = RouterFunction (it, handlerFunction) }
@@ -108,9 +110,28 @@ fun Filter.then(next: APIGatewayRequestHandlerFunction): APIGatewayRequestHandle
108110typealias APIGatewayRequestHandlerFunction = (APIGatewayProxyRequestEvent ) -> APIGatewayProxyResponseEvent
109111typealias HandlerFunction <I , T > = (request: Request <I >) -> ResponseEntity <T >
110112
113+ abstract class HandlerFunctionWrapper <I , T > {
114+ abstract val requestType: KType
115+ abstract val responseType: KType
116+
117+ abstract val handlerFunction: HandlerFunction <I , T >
118+
119+ companion object {
120+ inline operator fun <reified I , reified T > invoke (crossinline handler : HandlerFunction <I , T >): HandlerFunctionWrapper <I , T > {
121+ val requestType = typeOf<I >()
122+ val responseType = typeOf<T >()
123+ return object : HandlerFunctionWrapper <I , T >() {
124+ override val requestType: KType = requestType
125+ override val responseType: KType = responseType
126+ override val handlerFunction: HandlerFunction <I , T > = { request -> handler.invoke(request) }
127+ }
128+ }
129+ }
130+ }
131+
111132class RouterFunction <I , T >(
112133 val requestPredicate : RequestPredicate ,
113- val handler : HandlerFunction <I , T >,
134+ val handler : HandlerFunctionWrapper <I , T >,
114135) {
115136 override fun toString (): String {
116137 return " RouterFunction(requestPredicate=$requestPredicate )"
0 commit comments