From e7eb4d71e41dac6bbc4fd3bef396b7a83b172c45 Mon Sep 17 00:00:00 2001 From: Emil Date: Fri, 2 Aug 2024 11:36:45 +0200 Subject: [PATCH 1/3] Enable Strict Concurrency mode --- Package.swift | 25 +++++++++++-------- .../URLSessionDecodable.swift | 14 +++++------ .../URLSessionDecodableError.swift | 4 +-- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/Package.swift b/Package.swift index 27d7d8c..4c16ea0 100644 --- a/Package.swift +++ b/Package.swift @@ -1,13 +1,13 @@ -// swift-tools-version:5.7 +// swift-tools-version:5.10 import PackageDescription let package = Package( name: "URLSessionDecodable", platforms: [ - .macOS(.v10_13), - .iOS(.v11), - .tvOS(.v11), + .macOS(.v13), + .iOS(.v12), + .tvOS(.v12), .watchOS(.v4) ], products: [ @@ -15,16 +15,21 @@ let package = Package( name: "URLSessionDecodable", targets: ["URLSessionDecodable"]) ], - dependencies: [ - // Dependencies declare other packages that this package depends on. - // .package(url: /* package url */, from: "1.0.0"), - ], + dependencies: [], targets: [ .target( name: "URLSessionDecodable", - dependencies: []), + dependencies: [], + swiftSettings: [ + .enableExperimentalFeature("StrictConcurrency") + ] + ), .testTarget( name: "URLSessionDecodableTests", - dependencies: ["URLSessionDecodable"]) + dependencies: ["URLSessionDecodable"], + swiftSettings: [ + .enableExperimentalFeature("StrictConcurrency") + ] + ), ] ) diff --git a/Sources/URLSessionDecodable/URLSessionDecodable.swift b/Sources/URLSessionDecodable/URLSessionDecodable.swift index 7d7fe83..d7df122 100644 --- a/Sources/URLSessionDecodable/URLSessionDecodable.swift +++ b/Sources/URLSessionDecodable/URLSessionDecodable.swift @@ -23,8 +23,8 @@ extension URLSession { method: HTTPMethod, parameters: ParametersEncoding?, headers: HTTPHeaders?, - decoder: DataDecoder, - completionHandler: @escaping (Result) -> Void + decoder: @autoclosure @escaping @Sendable () -> DataDecoder, + completionHandler: @escaping @Sendable (Result) -> Void ) -> URLSessionDataTask { let request = self.request(with: url, method: method, parameters: parameters, headers: headers) let task = dataTask(with: request) { data, response, error in @@ -45,7 +45,7 @@ extension URLSession { return } - completionHandler(Self.handle(response: httpResponse, data: data, decoder: decoder, url: url)) + completionHandler(Self.handle(response: httpResponse, data: data, decoder: decoder(), url: url)) } return task } @@ -60,7 +60,7 @@ extension URLSession { method: HTTPMethod, parameters: ParametersEncoding?, headers: HTTPHeaders?, - decoder: DataDecoder + decoder: @autoclosure @escaping @Sendable () -> DataDecoder ) async throws -> T { let request = self.request(with: url, method: method, parameters: parameters, headers: headers) let (data, response) = try await data(for: request) @@ -68,7 +68,7 @@ extension URLSession { os_log("%@", "Non-http response \(String(describing: type(of: T.self))) \(url) - \(response)") throw URLSessionDecodableError.nonHTTPResponse(response) } - return try Self.handle(response: httpResponse, data: data, decoder: decoder, url: url).get() + return try Self.handle(response: httpResponse, data: data, decoder: decoder(), url: url).get() } // MARK: - Private @@ -76,7 +76,7 @@ extension URLSession { private static func handle( response: HTTPURLResponse, data: Data, - decoder: DataDecoder, + decoder: @autoclosure @escaping @Sendable () -> DataDecoder, url: URL ) -> Result { guard 200..<300 ~= response.statusCode else { @@ -87,7 +87,7 @@ extension URLSession { } do { - return try .success(decoder.decode(T.self, from: data)) + return try .success(decoder().decode(T.self, from: data)) } catch { os_log("%@", "Error while decoding \(String(describing: type(of: T.self))) \(error)") let deserializationError = URLSessionDecodableError.Deserialization(statusCode: response.statusCode, diff --git a/Sources/URLSessionDecodable/URLSessionDecodableError.swift b/Sources/URLSessionDecodable/URLSessionDecodableError.swift index 335f590..a908496 100644 --- a/Sources/URLSessionDecodable/URLSessionDecodableError.swift +++ b/Sources/URLSessionDecodable/URLSessionDecodableError.swift @@ -9,14 +9,14 @@ public enum URLSessionDecodableError: Error { case nonHTTPResponse(URLResponse) case serverResponse(ServerResponse) - public struct Deserialization { + public struct Deserialization: Sendable { public let statusCode: Int public let url: URL public let responseBody: Data public let underlyingError: Error? } - public struct ServerResponse { + public struct ServerResponse: Sendable { public let statusCode: Int public let url: URL public let responseBody: Data From e62bd32becc113a63e2d6aafd4fd0fb5362633a3 Mon Sep 17 00:00:00 2001 From: Emil Date: Fri, 2 Aug 2024 11:43:08 +0200 Subject: [PATCH 2/3] CR changes --- Sources/URLSessionDecodable/URLSessionDecodable.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/URLSessionDecodable/URLSessionDecodable.swift b/Sources/URLSessionDecodable/URLSessionDecodable.swift index d7df122..ea5eaa4 100644 --- a/Sources/URLSessionDecodable/URLSessionDecodable.swift +++ b/Sources/URLSessionDecodable/URLSessionDecodable.swift @@ -60,7 +60,7 @@ extension URLSession { method: HTTPMethod, parameters: ParametersEncoding?, headers: HTTPHeaders?, - decoder: @autoclosure @escaping @Sendable () -> DataDecoder + decoder: @autoclosure @Sendable () -> DataDecoder ) async throws -> T { let request = self.request(with: url, method: method, parameters: parameters, headers: headers) let (data, response) = try await data(for: request) @@ -76,7 +76,7 @@ extension URLSession { private static func handle( response: HTTPURLResponse, data: Data, - decoder: @autoclosure @escaping @Sendable () -> DataDecoder, + decoder: @autoclosure @Sendable () -> DataDecoder, url: URL ) -> Result { guard 200..<300 ~= response.statusCode else { From 1a5665b522ee0610b66cfb0dc3cf3b01e457ff84 Mon Sep 17 00:00:00 2001 From: Emil Date: Fri, 2 Aug 2024 12:14:07 +0200 Subject: [PATCH 3/3] Fix CI --- .github/workflows/continuous-integration-workflow.yml | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/continuous-integration-workflow.yml b/.github/workflows/continuous-integration-workflow.yml index 80c9598..3feed7c 100644 --- a/.github/workflows/continuous-integration-workflow.yml +++ b/.github/workflows/continuous-integration-workflow.yml @@ -10,13 +10,11 @@ on: jobs: tests: name: Tests - runs-on: macOS-latest + runs-on: macos-latest steps: - name: Checkout uses: actions/checkout@v2 - - name: Select Xcode 14.2 - run: sudo xcode-select -s /Applications/Xcode_14.2.app - - name: Run swiftlint - run: swiftlint + - name: Select Xcode 15.4 + run: sudo xcode-select -s /Applications/Xcode_15.4.app - name: Run tests run: swift test