Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare for Swift 6 #89

Merged
merged 11 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/matrix.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"xcode_version": [
"15.2",
"15.3"
"15.4"
],
"swift_version": [
"5.9",
Expand Down
3 changes: 3 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ disabled_rules:
- file_header
- file_name
- file_types_order
- force_unwrapping
- function_default_parameter_at_end
- lower_acl_than_parent
- missing_docs
- no_extension_access_modifier
- no_grouping_extension
- object_literal
- prefer_nimble
- prefixed_toplevel_constant
- prohibited_interface_builder
- required_deinit
Expand All @@ -30,6 +32,7 @@ disabled_rules:
- switch_case_on_newline
- unowned_variable_capture
- unused_optional_binding
- unused_parameter
- vertical_whitespace_between_cases
line_length:
warning: 180
Expand Down
2 changes: 1 addition & 1 deletion MultipartFormDataParser.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ Pod::Spec.new do |spec|

spec.source = { :git => "https://github.com/417-72KI/#{spec.name}.git", :tag => "#{spec.version}" }
spec.source_files = 'Sources/MultipartFormDataParser/**/*.swift'
spec.swift_versions = ['5.8', '5.9', '5.10']
spec.swift_versions = ['5.9', '5.10']
spec.frameworks = 'Foundation'
end
37 changes: 37 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,40 @@ if isDevelop {
}
}
}

// MARK: - Upcoming feature flags for Swift 6
package.targets.forEach {
$0.swiftSettings = [
// .forwardTrailingClosures,
.existentialAny,
.bareSlashRegexLiterals,
.conciseMagicFile,
.importObjcForwardDeclarations,
.disableOutwardActorInference,
// TODO: enable when 5.9 dropped
// .deprecateApplicationMain,
// .isolatedDefaultValues,
// .globalConcurrency,
]
}

// ref: https://github.com/treastrain/swift-upcomingfeatureflags-cheatsheet
private extension SwiftSetting {
static let forwardTrailingClosures: Self = .enableUpcomingFeature("ForwardTrailingClosures") // SE-0286, Swift 5.3, SwiftPM 5.8+
static let existentialAny: Self = .enableUpcomingFeature("ExistentialAny") // SE-0335, Swift 5.6, SwiftPM 5.8+
static let bareSlashRegexLiterals: Self = .enableUpcomingFeature("BareSlashRegexLiterals") // SE-0354, Swift 5.7, SwiftPM 5.8+
static let conciseMagicFile: Self = .enableUpcomingFeature("ConciseMagicFile") // SE-0274, Swift 5.8, SwiftPM 5.8+
static let importObjcForwardDeclarations: Self = .enableUpcomingFeature("ImportObjcForwardDeclarations") // SE-0384, Swift 5.9, SwiftPM 5.9+
static let disableOutwardActorInference: Self = .enableUpcomingFeature("DisableOutwardActorInference") // SE-0401, Swift 5.9, SwiftPM 5.9+
static let deprecateApplicationMain: Self = .enableUpcomingFeature("DeprecateApplicationMain") // SE-0383, Swift 5.10, SwiftPM 5.10+
static let isolatedDefaultValues: Self = .enableUpcomingFeature("IsolatedDefaultValues") // SE-0411, Swift 5.10, SwiftPM 5.10+
static let globalConcurrency: Self = .enableUpcomingFeature("GlobalConcurrency") // SE-0412, Swift 5.10, SwiftPM 5.10+
}

// MARK: - Enabling Complete Concurrency Checking for Swift 6
// ref: https://www.swift.org/documentation/concurrency/
package.targets.forEach {
var settings = $0.swiftSettings ?? []
settings.append(.enableExperimentalFeature("StrictConcurrency"))
$0.swiftSettings = settings
}
6 changes: 2 additions & 4 deletions Sources/MultipartFormDataParser/MultipartFormData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@ public extension MultipartFormData {
}

public extension MultipartFormData.Element {
// swiftlint:disable:next non_optional_string_data_conversion
var string: String? { String(data: data, encoding: .utf8) } // binaries should be nil
var string: String? { String(data: data, encoding: .utf8) }
}

extension MultipartFormData.Element {
static func from(_ data: [Data]) -> Self {
var element = Self(name: "", data: .init(), fileName: nil, mimeType: nil)
for line in data {
// swiftlint:disable:next non_optional_string_data_conversion
guard let string = String(data: line, encoding: .utf8) else { // binaries should be nil
guard let string = String(data: line, encoding: .utf8) else {
element.data = line
continue
}
Expand Down
28 changes: 28 additions & 0 deletions TestPlan.xctestplan
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"configurations" : [
{
"id" : "D06DC996-5C46-43C0-A9F0-BF25ED72E3C9",
"name" : "Default",
"options" : {

}
}
],
"defaultOptions" : {
"maximumTestRepetitions" : 5,
"nsZombieEnabled" : true,
"repeatInNewRunnerProcess" : true,
"testRepetitionMode" : "retryOnFailure",
"testTimeoutsEnabled" : true
},
"testTargets" : [
{
"target" : {
"containerPath" : "container:",
"identifier" : "MultipartFormDataParserTests",
"name" : "MultipartFormDataParserTests"
}
}
],
"version" : 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import FoundationNetworking

// `URLSession` in `FoundationNetworking` does not support `async`/`await`.
extension URLSession {
public func data(for request: URLRequest, delegate: (URLSessionTaskDelegate)? = nil) async throws -> (Data, URLResponse) {
public func data(for request: URLRequest, delegate: (any URLSessionTaskDelegate)? = nil) async throws -> (Data, URLResponse) {
try await withCheckedThrowingContinuation { continuation in
let task = dataTask(with: request) { data, res, err in
if let err = err {
Expand All @@ -20,11 +20,11 @@ extension URLSession {
}
}

public func data(from url: URL, delegate: (URLSessionTaskDelegate)? = nil) async throws -> (Data, URLResponse) {
public func data(from url: URL, delegate: (any URLSessionTaskDelegate)? = nil) async throws -> (Data, URLResponse) {
try await data(for: URLRequest(url: url), delegate: delegate)
}

public func upload(for request: URLRequest, fromFile fileURL: URL, delegate: (URLSessionTaskDelegate)? = nil) async throws -> (Data, URLResponse) {
public func upload(for request: URLRequest, fromFile fileURL: URL, delegate: (any URLSessionTaskDelegate)? = nil) async throws -> (Data, URLResponse) {
try await withCheckedThrowingContinuation { continuation in
let task = uploadTask(with: request, fromFile: fileURL) { data, res, err in
if let err = err {
Expand All @@ -40,7 +40,7 @@ extension URLSession {
}
}

public func upload(for request: URLRequest, from bodyData: Data, delegate: (URLSessionTaskDelegate)? = nil) async throws -> (Data, URLResponse) {
public func upload(for request: URLRequest, from bodyData: Data, delegate: (any URLSessionTaskDelegate)? = nil) async throws -> (Data, URLResponse) {
try await withCheckedThrowingContinuation { continuation in
let task = uploadTask(with: request, from: bodyData) { data, res, err in
if let err = err {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,24 @@ extension XCTestCase {
genbaNeko: Data,
denwaNeko: Data,
message: Data,
file: StaticString = #file,
file: StaticString = #filePath,
line: UInt = #line
) throws -> URLRequest {
try TestRequest(genbaNeko: genbaNeko,
denwaNeko: denwaNeko,
message: message,
file: file,
line: line)
.buildURLRequest()
try TestRequest(
genbaNeko: genbaNeko,
denwaNeko: denwaNeko,
message: message,
file: file,
line: line
).buildURLRequest()
}

func uploadWithAPIKit(
genbaNeko: Data,
denwaNeko: Data,
message: Data,
retryCount: UInt = 3,
file: StaticString = #file,
file: StaticString = #filePath,
line: UInt = #line
) throws -> TestEntity {
let exp = expectation(description: "response")
Expand Down Expand Up @@ -79,7 +80,7 @@ private struct TestRequest: APIKit.Request {
var file: StaticString
var line: UInt

var bodyParameters: BodyParameters? {
var bodyParameters: (any BodyParameters)? {
let parts: [MultipartFormDataBodyParameters.Part] = [
.init(
data: genbaNeko,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension XCTestCase {
denwaNeko: Data,
message: Data,
retryCount: UInt = 3,
file: StaticString = #file,
file: StaticString = #filePath,
line: UInt = #line
) throws -> TestEntity? {
let exp = expectation(description: "response")
Expand All @@ -42,12 +42,11 @@ extension XCTestCase {
var response: AFDataResponse<TestEntity>!
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
task.responseDecodable(of: TestEntity.self,
decoder: decoder) {
task.responseDecodable(of: TestEntity.self, decoder: decoder) {
response = $0
exp.fulfill()
}
waitForExpectations(timeout: timeoutInterval)
wait(for: [exp], timeout: timeoutInterval)

XCTAssertNotNil(response, file: file, line: line)
XCTAssertEqual(response?.response?.statusCode, 200, file: file, line: line)
Expand All @@ -62,7 +61,7 @@ extension XCTestCase {
denwaNeko: Data,
message: Data,
retryCount: UInt = 3,
file: StaticString = #file,
file: StaticString = #filePath,
line: UInt = #line
) async throws -> TestEntity {
let decoder = JSONDecoder()
Expand Down Expand Up @@ -94,7 +93,7 @@ extension XCTestCase {
private class Interceptor: RequestInterceptor {
private let lock = NSLock()

func retry(_ request: Request, for session: Session, dueTo error: Error, completion: @escaping (RetryResult) -> Void) {
func retry(_ request: Request, for session: Session, dueTo error: any Error, completion: @escaping (RetryResult) -> Void) {
lock.lock(); defer { lock.unlock() }

if request.retryCount < 3 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension XCTestCase {
denwaNeko: Data,
message: Data,
retryCount: UInt = 3,
file: StaticString = #file,
file: StaticString = #filePath,
line: UInt = #line
) throws -> TestEntity? {
let exp = expectation(description: "response")
Expand All @@ -33,7 +33,7 @@ extension XCTestCase {
result = $0
exp.fulfill()
}
waitForExpectations(timeout: timeoutInterval)
wait(for: [exp], timeout: timeoutInterval)

XCTAssertNotNil(result)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension XCTestCase {
denwaNeko: Data,
message: Data,
retryCount: UInt = 3,
file: StaticString = #file,
file: StaticString = #filePath,
line: UInt = #line
) async throws -> TestEntity {
let request = createRequest(genbaNeko: genbaNeko, denwaNeko: denwaNeko, message: message)
Expand All @@ -39,7 +39,7 @@ extension XCTestCase {
denwaNeko: Data,
message: Data,
retryCount: UInt = 3,
file: StaticString = #file,
file: StaticString = #filePath,
line: UInt = #line
) async throws -> TestEntity {
let boundary = "YoWatanabe0417"
Expand Down Expand Up @@ -70,7 +70,7 @@ extension XCTestCase {
genbaNeko: Data,
denwaNeko: Data,
message: Data,
file: StaticString = #file,
file: StaticString = #filePath,
line: UInt = #line
) -> URLRequest {
let boundary = "YoWatanabe0417"
Expand Down
Loading