From 8ba70f27664ea8c8b7f38fb4c6f2fd4c129eb9c5 Mon Sep 17 00:00:00 2001 From: leogdion Date: Thu, 17 Jul 2025 15:17:57 -0400 Subject: [PATCH] Adding Support for Swift 6 (#1) --- Sources/XMLCoder/Auxiliaries/Box/DateBox.swift | 6 +++--- .../Auxiliaries/ISO8601DateFormatter.swift | 18 +++++++----------- .../XMLCoder/Encoder/DynamicNodeEncoding.swift | 2 +- Sources/XMLCoder/Encoder/XMLEncoder.swift | 16 +++++++++++----- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/Sources/XMLCoder/Auxiliaries/Box/DateBox.swift b/Sources/XMLCoder/Auxiliaries/Box/DateBox.swift index a9d18d4b..c18957b5 100644 --- a/Sources/XMLCoder/Auxiliaries/Box/DateBox.swift +++ b/Sources/XMLCoder/Auxiliaries/Box/DateBox.swift @@ -8,7 +8,7 @@ import Foundation -struct DateBox: Equatable { +struct DateBox: Equatable, Sendable { enum Format: Equatable { case secondsSince1970 case millisecondsSince1970 @@ -44,7 +44,7 @@ struct DateBox: Equatable { init?(iso8601 string: String) { if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) { - guard let unboxed = _iso8601Formatter.date(from: string) else { + guard let unboxed = ISO8601DateFormatter.xmlCoderFormatter().date(from: string) else { return nil } self.init(unboxed, format: .iso8601) @@ -70,7 +70,7 @@ struct DateBox: Equatable { return milliseconds.description case .iso8601: if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) { - return _iso8601Formatter.string(from: self.unboxed) + return ISO8601DateFormatter.xmlCoderFormatter().string(from: self.unboxed) } else { fatalError("ISO8601DateFormatter is unavailable on this platform.") } diff --git a/Sources/XMLCoder/Auxiliaries/ISO8601DateFormatter.swift b/Sources/XMLCoder/Auxiliaries/ISO8601DateFormatter.swift index 08c248bf..a431b23e 100644 --- a/Sources/XMLCoder/Auxiliaries/ISO8601DateFormatter.swift +++ b/Sources/XMLCoder/Auxiliaries/ISO8601DateFormatter.swift @@ -8,14 +8,10 @@ import Foundation -/// Shared ISO8601 Date Formatter -/// NOTE: This value is implicitly lazy and _must_ be lazy. We're compiled -/// against the latest SDK (w/ ISO8601DateFormatter), but linked against -/// whichever Foundation the user has. ISO8601DateFormatter might not exist, so -/// we better not hit this code path on an older OS. -@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) -var _iso8601Formatter: ISO8601DateFormatter = { - let formatter = ISO8601DateFormatter() - formatter.formatOptions = .withInternetDateTime - return formatter -}() +extension ISO8601DateFormatter { + static func xmlCoderFormatter() -> ISO8601DateFormatter { + let formatter = ISO8601DateFormatter() + formatter.formatOptions = .withInternetDateTime + return formatter + } +} \ No newline at end of file diff --git a/Sources/XMLCoder/Encoder/DynamicNodeEncoding.swift b/Sources/XMLCoder/Encoder/DynamicNodeEncoding.swift index 79cbec6f..bbb79fa8 100644 --- a/Sources/XMLCoder/Encoder/DynamicNodeEncoding.swift +++ b/Sources/XMLCoder/Encoder/DynamicNodeEncoding.swift @@ -40,7 +40,7 @@ ``` */ -public protocol DynamicNodeEncoding: Encodable { +public protocol DynamicNodeEncoding: Encodable, Sendable { static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding } diff --git a/Sources/XMLCoder/Encoder/XMLEncoder.swift b/Sources/XMLCoder/Encoder/XMLEncoder.swift index 23efd3d0..3444c733 100644 --- a/Sources/XMLCoder/Encoder/XMLEncoder.swift +++ b/Sources/XMLCoder/Encoder/XMLEncoder.swift @@ -13,7 +13,7 @@ open class XMLEncoder { // MARK: Options /// The formatting of the output XML data. - public struct OutputFormatting: OptionSet { + public struct OutputFormatting: OptionSet, Sendable { /// The format's default value. public let rawValue: UInt @@ -39,7 +39,7 @@ open class XMLEncoder { } /// A node's encoding type. Specifies how a node will be encoded. - public enum NodeEncoding { + public enum NodeEncoding : Sendable { case attribute case element case both @@ -233,8 +233,8 @@ open class XMLEncoder { @available(*, deprecated, renamed: "NodeEncodingStrategy") public typealias NodeEncodingStrategies = NodeEncodingStrategy - public typealias XMLNodeEncoderClosure = (CodingKey) -> NodeEncoding? - public typealias XMLEncodingClosure = (Encodable.Type, Encoder) -> XMLNodeEncoderClosure + public typealias XMLNodeEncoderClosure = @Sendable (CodingKey) -> NodeEncoding? + public typealias XMLEncodingClosure = @Sendable (Encodable.Type, Encoder) -> XMLNodeEncoderClosure /// Set of strategies to use for encoding of nodes. public enum NodeEncodingStrategy { @@ -262,7 +262,13 @@ open class XMLEncoder { guard let dynamicType = codableType as? DynamicNodeEncoding.Type else { return { _ in nil } } - return dynamicType.nodeEncoding(for:) + #if compiler(>=6.1) + return dynamicType.nodeEncoding(for:) + #else + return { (@Sendable key: CodingKey) in + dynamicType.nodeEncoding(for: key) + } + #endif } }