@@ -3,18 +3,23 @@ import NIO
3
3
import NIOFoundationCompat
4
4
5
5
extension LambdaRuntime {
6
+
7
+ public static let awsJSONDecoder = LambdaRuntime . createAWSDecoder ( )
6
8
7
9
/// wrapper to use for the register function that wraps the encoding and decoding
8
10
public static func codable< Event: Decodable , Result: Encodable > (
11
+ decoder: JSONDecoder = LambdaRuntime . awsJSONDecoder,
9
12
_ handler: @escaping ( Event , Context ) -> EventLoopFuture < Result > )
10
13
-> ( ( NIO . ByteBuffer , Context ) -> EventLoopFuture < ByteBuffer ? > )
11
14
{
12
15
return { ( inputBytes: NIO . ByteBuffer , ctx: Context ) -> EventLoopFuture < ByteBuffer ? > in
13
16
let input : Event
14
17
do {
15
- input = try JSONDecoder ( ) . decode ( Event . self, from: inputBytes)
18
+ input = try decoder . decode ( Event . self, from: inputBytes)
16
19
}
17
20
catch {
21
+ let payload = inputBytes. getString ( at: 0 , length: inputBytes. readableBytes)
22
+ ctx. logger. error ( " Could not decode to type ` \( String ( describing: Event . self) ) `: \( error) , payload: \( String ( describing: payload) ) " )
18
23
return ctx. eventLoop. makeFailedFuture ( error)
19
24
}
20
25
@@ -26,19 +31,35 @@ extension LambdaRuntime {
26
31
}
27
32
28
33
public static func codable< Event: Decodable > (
34
+ decoder: JSONDecoder = LambdaRuntime . awsJSONDecoder,
29
35
_ handler: @escaping ( Event , Context ) -> EventLoopFuture < Void > )
30
36
-> ( ( NIO . ByteBuffer , Context ) -> EventLoopFuture < ByteBuffer ? > )
31
37
{
32
38
return { ( inputBytes: NIO . ByteBuffer , ctx: Context ) -> EventLoopFuture < ByteBuffer ? > in
33
39
let input : Event
34
40
do {
35
- input = try JSONDecoder ( ) . decode ( Event . self, from: inputBytes)
41
+ input = try decoder . decode ( Event . self, from: inputBytes)
36
42
}
37
43
catch {
44
+ let payload = inputBytes. getString ( at: 0 , length: inputBytes. readableBytes)
45
+ ctx. logger. error ( " Could not decode to type ` \( String ( describing: Event . self) ) `: \( error) , payload: \( String ( describing: payload) ) " )
38
46
return ctx. eventLoop. makeFailedFuture ( error)
39
47
}
40
48
41
49
return handler ( input, ctx) . map { return nil }
42
50
}
43
51
}
52
+
53
+ public static func createAWSDateFormatter( ) -> DateFormatter {
54
+ let formatter = DateFormatter ( )
55
+ formatter. dateFormat = " yyyy-MM-dd'T'HH:mm:ss.SSSZ "
56
+ formatter. timeZone = TimeZone ( secondsFromGMT: 0 )
57
+ return formatter
58
+ }
59
+
60
+ public static func createAWSDecoder( ) -> JSONDecoder {
61
+ let decoder = JSONDecoder ( )
62
+ decoder. dateDecodingStrategy = . formatted( self . createAWSDateFormatter ( ) )
63
+ return decoder
64
+ }
44
65
}
0 commit comments