Improved the ISODateTimeTranscoder transcoder in the library target to support both ISO dates and timestamps.
This commit is contained in:
+21
-14
@@ -15,19 +15,22 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import OpenAPIRuntime
|
import OpenAPIRuntime
|
||||||
|
|
||||||
/// A type that allows the decoding and encoding of ISO timestamp dates, defined by the `yyyy-MM-dd'T'HH:mm:ss.SSSSSS` custom date format.
|
/// A type that allows the decoding and encoding of ISO dates, supporting both the `yyyy-MM-dd'T'HH:mm:ss.SSSSSS` timestamp format and the `yyyy-MM-dd` date-only format.
|
||||||
struct ISOTimestampTranscoder {
|
struct ISODateTimeTranscoder {
|
||||||
|
|
||||||
// MARK: Properties
|
// MARK: Properties
|
||||||
|
|
||||||
/// A formatter to use to decode and encode ISO timestamps dates.
|
/// A formatter to use to decode and encode ISO timestamp dates.
|
||||||
private let dateFormatter: DateFormatter = .isoTimestamp
|
private let timestampFormatter: DateFormatter = .isoTimestamp
|
||||||
|
|
||||||
|
/// A formatter to use to decode and encode ISO date-only strings.
|
||||||
|
private let dateFormatter: DateFormatter = .isoDate
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - DateTranscoder
|
// MARK: - DateTranscoder
|
||||||
|
|
||||||
extension ISOTimestampTranscoder: DateTranscoder {
|
extension ISODateTimeTranscoder: DateTranscoder {
|
||||||
|
|
||||||
// MARK: Functions
|
// MARK: Functions
|
||||||
|
|
||||||
@@ -35,22 +38,26 @@ extension ISOTimestampTranscoder: DateTranscoder {
|
|||||||
/// - Parameter date: A date to encode.
|
/// - Parameter date: A date to encode.
|
||||||
/// - Returns: A string representation of the date in `yyyy-MM-dd'T'HH:mm:ss.SSSSSS` format.
|
/// - Returns: A string representation of the date in `yyyy-MM-dd'T'HH:mm:ss.SSSSSS` format.
|
||||||
func encode(_ date: Date) throws -> String {
|
func encode(_ date: Date) throws -> String {
|
||||||
dateFormatter.string(from: date)
|
timestampFormatter.string(from: date)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Decodes an ISO timestamp string into a date.
|
/// Decodes an ISO date string into a date, trying the timestamp format first and falling back to the date-only format.
|
||||||
/// - Parameter string: A string to decode.
|
/// - Parameter string: A string to decode.
|
||||||
/// - Returns: A date parsed from the string.
|
/// - Returns: A date parsed from the string.
|
||||||
/// - Throws: A `DecodingError` if the string cannot be parsed into a valid date.
|
/// - Throws: A `DecodingError` if the string cannot be parsed into a valid date.
|
||||||
func decode(_ string: String) throws -> Date {
|
func decode(_ string: String) throws -> Date {
|
||||||
guard let date = dateFormatter.date(from: string) else {
|
if let date = timestampFormatter.date(from: string) {
|
||||||
throw DecodingError.dataCorrupted(.init(
|
return date
|
||||||
codingPath: [],
|
|
||||||
debugDescription: "Expected an ISO timestamp with format 'yyyy-MM-dd'T'HH:mm:ss.SSSSSS', but found '\(string)' instead."
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return date
|
if let date = dateFormatter.date(from: string) {
|
||||||
|
return date
|
||||||
|
}
|
||||||
|
|
||||||
|
throw DecodingError.dataCorrupted(.init(
|
||||||
|
codingPath: [],
|
||||||
|
debugDescription: "Expected an ISO date with format 'yyyy-MM-dd'T'HH:mm:ss.SSSSSS' or 'yyyy-MM-dd', but found '\(string)' instead."
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -35,7 +35,7 @@ public struct AmiiboLiveClient: Sendable {
|
|||||||
|
|
||||||
self.client = .init(
|
self.client = .init(
|
||||||
serverURL: serverURL,
|
serverURL: serverURL,
|
||||||
configuration: .init(dateTranscoder: ISOTimestampTranscoder()),
|
configuration: .init(dateTranscoder: ISODateTimeTranscoder()),
|
||||||
transport: transport
|
transport: transport
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user