From 686895d057d66190a85c1c335a5413c218502a69 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Fri, 27 Mar 2026 18:00:16 +0100 Subject: [PATCH] Improved the ISODateTimeTranscoder transcoder in the library target to support both ISO dates and timestamps. --- ...oder.swift => ISODateTimeTranscoder.swift} | 35 +++++++++++-------- .../Public/Clients/AmiiboLiveClient.swift | 2 +- 2 files changed, 22 insertions(+), 15 deletions(-) rename Sources/AmiiboService/Internal/Transcoders/{ISOTimestampTranscoder.swift => ISODateTimeTranscoder.swift} (50%) diff --git a/Sources/AmiiboService/Internal/Transcoders/ISOTimestampTranscoder.swift b/Sources/AmiiboService/Internal/Transcoders/ISODateTimeTranscoder.swift similarity index 50% rename from Sources/AmiiboService/Internal/Transcoders/ISOTimestampTranscoder.swift rename to Sources/AmiiboService/Internal/Transcoders/ISODateTimeTranscoder.swift index 0a2121a..28cc7bb 100644 --- a/Sources/AmiiboService/Internal/Transcoders/ISOTimestampTranscoder.swift +++ b/Sources/AmiiboService/Internal/Transcoders/ISODateTimeTranscoder.swift @@ -15,19 +15,22 @@ import Foundation 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. -struct ISOTimestampTranscoder { +/// 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 ISODateTimeTranscoder { // MARK: Properties - /// A formatter to use to decode and encode ISO timestamps dates. - private let dateFormatter: DateFormatter = .isoTimestamp + /// A formatter to use to decode and encode ISO timestamp dates. + 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 @@ -35,22 +38,26 @@ extension ISOTimestampTranscoder: DateTranscoder { /// - Parameter date: A date to encode. /// - Returns: A string representation of the date in `yyyy-MM-dd'T'HH:mm:ss.SSSSSS` format. 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. /// - Returns: A date parsed from the string. /// - Throws: A `DecodingError` if the string cannot be parsed into a valid date. func decode(_ string: String) throws -> Date { - guard let date = dateFormatter.date(from: string) else { - throw DecodingError.dataCorrupted(.init( - codingPath: [], - debugDescription: "Expected an ISO timestamp with format 'yyyy-MM-dd'T'HH:mm:ss.SSSSSS', but found '\(string)' instead." - )) + if let date = timestampFormatter.date(from: string) { + return date } - 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." + )) } } diff --git a/Sources/AmiiboService/Public/Clients/AmiiboLiveClient.swift b/Sources/AmiiboService/Public/Clients/AmiiboLiveClient.swift index 0ba560b..81933ff 100644 --- a/Sources/AmiiboService/Public/Clients/AmiiboLiveClient.swift +++ b/Sources/AmiiboService/Public/Clients/AmiiboLiveClient.swift @@ -35,7 +35,7 @@ public struct AmiiboLiveClient: Sendable { self.client = .init( serverURL: serverURL, - configuration: .init(dateTranscoder: ISOTimestampTranscoder()), + configuration: .init(dateTranscoder: ISODateTimeTranscoder()), transport: transport ) }