From db81ff75675cb9f93ca89c2577a9d064965a4882 Mon Sep 17 00:00:00 2001 From: Ivan Smolin Date: Fri, 1 Apr 2022 11:42:21 +0300 Subject: [PATCH] fix: Try parse date in ISO8601 format appending `.withFractionalSeconds` if `.withInternetDateTime` fails --- CHANGELOG.md | 4 ++ LeadKit.podspec | 2 +- .../Sources/Codable+DateCoding.swift | 51 ++++++++++++++++--- TIFoundationUtils/TIFoundationUtils.podspec | 2 +- TIKeychainUtils/TIKeychainUtils.podspec | 2 +- ...lable+Factory.swift => Cancellables.swift} | 11 ++-- .../DefaultJsonNetworkService.swift | 8 +-- TIMoyaNetworking/TIMoyaNetworking.podspec | 2 +- TINetworking/TINetworking.podspec | 2 +- TISwiftUtils/TISwiftUtils.podspec | 2 +- TITableKitUtils/TITableKitUtils.podspec | 2 +- TITransitions/TITransitions.podspec | 2 +- TIUIElements/TIUIElements.podspec | 2 +- TIUIKitCore/TIUIKitCore.podspec | 2 +- 14 files changed, 69 insertions(+), 25 deletions(-) rename TIMoyaNetworking/Sources/NetworkService/Cancellables/{Cancellable+Factory.swift => Cancellables.swift} (79%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92ee79f9..4ae0b8b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +### 1.12.3 + +- **Fix**: Try parse date in ISO8601 format appending `.withFractionalSeconds` if `.withInternetDateTime` fails + ### 1.12.2 - **Fix**: HeaderParameterEncoding encodes array correctly diff --git a/LeadKit.podspec b/LeadKit.podspec index 792c9d55..4acd8148 100644 --- a/LeadKit.podspec +++ b/LeadKit.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "LeadKit" - s.version = "1.12.2" + s.version = "1.12.3" s.summary = "iOS framework with a bunch of tools for rapid development" s.homepage = "https://github.com/TouchInstinct/LeadKit" s.license = "Apache License, Version 2.0" diff --git a/TIFoundationUtils/DateFormatting/Sources/Codable+DateCoding.swift b/TIFoundationUtils/DateFormatting/Sources/Codable+DateCoding.swift index 76c575ce..e6076fa2 100644 --- a/TIFoundationUtils/DateFormatting/Sources/Codable+DateCoding.swift +++ b/TIFoundationUtils/DateFormatting/Sources/Codable+DateCoding.swift @@ -44,15 +44,49 @@ public extension KeyedDecodingContainer { using: dateFormatter) } - func decodeDate(forKey key: Key, - using dateFormatter: ISO8601DateFormatter) throws -> Date { + func decodeDate(from string: String, + forKey key: Key, + userInfo: [CodingUserInfoKey: Any], + options formatOptions: ISO8601DateFormatter.Options) throws -> Date { - try date(from: try decode(String.self, forKey: key), - forKey: key, - using: dateFormatter) + let dateFormatter = try userInfo.iso8601DateFormatter(for: formatOptions) + + do { + return try date(from: string, + forKey: key, + using: dateFormatter) + } catch DecodingError.dataCorrupted where formatOptions == .withInternetDateTime { + let fractionSecondsOptions = formatOptions.union(.withFractionalSeconds) + + do { + let fractionalSecondsDateFormatter = try userInfo.iso8601DateFormatter(for: fractionSecondsOptions) + + return try date(from: string, + forKey: key, + using: fractionalSecondsDateFormatter) + } catch { + let failureReason = "Unable to decode date from \(string). Tried ISO8601 options: \(dateFormatter.formatOptions), \(fractionSecondsOptions)" + + throw DecodingError.dataCorruptedError(forKey: key, + in: self, + debugDescription: failureReason) + } + } } func decodeDate(forKey key: Key, + userInfo: [CodingUserInfoKey: Any], + options formatOptions: ISO8601DateFormatter.Options) throws -> Date { + + try decodeDate(from: try decode(String.self, forKey: key), + forKey: key, + userInfo: userInfo, + options: formatOptions) + } + + func decodeDate(forKey key: Key, + userInfo: [CodingUserInfoKey: Any], + options formatOptions: ISO8601DateFormatter.Options, using dateFormatter: ISO8601DateFormatter, required: Bool) throws -> Date? { @@ -60,9 +94,10 @@ public extension KeyedDecodingContainer { return nil } - return try date(from: stringDate, - forKey: key, - using: dateFormatter) + return try decodeDate(from: stringDate, + forKey: key, + userInfo: userInfo, + options: formatOptions) } private func date(from string: String, diff --git a/TIFoundationUtils/TIFoundationUtils.podspec b/TIFoundationUtils/TIFoundationUtils.podspec index e43aadcb..f51881db 100644 --- a/TIFoundationUtils/TIFoundationUtils.podspec +++ b/TIFoundationUtils/TIFoundationUtils.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'TIFoundationUtils' - s.version = '1.12.2' + s.version = '1.12.3' s.summary = 'Set of helpers for Foundation framework classes.' s.homepage = 'https://github.com/TouchInstinct/LeadKit/tree/' + s.version.to_s + '/' + s.name s.license = { :type => 'MIT', :file => 'LICENSE' } diff --git a/TIKeychainUtils/TIKeychainUtils.podspec b/TIKeychainUtils/TIKeychainUtils.podspec index 86507ad0..688065e9 100644 --- a/TIKeychainUtils/TIKeychainUtils.podspec +++ b/TIKeychainUtils/TIKeychainUtils.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'TIKeychainUtils' - s.version = '1.12.2' + s.version = '1.12.3' s.summary = 'Set of helpers for Keychain classes.' s.homepage = 'https://github.com/TouchInstinct/LeadKit/tree/' + s.version.to_s + '/' + s.name s.license = { :type => 'MIT', :file => 'LICENSE' } diff --git a/TIMoyaNetworking/Sources/NetworkService/Cancellables/Cancellable+Factory.swift b/TIMoyaNetworking/Sources/NetworkService/Cancellables/Cancellables.swift similarity index 79% rename from TIMoyaNetworking/Sources/NetworkService/Cancellables/Cancellable+Factory.swift rename to TIMoyaNetworking/Sources/NetworkService/Cancellables/Cancellables.swift index 2b7ee6f0..87955661 100644 --- a/TIMoyaNetworking/Sources/NetworkService/Cancellables/Cancellable+Factory.swift +++ b/TIMoyaNetworking/Sources/NetworkService/Cancellables/Cancellables.swift @@ -22,13 +22,18 @@ import Moya import TISwiftUtils +import TIFoundationUtils -public extension Cancellable { - static func nonCancellable() -> Cancellable { +public struct Cancellables { + public static func nonCancellable() -> Cancellable { NonCancellable() } - static func scoped(scopeCancellableClosure: ScopeCancellable.ScopeCancellableClosure) -> Cancellable { + public static func scoped(scopeCancellableClosure: ScopeCancellable.ScopeCancellableClosure) -> Cancellable { ScopeCancellable(scopeCancellableClosure: scopeCancellableClosure) } + + public static func nonCancellableTask() -> CancellableTask { + NonCancellable() + } } diff --git a/TIMoyaNetworking/Sources/NetworkService/DefaultJsonNetworkService.swift b/TIMoyaNetworking/Sources/NetworkService/DefaultJsonNetworkService.swift index 7ff2a0ff..ec9146d8 100644 --- a/TIMoyaNetworking/Sources/NetworkService/DefaultJsonNetworkService.swift +++ b/TIMoyaNetworking/Sources/NetworkService/DefaultJsonNetworkService.swift @@ -25,6 +25,7 @@ import Alamofire import Moya import TISwiftUtils import Foundation +import TIFoundationUtils open class DefaultJsonNetworkService { public var session: Session @@ -43,13 +44,12 @@ open class DefaultJsonNetworkService { public var plugins: [PluginType] = [] public init(session: Session, - jsonDecoder: JSONDecoder, - jsonEncoder: JSONEncoder, + jsonCodingConfigurator: JsonCodingConfigurator, defaultServer: Server) { self.session = session - self.jsonDecoder = jsonDecoder - self.jsonEncoder = jsonEncoder + self.jsonDecoder = jsonCodingConfigurator.jsonDecoder + self.jsonEncoder = jsonCodingConfigurator.jsonEncoder self.defaultServer = defaultServer } diff --git a/TIMoyaNetworking/TIMoyaNetworking.podspec b/TIMoyaNetworking/TIMoyaNetworking.podspec index d2fbfbd5..a7c25ff3 100644 --- a/TIMoyaNetworking/TIMoyaNetworking.podspec +++ b/TIMoyaNetworking/TIMoyaNetworking.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'TIMoyaNetworking' - s.version = '1.12.2' + s.version = '1.12.3' s.summary = 'Moya + Swagger network service.' s.homepage = 'https://github.com/TouchInstinct/LeadKit/tree/' + s.version.to_s + '/' + s.name s.license = { :type => 'MIT', :file => 'LICENSE' } diff --git a/TINetworking/TINetworking.podspec b/TINetworking/TINetworking.podspec index 3731b590..44c60c41 100644 --- a/TINetworking/TINetworking.podspec +++ b/TINetworking/TINetworking.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'TINetworking' - s.version = '1.12.2' + s.version = '1.12.3' s.summary = 'Swagger-frendly networking layer helpers.' s.homepage = 'https://github.com/TouchInstinct/LeadKit/tree/' + s.version.to_s + '/' + s.name s.license = { :type => 'MIT', :file => 'LICENSE' } diff --git a/TISwiftUtils/TISwiftUtils.podspec b/TISwiftUtils/TISwiftUtils.podspec index df6ed9bd..108d05e3 100644 --- a/TISwiftUtils/TISwiftUtils.podspec +++ b/TISwiftUtils/TISwiftUtils.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'TISwiftUtils' - s.version = '1.12.2' + s.version = '1.12.3' s.summary = 'Bunch of useful helpers for Swift development.' s.homepage = 'https://github.com/TouchInstinct/LeadKit/tree/' + s.version.to_s + '/' + s.name s.license = { :type => 'MIT', :file => 'LICENSE' } diff --git a/TITableKitUtils/TITableKitUtils.podspec b/TITableKitUtils/TITableKitUtils.podspec index 6b1f4e8c..806a6561 100644 --- a/TITableKitUtils/TITableKitUtils.podspec +++ b/TITableKitUtils/TITableKitUtils.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'TITableKitUtils' - s.version = '1.12.2' + s.version = '1.12.3' s.summary = 'Set of helpers for TableKit classes.' s.homepage = 'https://github.com/TouchInstinct/LeadKit/tree/' + s.version.to_s + '/' + s.name s.license = { :type => 'MIT', :file => 'LICENSE' } diff --git a/TITransitions/TITransitions.podspec b/TITransitions/TITransitions.podspec index b7869128..0c5acd19 100644 --- a/TITransitions/TITransitions.podspec +++ b/TITransitions/TITransitions.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'TITransitions' - s.version = '1.12.2' + s.version = '1.12.3' s.summary = 'Set of custom transitions to present controller. ' s.homepage = 'https://github.com/TouchInstinct/LeadKit/tree/' + s.version.to_s + '/' + s.name s.license = { :type => 'MIT', :file => 'LICENSE' } diff --git a/TIUIElements/TIUIElements.podspec b/TIUIElements/TIUIElements.podspec index 19a5f040..df5ea882 100644 --- a/TIUIElements/TIUIElements.podspec +++ b/TIUIElements/TIUIElements.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'TIUIElements' - s.version = '1.12.2' + s.version = '1.12.3' s.summary = 'Bunch of useful protocols and views.' s.homepage = 'https://github.com/TouchInstinct/LeadKit/tree/' + s.version.to_s + '/' + s.name s.license = { :type => 'MIT', :file => 'LICENSE' } diff --git a/TIUIKitCore/TIUIKitCore.podspec b/TIUIKitCore/TIUIKitCore.podspec index 0105b11c..aa202504 100644 --- a/TIUIKitCore/TIUIKitCore.podspec +++ b/TIUIKitCore/TIUIKitCore.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'TIUIKitCore' - s.version = '1.12.2' + s.version = '1.12.3' s.summary = 'Core UI elements: protocols, views and helpers.' s.homepage = 'https://github.com/TouchInstinct/LeadKit/tree/' + s.version.to_s + '/' + s.name s.license = { :type => 'MIT', :file => 'LICENSE' }