@@ -19,6 +19,8 @@ package io.moia.router
19
19
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
20
20
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent.ProxyRequestContext
21
21
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent
22
+ import kotlin.reflect.KType
23
+ import kotlin.reflect.typeOf
22
24
23
25
typealias PredicateFactory = (String , String , Set <String >, Set <String >) -> RequestPredicate
24
26
@@ -33,35 +35,35 @@ class Router(private val predicateFactory: PredicateFactory) {
33
35
34
36
var filter: Filter = Filter .NoOp
35
37
36
- fun <I , T > GET (
38
+ inline fun <reified I , reified T > GET (
37
39
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())
40
42
41
- fun <I , T > POST (
43
+ inline fun <reified I , reified T > POST (
42
44
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) )
45
47
46
- fun <I , T > PUT (
48
+ inline fun <reified I , reified T > PUT (
47
49
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) )
50
52
51
- fun <I , T > DELETE (
53
+ inline fun <reified I , reified T > DELETE (
52
54
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())
55
57
56
- fun <I , T > PATCH (
58
+ inline fun <reified I , reified T > PATCH (
57
59
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) )
60
62
61
- private fun <I , T > defaultRequestPredicate (
63
+ fun <I , T > defaultRequestPredicate (
62
64
pattern : String ,
63
65
method : String ,
64
- handlerFunction : HandlerFunction <I , T >,
66
+ handlerFunction : HandlerFunctionWrapper <I , T >,
65
67
consuming : Set <String > = defaultConsuming,
66
68
) = predicateFactory(method, pattern, consuming, defaultProducing)
67
69
.also { routes + = RouterFunction (it, handlerFunction) }
@@ -108,9 +110,28 @@ fun Filter.then(next: APIGatewayRequestHandlerFunction): APIGatewayRequestHandle
108
110
typealias APIGatewayRequestHandlerFunction = (APIGatewayProxyRequestEvent ) -> APIGatewayProxyResponseEvent
109
111
typealias HandlerFunction <I , T > = (request: Request <I >) -> ResponseEntity <T >
110
112
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
+
111
132
class RouterFunction <I , T >(
112
133
val requestPredicate : RequestPredicate ,
113
- val handler : HandlerFunction <I , T >,
134
+ val handler : HandlerFunctionWrapper <I , T >,
114
135
) {
115
136
override fun toString (): String {
116
137
return " RouterFunction(requestPredicate=$requestPredicate )"
0 commit comments