remove requirment on macOS 10.13 (#156)

motivation: simplify downstream libraries

changes: precondition out debugging uses cases on macOS < 10.13
This commit is contained in:
tomer doron
2020-08-10 10:02:12 -07:00
committed by GitHub
parent f3c68d66c4
commit f731664c4d
3 changed files with 38 additions and 11 deletions
-3
View File
@@ -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"]),
@@ -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
}
@@ -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)
}
}