Skip to content

Commit e48b152

Browse files
authored
Disable "respectsExistingLineBreaks" in .swift-format for more consistent styling (#23)
### 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
1 parent 7c6a9e7 commit e48b152

File tree

3 files changed

+53
-148
lines changed

3 files changed

+53
-148
lines changed

Diff for: .swift-format

+1-1
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,

Diff for: Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift

+39-101
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,7 @@ public struct AsyncHTTPClientTransport: ClientTransport {
115115

116116
// MARK: LocalizedError
117117

118-
var errorDescription: String? {
119-
description
120-
}
118+
var errorDescription: String? { description }
121119
}
122120

123121
/// A set of configuration values used by the transport.
@@ -130,21 +128,15 @@ public struct AsyncHTTPClientTransport: ClientTransport {
130128
/// - Parameters:
131129
/// - configuration: A set of configuration values used by the transport.
132130
/// - requestSender: The underlying request sender.
133-
internal init(
134-
configuration: Configuration,
135-
requestSender: any HTTPRequestSending
136-
) {
131+
internal init(configuration: Configuration, requestSender: any HTTPRequestSending) {
137132
self.configuration = configuration
138133
self.requestSender = requestSender
139134
}
140135

141136
/// Creates a new transport.
142137
/// - Parameter configuration: A set of configuration values used by the transport.
143138
public init(configuration: Configuration) {
144-
self.init(
145-
configuration: configuration,
146-
requestSender: AsyncHTTPRequestSender()
147-
)
139+
self.init(configuration: configuration, requestSender: AsyncHTTPRequestSender())
148140
}
149141

150142
// MARK: ClientTransport
@@ -159,40 +151,27 @@ public struct AsyncHTTPClientTransport: ClientTransport {
159151
///
160152
/// - Returns: A tuple containing the HTTP response and an optional HTTP body in the response.
161153
/// - Throws: An error if the request or response handling encounters any issues.
162-
public func send(
163-
_ request: HTTPRequest,
164-
body: HTTPBody?,
165-
baseURL: URL,
166-
operationID: String
167-
) async throws -> (HTTPResponse, HTTPBody?) {
154+
public func send(_ request: HTTPRequest, body: HTTPBody?, baseURL: URL, operationID: String) async throws -> (
155+
HTTPResponse, HTTPBody?
156+
) {
168157
let httpRequest = try Self.convertRequest(request, body: body, baseURL: baseURL)
169158
let httpResponse = try await invokeSession(with: httpRequest)
170-
let response = try await Self.convertResponse(
171-
method: request.method,
172-
httpResponse: httpResponse
173-
)
159+
let response = try await Self.convertResponse(method: request.method, httpResponse: httpResponse)
174160
return response
175161
}
176162

177163
// MARK: Internal
178164

179165
/// Converts the shared Request type into URLRequest.
180-
internal static func convertRequest(
181-
_ request: HTTPRequest,
182-
body: HTTPBody?,
183-
baseURL: URL
184-
) throws -> HTTPClientRequest {
185-
guard
186-
var baseUrlComponents = URLComponents(string: baseURL.absoluteString),
166+
internal static func convertRequest(_ request: HTTPRequest, body: HTTPBody?, baseURL: URL) throws
167+
-> HTTPClientRequest
168+
{
169+
guard var baseUrlComponents = URLComponents(string: baseURL.absoluteString),
187170
let requestUrlComponents = URLComponents(string: request.path ?? "")
188-
else {
189-
throw Error.invalidRequestURL(request: request, baseURL: baseURL)
190-
}
171+
else { throw Error.invalidRequestURL(request: request, baseURL: baseURL) }
191172
baseUrlComponents.percentEncodedPath += requestUrlComponents.percentEncodedPath
192173
baseUrlComponents.percentEncodedQuery = requestUrlComponents.percentEncodedQuery
193-
guard let url = baseUrlComponents.url else {
194-
throw Error.invalidRequestURL(request: request, baseURL: baseURL)
195-
}
174+
guard let url = baseUrlComponents.url else { throw Error.invalidRequestURL(request: request, baseURL: baseURL) }
196175
var clientRequest = HTTPClientRequest(url: url.absoluteString)
197176
clientRequest.method = request.method.asHTTPMethod
198177
for header in request.headerFields {
@@ -201,114 +180,73 @@ public struct AsyncHTTPClientTransport: ClientTransport {
201180
if let body {
202181
let length: HTTPClientRequest.Body.Length
203182
switch body.length {
204-
case .unknown:
205-
length = .unknown
206-
case .known(let count):
207-
length = .known(count)
183+
case .unknown: length = .unknown
184+
case .known(let count): length = .known(count)
208185
}
209-
clientRequest.body = .stream(
210-
body.map { .init(bytes: $0) },
211-
length: length
212-
)
186+
clientRequest.body = .stream(body.map { .init(bytes: $0) }, length: length)
213187
}
214188
return clientRequest
215189
}
216190

217191
/// Converts the received URLResponse into the shared Response.
218-
internal static func convertResponse(
219-
method: HTTPRequest.Method,
220-
httpResponse: HTTPClientResponse
221-
) async throws -> (HTTPResponse, HTTPBody?) {
192+
internal static func convertResponse(method: HTTPRequest.Method, httpResponse: HTTPClientResponse) async throws -> (
193+
HTTPResponse, HTTPBody?
194+
) {
222195

223196
var headerFields: HTTPFields = [:]
224-
for header in httpResponse.headers {
225-
headerFields[.init(header.name)!] = header.value
226-
}
197+
for header in httpResponse.headers { headerFields[.init(header.name)!] = header.value }
227198

228199
let length: HTTPBody.Length
229-
if let lengthHeaderString = headerFields[.contentLength],
230-
let lengthHeader = Int(lengthHeaderString)
231-
{
200+
if let lengthHeaderString = headerFields[.contentLength], let lengthHeader = Int(lengthHeaderString) {
232201
length = .known(lengthHeader)
233202
} else {
234203
length = .unknown
235204
}
236205

237206
let body: HTTPBody?
238207
switch method {
239-
case .head, .connect, .trace:
240-
body = nil
208+
case .head, .connect, .trace: body = nil
241209
default:
242-
body = HTTPBody(
243-
httpResponse.body.map { $0.readableBytesView },
244-
length: length,
245-
iterationBehavior: .single
246-
)
210+
body = HTTPBody(httpResponse.body.map { $0.readableBytesView }, length: length, iterationBehavior: .single)
247211
}
248212

249-
let response = HTTPResponse(
250-
status: .init(code: Int(httpResponse.status.code)),
251-
headerFields: headerFields
252-
)
213+
let response = HTTPResponse(status: .init(code: Int(httpResponse.status.code)), headerFields: headerFields)
253214
return (response, body)
254215
}
255216

256217
// MARK: Private
257218

258219
/// Makes the underlying HTTP call.
259220
private func invokeSession(with request: Request) async throws -> Response {
260-
try await requestSender.send(
261-
request: request,
262-
with: configuration.client,
263-
timeout: configuration.timeout
264-
)
221+
try await requestSender.send(request: request, with: configuration.client, timeout: configuration.timeout)
265222
}
266223
}
267224

268225
extension HTTPTypes.HTTPRequest.Method {
269226
var asHTTPMethod: NIOHTTP1.HTTPMethod {
270227
switch self {
271-
case .get:
272-
return .GET
273-
case .put:
274-
return .PUT
275-
case .post:
276-
return .POST
277-
case .delete:
278-
return .DELETE
279-
case .options:
280-
return .OPTIONS
281-
case .head:
282-
return .HEAD
283-
case .patch:
284-
return .PATCH
285-
case .trace:
286-
return .TRACE
287-
default:
288-
return .RAW(value: rawValue)
228+
case .get: return .GET
229+
case .put: return .PUT
230+
case .post: return .POST
231+
case .delete: return .DELETE
232+
case .options: return .OPTIONS
233+
case .head: return .HEAD
234+
case .patch: return .PATCH
235+
case .trace: return .TRACE
236+
default: return .RAW(value: rawValue)
289237
}
290238
}
291239
}
292240

293241
/// A type that performs HTTP operations using the HTTP client.
294242
internal protocol HTTPRequestSending: Sendable {
295-
func send(
296-
request: AsyncHTTPClientTransport.Request,
297-
with client: HTTPClient,
298-
timeout: TimeAmount
299-
) async throws -> AsyncHTTPClientTransport.Response
243+
func send(request: AsyncHTTPClientTransport.Request, with client: HTTPClient, timeout: TimeAmount) async throws
244+
-> AsyncHTTPClientTransport.Response
300245
}
301246

302247
/// Performs HTTP calls using AsyncHTTPClient
303248
internal struct AsyncHTTPRequestSender: HTTPRequestSending {
304-
func send(
305-
request: AsyncHTTPClientTransport.Request,
306-
with client: AsyncHTTPClient.HTTPClient,
307-
timeout: TimeAmount
308-
) async throws -> AsyncHTTPClientTransport.Response {
309-
try await client.execute(
310-
request,
311-
timeout: timeout
312-
)
313-
}
249+
func send(request: AsyncHTTPClientTransport.Request, with client: AsyncHTTPClient.HTTPClient, timeout: TimeAmount)
250+
async throws -> AsyncHTTPClientTransport.Response
251+
{ try await client.execute(request, timeout: timeout) }
314252
}

Diff for: Tests/OpenAPIAsyncHTTPClientTests/Test_AsyncHTTPClientTransport.swift

+13-46
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,12 @@ import HTTPTypes
2121

2222
class Test_AsyncHTTPClientTransport: XCTestCase {
2323

24-
static var testData: Data {
25-
get throws {
26-
try XCTUnwrap(#"[{}]"#.data(using: .utf8))
27-
}
28-
}
24+
static var testData: Data { get throws { try XCTUnwrap(#"[{}]"#.data(using: .utf8)) } }
2925

30-
static var testBuffer: ByteBuffer {
31-
ByteBuffer(string: #"[{}]"#)
32-
}
26+
static var testBuffer: ByteBuffer { ByteBuffer(string: #"[{}]"#) }
3327

3428
static var testUrl: URL {
35-
get throws {
36-
try XCTUnwrap(URL(string: "http://example.com/api/v1/hello/Maria?greeting=Howdy"))
37-
}
29+
get throws { try XCTUnwrap(URL(string: "http://example.com/api/v1/hello/Maria?greeting=Howdy")) }
3830
}
3931

4032
func testConvertRequest() throws {
@@ -43,9 +35,7 @@ class Test_AsyncHTTPClientTransport: XCTestCase {
4335
scheme: nil,
4436
authority: nil,
4537
path: "/hello%20world/Maria?greeting=Howdy",
46-
headerFields: [
47-
.contentType: "application/json"
48-
]
38+
headerFields: [.contentType: "application/json"]
4939
)
5040
let requestBody = try HTTPBody(Self.testData)
5141
let httpRequest = try AsyncHTTPClientTransport.convertRequest(
@@ -55,22 +45,15 @@ class Test_AsyncHTTPClientTransport: XCTestCase {
5545
)
5646
XCTAssertEqual(httpRequest.url, "http://example.com/api/v1/hello%20world/Maria?greeting=Howdy")
5747
XCTAssertEqual(httpRequest.method, .POST)
58-
XCTAssertEqual(
59-
httpRequest.headers,
60-
[
61-
"content-type": "application/json"
62-
]
63-
)
48+
XCTAssertEqual(httpRequest.headers, ["content-type": "application/json"])
6449
// TODO: Not sure how to test that httpRequest.body is what we expect, can't
6550
// find an API for reading it back.
6651
}
6752

6853
func testConvertResponse() async throws {
6954
let httpResponse = HTTPClientResponse(
7055
status: .ok,
71-
headers: [
72-
"content-type": "application/json"
73-
],
56+
headers: ["content-type": "application/json"],
7457
body: .bytes(Self.testBuffer)
7558
)
7659
let (response, maybeResponseBody) = try await AsyncHTTPClientTransport.convertResponse(
@@ -79,29 +62,19 @@ class Test_AsyncHTTPClientTransport: XCTestCase {
7962
)
8063
let responseBody = try XCTUnwrap(maybeResponseBody)
8164
XCTAssertEqual(response.status.code, 200)
82-
XCTAssertEqual(
83-
response.headerFields,
84-
[
85-
.contentType: "application/json"
86-
]
87-
)
65+
XCTAssertEqual(response.headerFields, [.contentType: "application/json"])
8866
let bufferedResponseBody = try await Data(collecting: responseBody, upTo: .max)
8967
XCTAssertEqual(bufferedResponseBody, try Self.testData)
9068
}
9169

9270
func testSend() async throws {
93-
let transport = AsyncHTTPClientTransport(
94-
configuration: .init(),
95-
requestSender: TestSender.test
96-
)
71+
let transport = AsyncHTTPClientTransport(configuration: .init(), requestSender: TestSender.test)
9772
let request: HTTPRequest = .init(
9873
method: .get,
9974
scheme: nil,
10075
authority: nil,
10176
path: "/api/v1/hello/Maria",
102-
headerFields: [
103-
.init("x-request")!: "yes"
104-
]
77+
headerFields: [.init("x-request")!: "yes"]
10578
)
10679
let (response, maybeResponseBody) = try await transport.send(
10780
request,
@@ -120,22 +93,16 @@ struct TestSender: HTTPRequestSending {
12093
var sendClosure:
12194
@Sendable (AsyncHTTPClientTransport.Request, HTTPClient, TimeAmount) async throws ->
12295
AsyncHTTPClientTransport.Response
123-
func send(
124-
request: AsyncHTTPClientTransport.Request,
125-
with client: HTTPClient,
126-
timeout: TimeAmount
127-
) async throws -> AsyncHTTPClientTransport.Response {
128-
try await sendClosure(request, client, timeout)
129-
}
96+
func send(request: AsyncHTTPClientTransport.Request, with client: HTTPClient, timeout: TimeAmount) async throws
97+
-> AsyncHTTPClientTransport.Response
98+
{ try await sendClosure(request, client, timeout) }
13099

131100
static var test: Self {
132101
TestSender { request, _, _ in
133102
XCTAssertEqual(request.headers.first(name: "x-request"), "yes")
134103
return HTTPClientResponse(
135104
status: .ok,
136-
headers: [
137-
"content-type": "application/json"
138-
],
105+
headers: ["content-type": "application/json"],
139106
body: .bytes(Test_AsyncHTTPClientTransport.testBuffer)
140107
)
141108
}

0 commit comments

Comments
 (0)