Skip to content

Commit 22ec9a6

Browse files
PARAIPAN9czechboy0
andauthored
Disable "respectsExistingLineBreaks" in .swift-format for more consistent styling (#20)
### Motivation - Relates to [#230](apple/swift-openapi-generator#230) ### Modifications - Disable respectsExistingLineBreaks .swift-format rule and address changes requested ### Result - One of the .swift-format rules will be disabled ### Test Plan - Run Tests Co-authored-by: Honza Dvorsky <[email protected]>
1 parent c1c96c7 commit 22ec9a6

File tree

4 files changed

+29
-82
lines changed

4 files changed

+29
-82
lines changed

.swift-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"lineLength" : 120,
1515
"maximumBlankLines" : 1,
1616
"prioritizeKeepingFunctionOutputTogether" : false,
17-
"respectsExistingLineBreaks" : true,
17+
"respectsExistingLineBreaks" : false,
1818
"rules" : {
1919
"AllPublicDeclarationsHaveDocumentation" : true,
2020
"AlwaysUseLowerCamelCase" : false,

Sources/OpenAPIURLSession/URLSessionTransport.swift

Lines changed: 19 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,15 @@ public struct URLSessionTransport: ClientTransport {
7474
/// Creates a new configuration with the provided session.
7575
/// - Parameter session: The URLSession used for performing HTTP operations.
7676
/// If none is provided, the system uses the shared URLSession.
77-
public init(session: URLSession = .shared) {
78-
self.session = session
79-
}
77+
public init(session: URLSession = .shared) { self.session = session }
8078
}
8179

8280
/// A set of configuration values used by the transport.
8381
public var configuration: Configuration
8482

8583
/// Creates a new URLSession-based transport.
8684
/// - Parameter configuration: A set of configuration values used by the transport.
87-
public init(configuration: Configuration = .init()) {
88-
self.configuration = configuration
89-
}
85+
public init(configuration: Configuration = .init()) { self.configuration = configuration }
9086

9187
/// Asynchronously sends an HTTP request and returns the response and body.
9288
///
@@ -97,20 +93,13 @@ public struct URLSessionTransport: ClientTransport {
9793
/// - operationID: An optional identifier for the operation or request.
9894
/// - Returns: A tuple containing the HTTP response and an optional HTTP response body.
9995
/// - Throws: An error if there is a problem sending the request or processing the response.
100-
public func send(
101-
_ request: HTTPRequest,
102-
body: HTTPBody?,
103-
baseURL: URL,
104-
operationID: String
105-
) async throws -> (HTTPResponse, HTTPBody?) {
96+
public func send(_ request: HTTPRequest, body: HTTPBody?, baseURL: URL, operationID: String) async throws -> (
97+
HTTPResponse, HTTPBody?
98+
) {
10699
// TODO: https://github.com/apple/swift-openapi-generator/issues/301
107100
let urlRequest = try await URLRequest(request, body: body, baseURL: baseURL)
108101
let (responseBody, urlResponse) = try await invokeSession(urlRequest)
109-
return try HTTPResponse.response(
110-
method: request.method,
111-
urlResponse: urlResponse,
112-
data: responseBody
113-
)
102+
return try HTTPResponse.response(method: request.method, urlResponse: urlResponse, data: responseBody)
114103
}
115104

116105
private func invokeSession(_ urlRequest: URLRequest) async throws -> (Data, URLResponse) {
@@ -124,15 +113,11 @@ public struct URLSessionTransport: ClientTransport {
124113
}
125114

126115
guard let response else {
127-
continuation.resume(
128-
with: .failure(URLSessionTransportError.noResponse(url: urlRequest.url))
129-
)
116+
continuation.resume(with: .failure(URLSessionTransportError.noResponse(url: urlRequest.url)))
130117
return
131118
}
132119

133-
continuation.resume(
134-
with: .success((data ?? Data(), response))
135-
)
120+
continuation.resume(with: .success((data ?? Data(), response)))
136121
}
137122
.resume()
138123
}
@@ -153,46 +138,31 @@ internal enum URLSessionTransportError: Error {
153138
}
154139

155140
extension HTTPResponse {
156-
static func response(
157-
method: HTTPRequest.Method,
158-
urlResponse: URLResponse,
159-
data: Data
160-
) throws -> (HTTPResponse, HTTPBody?) {
141+
static func response(method: HTTPRequest.Method, urlResponse: URLResponse, data: Data) throws -> (
142+
HTTPResponse, HTTPBody?
143+
) {
161144
guard let httpResponse = urlResponse as? HTTPURLResponse else {
162145
throw URLSessionTransportError.notHTTPResponse(urlResponse)
163146
}
164147
var headerFields = HTTPFields()
165148
for (headerName, headerValue) in httpResponse.allHeaderFields {
166-
guard
167-
let rawName = headerName as? String,
168-
let name = HTTPField.Name(rawName),
149+
guard let rawName = headerName as? String, let name = HTTPField.Name(rawName),
169150
let value = headerValue as? String
170-
else {
171-
continue
172-
}
151+
else { continue }
173152
headerFields[name] = value
174153
}
175154
let body: HTTPBody?
176155
switch method {
177-
case .head, .connect, .trace:
178-
body = nil
179-
default:
180-
body = .init(data)
156+
case .head, .connect, .trace: body = nil
157+
default: body = .init(data)
181158
}
182-
return (
183-
HTTPResponse(
184-
status: .init(code: httpResponse.statusCode),
185-
headerFields: headerFields
186-
),
187-
body
188-
)
159+
return (HTTPResponse(status: .init(code: httpResponse.statusCode), headerFields: headerFields), body)
189160
}
190161
}
191162

192163
extension URLRequest {
193164
init(_ request: HTTPRequest, body: HTTPBody?, baseURL: URL) async throws {
194-
guard
195-
var baseUrlComponents = URLComponents(string: baseURL.absoluteString),
165+
guard var baseUrlComponents = URLComponents(string: baseURL.absoluteString),
196166
let requestUrlComponents = URLComponents(string: request.path ?? "")
197167
else {
198168
throw URLSessionTransportError.invalidRequestURL(
@@ -206,11 +176,7 @@ extension URLRequest {
206176
baseUrlComponents.percentEncodedPath += path
207177
baseUrlComponents.percentEncodedQuery = requestUrlComponents.percentEncodedQuery
208178
guard let url = baseUrlComponents.url else {
209-
throw URLSessionTransportError.invalidRequestURL(
210-
path: path,
211-
method: request.method,
212-
baseURL: baseURL
213-
)
179+
throw URLSessionTransportError.invalidRequestURL(path: path, method: request.method, baseURL: baseURL)
214180
}
215181
self.init(url: url)
216182
self.httpMethod = request.method.rawValue
@@ -238,8 +204,7 @@ extension URLSessionTransportError: CustomStringConvertible {
238204
"Invalid request URL from request path: \(path), method: \(method), relative to base URL: \(baseURL.absoluteString)"
239205
case .notHTTPResponse(let response):
240206
return "Received a non-HTTP response, of type: \(String(describing: type(of: response)))"
241-
case .noResponse(let url):
242-
return "Received a nil response for \(url?.absoluteString ?? "<nil URL>")"
207+
case .noResponse(let url): return "Received a nil response for \(url?.absoluteString ?? "<nil URL>")"
243208
}
244209
}
245210
}

Tests/OpenAPIURLSessionTests/Locking.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ import Foundation
2020
/// performed manually using locks.
2121
///
2222
/// Note: Use the `package` access modifier once min Swift version is increased.
23-
@_spi(Locking)
24-
public final class LockedValueBox<Value: Sendable>: @unchecked Sendable {
23+
@_spi(Locking) public final class LockedValueBox<Value: Sendable>: @unchecked Sendable {
2524
private let lock: NSLock = {
2625
let lock = NSLock()
2726
lock.name = "com.apple.swift-openapi-urlsession.lock.LockedValueBox"
@@ -31,9 +30,7 @@ public final class LockedValueBox<Value: Sendable>: @unchecked Sendable {
3130
/// Initializes a new `LockedValueBox` instance with the provided initial value.
3231
///
3332
/// - Parameter value: The initial value to store in the `LockedValueBox`.
34-
public init(_ value: Value) {
35-
self.value = value
36-
}
33+
public init(_ value: Value) { self.value = value }
3734
/// Perform an operation on the value in a synchronized manner.
3835
///
3936
/// - Parameter work: A closure that takes an inout reference to the wrapped value and returns a result.
@@ -42,9 +39,7 @@ public final class LockedValueBox<Value: Sendable>: @unchecked Sendable {
4239
/// - Returns: The result of the closure passed to `work`.
4340
public func withValue<R>(_ work: (inout Value) throws -> R) rethrows -> R {
4441
lock.lock()
45-
defer {
46-
lock.unlock()
47-
}
42+
defer { lock.unlock() }
4843
return try work(&value)
4944
}
5045
}

Tests/OpenAPIURLSessionTests/URLSessionTransportTests.swift

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,10 @@ class URLSessionTransportTests: XCTestCase {
3737
scheme: nil,
3838
authority: nil,
3939
path: "/hello%20world/Maria?greeting=Howdy",
40-
headerFields: [
41-
.init("x-mumble2")!: "mumble"
42-
]
40+
headerFields: [.init("x-mumble2")!: "mumble"]
4341
)
4442
let body: HTTPBody = "👋"
45-
let urlRequest = try await URLRequest(
46-
request,
47-
body: body,
48-
baseURL: URL(string: "http://example.com/api")!
49-
)
43+
let urlRequest = try await URLRequest(request, body: body, baseURL: URL(string: "http://example.com/api")!)
5044
XCTAssertEqual(urlRequest.url, URL(string: "http://example.com/api/hello%20world/Maria?greeting=Howdy"))
5145
XCTAssertEqual(urlRequest.httpMethod, "POST")
5246
XCTAssertEqual(urlRequest.allHTTPHeaderFields?.count, 1)
@@ -91,9 +85,7 @@ class URLSessionTransportTests: XCTestCase {
9185
scheme: nil,
9286
authority: nil,
9387
path: "/hello%20world/Maria?greeting=Howdy",
94-
headerFields: [
95-
.init("x-mumble1")!: "mumble"
96-
]
88+
headerFields: [.init("x-mumble1")!: "mumble"]
9789
)
9890
let requestBody: HTTPBody = "👋"
9991
let (response, maybeResponseBody) = try await transport.send(
@@ -128,18 +120,13 @@ class MockURLProtocol: URLProtocol {
128120
override func startLoading() {
129121
Self.recordedHTTPRequests.withValue { $0.append(self.request) }
130122
guard let url = self.request.url else { return }
131-
guard let response = Self.mockHTTPResponses.withValue({ $0[url] }) else {
132-
return
133-
}
123+
guard let response = Self.mockHTTPResponses.withValue({ $0[url] }) else { return }
134124
switch response {
135125
case .success(let mockResponse):
136126
client?.urlProtocol(self, didReceive: mockResponse.response, cacheStoragePolicy: .notAllowed)
137-
if let data = mockResponse.body {
138-
client?.urlProtocol(self, didLoad: data)
139-
}
127+
if let data = mockResponse.body { client?.urlProtocol(self, didLoad: data) }
140128
client?.urlProtocolDidFinishLoading(self)
141-
case let .failure(error):
142-
client?.urlProtocol(self, didFailWithError: error)
129+
case let .failure(error): client?.urlProtocol(self, didFailWithError: error)
143130
}
144131
}
145132

0 commit comments

Comments
 (0)