Skip to content
This repository was archived by the owner on Jul 14, 2020. It is now read-only.

Commit 4bc39e2

Browse files
committed
Added a default jsondecoder
1 parent ca5b8f0 commit 4bc39e2

File tree

4 files changed

+24
-18
lines changed

4 files changed

+24
-18
lines changed

Diff for: Examples/EventSources/Tests/LinuxMain.swift

-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
import XCTest
21

3-
import EventSourcesTests
4-
5-
var tests = [XCTestCaseEntry]()
6-
tests += EventSourcesTests.allTests()
7-
XCTMain(tests)

Diff for: Sources/LambdaRuntime/Events/SNS.swift

-7
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ public struct SNS {
2828
}
2929
}
3030

31-
public static var dateFormatter: DateFormatter {
32-
let formatter = DateFormatter()
33-
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
34-
formatter.timeZone = TimeZone(secondsFromGMT: 0)
35-
return formatter
36-
}
37-
3831
public struct Message: Codable {
3932

4033
public enum Attributes: Codable {

Diff for: Sources/LambdaRuntime/Runtime+Codable.swift

+23-2
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@ import NIO
33
import NIOFoundationCompat
44

55
extension LambdaRuntime {
6+
7+
public static let awsJSONDecoder = LambdaRuntime.createAWSDecoder()
68

79
/// wrapper to use for the register function that wraps the encoding and decoding
810
public static func codable<Event: Decodable, Result: Encodable>(
11+
decoder: JSONDecoder = LambdaRuntime.awsJSONDecoder,
912
_ handler: @escaping (Event, Context) -> EventLoopFuture<Result>)
1013
-> ((NIO.ByteBuffer, Context) -> EventLoopFuture<ByteBuffer?>)
1114
{
1215
return { (inputBytes: NIO.ByteBuffer, ctx: Context) -> EventLoopFuture<ByteBuffer?> in
1316
let input: Event
1417
do {
15-
input = try JSONDecoder().decode(Event.self, from: inputBytes)
18+
input = try decoder.decode(Event.self, from: inputBytes)
1619
}
1720
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))")
1823
return ctx.eventLoop.makeFailedFuture(error)
1924
}
2025

@@ -26,19 +31,35 @@ extension LambdaRuntime {
2631
}
2732

2833
public static func codable<Event: Decodable>(
34+
decoder: JSONDecoder = LambdaRuntime.awsJSONDecoder,
2935
_ handler: @escaping (Event, Context) -> EventLoopFuture<Void>)
3036
-> ((NIO.ByteBuffer, Context) -> EventLoopFuture<ByteBuffer?>)
3137
{
3238
return { (inputBytes: NIO.ByteBuffer, ctx: Context) -> EventLoopFuture<ByteBuffer?> in
3339
let input: Event
3440
do {
35-
input = try JSONDecoder().decode(Event.self, from: inputBytes)
41+
input = try decoder.decode(Event.self, from: inputBytes)
3642
}
3743
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))")
3846
return ctx.eventLoop.makeFailedFuture(error)
3947
}
4048

4149
return handler(input, ctx).map { return nil }
4250
}
4351
}
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+
}
4465
}

Diff for: Tests/LambdaRuntimeTests/Events/SNSTests.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ class SNSTests: XCTestCase {
4646
func testSimpleEventFromJSON() {
4747
let data = SNSTests.eventPayload.data(using: .utf8)!
4848
do {
49-
let decoder = JSONDecoder()
50-
decoder.dateDecodingStrategy = .formatted(SNS.dateFormatter)
51-
let event = try decoder.decode(SNS.Event.self, from: data)
49+
let event = try LambdaRuntime.awsJSONDecoder.decode(SNS.Event.self, from: data)
5250

5351
guard let record = event.records.first else {
5452
XCTFail("Expected to have one record"); return

0 commit comments

Comments
 (0)