From 2b33f48304464016c98b0de7b2497757d315a5a0 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Fri, 26 Jun 2020 11:01:20 +0100 Subject: [PATCH 1/5] Support common header date format with brackets Sometimes common header date comes with an alphabetical timezone in brackets after the numeric timezone --- .../AWSLambdaEvents/Utils/DateWrappers.swift | 6 +++-- .../Utils/DateWrapperTests.swift | 26 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Sources/AWSLambdaEvents/Utils/DateWrappers.swift b/Sources/AWSLambdaEvents/Utils/DateWrappers.swift index 81ed03ec..753f57e7 100644 --- a/Sources/AWSLambdaEvents/Utils/DateWrappers.swift +++ b/Sources/AWSLambdaEvents/Utils/DateWrappers.swift @@ -80,10 +80,12 @@ public struct RFC5322DateTimeCoding: Decodable { public init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() - let dateString = try container.decode(String.self) + let string = try container.decode(String.self) + let bracket = string.firstIndex(of: "(") ?? string.endIndex + let dateString = String(string[string.startIndex.. Date: Fri, 26 Jun 2020 11:13:12 +0100 Subject: [PATCH 2/5] sanity.sh changes --- Sources/AWSLambdaEvents/Utils/DateWrappers.swift | 2 +- Tests/AWSLambdaEventsTests/Utils/DateWrapperTests.swift | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Sources/AWSLambdaEvents/Utils/DateWrappers.swift b/Sources/AWSLambdaEvents/Utils/DateWrappers.swift index 753f57e7..a2cfdf1c 100644 --- a/Sources/AWSLambdaEvents/Utils/DateWrappers.swift +++ b/Sources/AWSLambdaEvents/Utils/DateWrappers.swift @@ -82,7 +82,7 @@ public struct RFC5322DateTimeCoding: Decodable { let container = try decoder.singleValueContainer() let string = try container.decode(String.self) let bracket = string.firstIndex(of: "(") ?? string.endIndex - let dateString = String(string[string.startIndex.. Date: Sat, 27 Jun 2020 11:39:29 +0100 Subject: [PATCH 3/5] Additions based on review comments --- Sources/AWSLambdaEvents/Utils/DateWrappers.swift | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Sources/AWSLambdaEvents/Utils/DateWrappers.swift b/Sources/AWSLambdaEvents/Utils/DateWrappers.swift index a2cfdf1c..444e93f9 100644 --- a/Sources/AWSLambdaEvents/Utils/DateWrappers.swift +++ b/Sources/AWSLambdaEvents/Utils/DateWrappers.swift @@ -80,10 +80,13 @@ public struct RFC5322DateTimeCoding: Decodable { public init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() - let string = try container.decode(String.self) - let bracket = string.firstIndex(of: "(") ?? string.endIndex - let dateString = String(string[string.startIndex ..< bracket]) - guard let date = Self.dateFormatter.date(from: dateString) else { + var string = try container.decode(String.self) + // RFC5322 dates sometimes have the alphabetic version of the timezone in brackets after the numeric version. The date formatter + // fails to parse this so we need to remove this before parsing. + if let bracket = string.firstIndex(of: "(") { + string = String(string[string.startIndex ..< bracket]) + } + guard let date = Self.dateFormatter.date(from: string) else { throw DecodingError.dataCorruptedError(in: container, debugDescription: "Expected date to be in RFC5322 date-time format with fractional seconds, but `\(string)` does not forfill format") } From 953e853705b81778b723436376bad8c9bbc1d317 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Sat, 27 Jun 2020 11:42:27 +0100 Subject: [PATCH 4/5] Update readme with SES Event link --- readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/readme.md b/readme.md index 91d040ad..377eeec3 100644 --- a/readme.md +++ b/readme.md @@ -337,6 +337,7 @@ AWS Lambda functions can be invoked directly from the AWS Lambda console UI, AWS * [APIGateway Proxy](https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html) * [S3 Events](https://docs.aws.amazon.com/lambda/latest/dg/with-s3.html) +* [SES Events](https://docs.aws.amazon.com/lambda/latest/dg/services-ses.html) * [SNS Events](https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html) * [SQS Events](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html) * [CloudWatch Events](https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents.html) From 49edceb13bea665105cbed857a639b55626dc891 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Sat, 27 Jun 2020 12:25:28 +0100 Subject: [PATCH 5/5] Need to trim whitespace after removing timezone --- Sources/AWSLambdaEvents/Utils/DateWrappers.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/AWSLambdaEvents/Utils/DateWrappers.swift b/Sources/AWSLambdaEvents/Utils/DateWrappers.swift index 444e93f9..e8432cf8 100644 --- a/Sources/AWSLambdaEvents/Utils/DateWrappers.swift +++ b/Sources/AWSLambdaEvents/Utils/DateWrappers.swift @@ -84,7 +84,7 @@ public struct RFC5322DateTimeCoding: Decodable { // RFC5322 dates sometimes have the alphabetic version of the timezone in brackets after the numeric version. The date formatter // fails to parse this so we need to remove this before parsing. if let bracket = string.firstIndex(of: "(") { - string = String(string[string.startIndex ..< bracket]) + string = String(string[string.startIndex ..< bracket].trimmingCharacters(in: .whitespaces)) } guard let date = Self.dateFormatter.date(from: string) else { throw DecodingError.dataCorruptedError(in: container, debugDescription: