Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
Expand Down
40 changes: 35 additions & 5 deletions Sources/AWSLambdaEvents/Utils/DateWrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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 = [
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/AWSLambdaEventsTests/Utils/DateWrapperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
}
}
Expand Down