diff --git a/Package.swift b/Package.swift index 1d56806e..0e5823f6 100644 --- a/Package.swift +++ b/Package.swift @@ -4,9 +4,6 @@ import PackageDescription let package = Package( name: "swift-aws-lambda-runtime", - platforms: [ - .macOS(.v10_13), - ], products: [ // this library exports `AWSLambdaRuntimeCore` and adds Foundation convenience methods .library(name: "AWSLambdaRuntime", targets: ["AWSLambdaRuntime"]), diff --git a/Sources/AWSLambdaEvents/Utils/DateWrappers.swift b/Sources/AWSLambdaEvents/Utils/DateWrappers.swift index e8432cf8..d5bc22f6 100644 --- a/Sources/AWSLambdaEvents/Utils/DateWrappers.swift +++ b/Sources/AWSLambdaEvents/Utils/DateWrappers.swift @@ -28,13 +28,27 @@ public struct ISO8601Coding: Decodable { public init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() let dateString = try container.decode(String.self) - guard let date = Self.dateFormatter.date(from: dateString) else { + guard let date = Self.decodeDate(from: dateString) else { throw DecodingError.dataCorruptedError(in: container, debugDescription: - "Expected date to be in iso8601 date format, but `\(dateString)` does not forfill format") + "Expected date to be in ISO8601 date format, but `\(dateString)` is not in the correct format") } self.wrappedValue = date } + private static func decodeDate(from string: String) -> Date? { + #if os(Linux) + return Self.dateFormatter.date(from: string) + #elseif os(macOS) + if #available(macOS 10.12, *) { + return Self.dateFormatter.date(from: string) + } else { + // unlikely *debugging* use case of swift 5.2+ on older macOS + preconditionFailure("Unsporrted macOS version") + } + #endif + } + + @available(macOS 10.12, *) private static let dateFormatter = ISO8601DateFormatter() } @@ -49,14 +63,30 @@ public struct ISO8601WithFractionalSecondsCoding: Decodable { public init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() let dateString = try container.decode(String.self) - guard let date = Self.dateFormatter.date(from: dateString) else { + guard let date = Self.decodeDate(from: dateString) else { throw DecodingError.dataCorruptedError(in: container, debugDescription: - "Expected date to be in iso8601 date format with fractional seconds, but `\(dateString)` does not forfill format") + "Expected date to be in ISO8601 date format with fractional seconds, but `\(dateString)` is not in the correct format") } self.wrappedValue = date } + private static func decodeDate(from string: String) -> Date? { + #if os(Linux) + return Self.dateFormatter.date(from: string) + #elseif os(macOS) + if #available(macOS 10.13, *) { + return self.dateFormatter.date(from: string) + } else { + // unlikely *debugging* use case of swift 5.2+ on older macOS + preconditionFailure("Unsporrted macOS version") + } + #endif + } + + @available(macOS 10.13, *) private static let dateFormatter: ISO8601DateFormatter = Self.createDateFormatter() + + @available(macOS 10.13, *) private static func createDateFormatter() -> ISO8601DateFormatter { let formatter = ISO8601DateFormatter() formatter.formatOptions = [ @@ -88,7 +118,7 @@ public struct RFC5322DateTimeCoding: Decodable { } 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") + "Expected date to be in RFC5322 date-time format with fractional seconds, but `\(string)` is not in the correct format") } self.wrappedValue = date } diff --git a/Tests/AWSLambdaEventsTests/Utils/DateWrapperTests.swift b/Tests/AWSLambdaEventsTests/Utils/DateWrapperTests.swift index fef0b2ba..35b1a474 100644 --- a/Tests/AWSLambdaEventsTests/Utils/DateWrapperTests.swift +++ b/Tests/AWSLambdaEventsTests/Utils/DateWrapperTests.swift @@ -43,7 +43,7 @@ class DateWrapperTests: XCTestCase { } XCTAssertEqual(context.codingPath.compactMap { $0.stringValue }, ["date"]) - XCTAssertEqual(context.debugDescription, "Expected date to be in iso8601 date format, but `\(date)` does not forfill format") + XCTAssertEqual(context.debugDescription, "Expected date to be in ISO8601 date format, but `\(date)` is not in the correct format") XCTAssertNil(context.underlyingError) } } @@ -75,7 +75,7 @@ class DateWrapperTests: XCTestCase { } XCTAssertEqual(context.codingPath.compactMap { $0.stringValue }, ["date"]) - XCTAssertEqual(context.debugDescription, "Expected date to be in iso8601 date format with fractional seconds, but `\(date)` does not forfill format") + XCTAssertEqual(context.debugDescription, "Expected date to be in ISO8601 date format with fractional seconds, but `\(date)` is not in the correct format") XCTAssertNil(context.underlyingError) } } @@ -133,7 +133,7 @@ class DateWrapperTests: XCTestCase { } XCTAssertEqual(context.codingPath.compactMap { $0.stringValue }, ["date"]) - XCTAssertEqual(context.debugDescription, "Expected date to be in RFC5322 date-time format with fractional seconds, but `\(date)` does not forfill format") + XCTAssertEqual(context.debugDescription, "Expected date to be in RFC5322 date-time format with fractional seconds, but `\(date)` is not in the correct format") XCTAssertNil(context.underlyingError) } }