Skip to content

Commit e043f6e

Browse files
Merge pull request #30 from NeedleInAJayStack/refactor/swift-format
Converts to swift-format
2 parents 321c781 + 852b657 commit e043f6e

9 files changed

Lines changed: 66 additions & 52 deletions

File tree

.swift-format

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"version": 1,
3+
"indentation" : {
4+
"spaces" : 4
5+
},
6+
"lineBreakBeforeEachArgument": true
7+
}

Sources/AsyncDataLoader/Channel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ extension Channel {
4141
try await withCheckedThrowingContinuation { continuation in
4242
Task {
4343
switch result {
44-
case let .success(success):
44+
case .success(let success):
4545
continuation.resume(returning: success)
46-
case let .failure(failure):
46+
case .failure(let failure):
4747
continuation.resume(throwing: failure)
4848
case nil:
4949
waiters.append(continuation)

Sources/AsyncDataLoader/DataLoader.swift

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,16 @@ public actor DataLoader<Key: Hashable & Sendable, Value: Sendable> {
6565
let results = try await self.batchLoadFunction([key])
6666

6767
if results.isEmpty {
68-
await channel
69-
.fail(
70-
DataLoaderError
71-
.noValueForKey("Did not return value for key: \(key)")
72-
)
68+
await channel.fail(
69+
DataLoaderError.noValueForKey("Did not return value for key: \(key)")
70+
)
7371
} else {
7472
let result = results[0]
7573

7674
switch result {
77-
case let .success(value):
75+
case .success(let value):
7876
await channel.fulfill(value)
79-
case let .failure(error):
77+
case .failure(let error):
8078
await channel.fail(error)
8179
}
8280
}
@@ -198,19 +196,18 @@ public actor DataLoader<Key: Hashable & Sendable, Value: Sendable> {
198196
let values = try await batchLoadFunction(keys)
199197

200198
if values.count != keys.count {
201-
throw DataLoaderError
202-
.typeError(
203-
"The function did not return an array of the same length as the array of keys. \nKeys count: \(keys.count)\nValues count: \(values.count)"
204-
)
199+
throw DataLoaderError.typeError(
200+
"The function did not return an array of the same length as the array of keys. \nKeys count: \(keys.count)\nValues count: \(values.count)"
201+
)
205202
}
206203

207204
for entry in batch.enumerated() {
208205
let result = values[entry.offset]
209206

210207
switch result {
211-
case let .failure(error):
208+
case .failure(let error):
212209
await entry.element.channel.fail(error)
213-
case let .success(value):
210+
case .success(let value):
214211
await entry.element.channel.fulfill(value)
215212
}
216213
}

Sources/DataLoader/DataLoader.swift

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ public final class DataLoader<Key: Hashable, Value> {
5959
do {
6060
_ = try batchLoadFunction([key]).map { results in
6161
if results.isEmpty {
62-
promise
63-
.fail(
64-
DataLoaderError
65-
.noValueForKey("Did not return value for key: \(key)")
62+
promise.fail(
63+
DataLoaderError.noValueForKey(
64+
"Did not return value for key: \(key)"
6665
)
66+
)
6767
} else {
6868
let result = results[0]
6969
switch result {
70-
case let .success(value): promise.succeed(value)
71-
case let .failure(error): promise.fail(error)
70+
case .success(let value): promise.succeed(value)
71+
case .failure(let error): promise.fail(error)
7272
}
7373
}
7474
}
@@ -179,10 +179,10 @@ public final class DataLoader<Key: Hashable, Value> {
179179
// If a maxBatchSize was provided and the queue is longer, then segment the
180180
// queue into multiple batches, otherwise treat the queue as a single batch.
181181
if let maxBatchSize = options.maxBatchSize, maxBatchSize > 0, maxBatchSize < batch.count {
182-
for i in 0 ... (batch.count / maxBatchSize) {
182+
for i in 0...(batch.count / maxBatchSize) {
183183
let startIndex = i * maxBatchSize
184184
let endIndex = (i + 1) * maxBatchSize
185-
let slicedBatch = batch[startIndex ..< min(endIndex, batch.count)]
185+
let slicedBatch = batch[startIndex..<min(endIndex, batch.count)]
186186
try executeBatch(batch: Array(slicedBatch))
187187
}
188188
} else {
@@ -202,18 +202,17 @@ public final class DataLoader<Key: Hashable, Value> {
202202
do {
203203
_ = try batchLoadFunction(keys).flatMapThrowing { values in
204204
if values.count != keys.count {
205-
throw DataLoaderError
206-
.typeError(
207-
"The function did not return an array of the same length as the array of keys. \nKeys count: \(keys.count)\nValues count: \(values.count)"
208-
)
205+
throw DataLoaderError.typeError(
206+
"The function did not return an array of the same length as the array of keys. \nKeys count: \(keys.count)\nValues count: \(values.count)"
207+
)
209208
}
210209

211210
for entry in batch.enumerated() {
212211
let result = values[entry.offset]
213212

214213
switch result {
215-
case let .failure(error): entry.element.promise.fail(error)
216-
case let .success(value): entry.element.promise.succeed(value)
214+
case .failure(let error): entry.element.promise.fail(error)
215+
case .success(let value): entry.element.promise.succeed(value)
217216
}
218217
}
219218
}.recover { error in
@@ -238,25 +237,30 @@ public final class DataLoader<Key: Hashable, Value> {
238237
public typealias ConcurrentBatchLoadFunction<Key, Value> =
239238
@Sendable (_ keys: [Key]) async throws -> [DataLoaderFutureValue<Value>]
240239

241-
public extension DataLoader {
240+
extension DataLoader {
242241
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
243-
convenience init(
242+
public convenience init(
244243
on eventLoop: EventLoop,
245244
options: DataLoaderOptions<Key, Value> = DataLoaderOptions(),
246245
throwing asyncThrowingLoadFunction: @escaping ConcurrentBatchLoadFunction<Key, Value>
247246
) {
248-
self.init(options: options, batchLoadFunction: { keys in
249-
let promise = eventLoop.next().makePromise(of: [DataLoaderFutureValue<Value>].self)
250-
promise.completeWithTask {
251-
try await asyncThrowingLoadFunction(keys)
247+
self.init(
248+
options: options,
249+
batchLoadFunction: { keys in
250+
let promise = eventLoop.next().makePromise(
251+
of: [DataLoaderFutureValue<Value>].self
252+
)
253+
promise.completeWithTask {
254+
try await asyncThrowingLoadFunction(keys)
255+
}
256+
return promise.futureResult
252257
}
253-
return promise.futureResult
254-
})
258+
)
255259
}
256260

257261
/// Asynchronously loads a key, returning the value represented by that key.
258262
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
259-
func load(key: Key, on eventLoopGroup: EventLoopGroup) async throws -> Value {
263+
public func load(key: Key, on eventLoopGroup: EventLoopGroup) async throws -> Value {
260264
try await load(key: key, on: eventLoopGroup).get()
261265
}
262266

@@ -274,7 +278,8 @@ public final class DataLoader<Key: Hashable, Value> {
274278
/// let aAndB = try await a + b
275279
/// ```
276280
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
277-
func loadMany(keys: [Key], on eventLoopGroup: EventLoopGroup) async throws -> [Value] {
281+
public func loadMany(keys: [Key], on eventLoopGroup: EventLoopGroup) async throws -> [Value]
282+
{
278283
try await loadMany(keys: keys, on: eventLoopGroup).get()
279284
}
280285
}

Tests/AsyncDataLoaderTests/DataLoaderAbuseTests.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
@testable import AsyncDataLoader
21
import XCTest
32

3+
@testable import AsyncDataLoader
4+
45
/// Provides descriptive error messages for API abuse
56
class DataLoaderAbuseTests: XCTestCase {
67
func testFuntionWithNoValues() async throws {
@@ -24,7 +25,7 @@ class DataLoaderAbuseTests: XCTestCase {
2425
}
2526

2627
func testBatchFuntionMustPromiseAnArrayOfCorrectLength() async {
27-
let identityLoader = DataLoader<Int, Int>() { _ in
28+
let identityLoader = DataLoader<Int, Int> { _ in
2829
[]
2930
}
3031

@@ -42,7 +43,7 @@ class DataLoaderAbuseTests: XCTestCase {
4243
}
4344

4445
func testBatchFuntionWithSomeValues() async throws {
45-
let identityLoader = DataLoader<Int, Int>() { keys in
46+
let identityLoader = DataLoader<Int, Int> { keys in
4647
var results = [DataLoaderValue<Int>]()
4748

4849
for key in keys {

Tests/AsyncDataLoaderTests/DataLoaderTests.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
@testable import AsyncDataLoader
21
import XCTest
32

3+
@testable import AsyncDataLoader
4+
45
let sleepConstant = UInt64(2_000_000)
56

67
actor Concurrent<T> {
@@ -39,7 +40,7 @@ final class DataLoaderTests: XCTestCase {
3940

4041
/// Supports loading multiple keys in one call
4142
func testLoadingMultipleKeys() async throws {
42-
let identityLoader = DataLoader<Int, Int>() { keys in
43+
let identityLoader = DataLoader<Int, Int> { keys in
4344
keys.map { DataLoaderValue.success($0) }
4445
}
4546

@@ -618,7 +619,7 @@ final class DataLoaderTests: XCTestCase {
618619
var didFailWithErrorText2 = ""
619620

620621
switch didFailWithError2 {
621-
case let .typeError(text):
622+
case .typeError(let text):
622623
didFailWithErrorText2 = text
623624
case .noValueForKey:
624625
break
@@ -648,7 +649,7 @@ final class DataLoaderTests: XCTestCase {
648649
var didFailWithErrorText3 = ""
649650

650651
switch didFailWithError3 {
651-
case let .typeError(text):
652+
case .typeError(let text):
652653
didFailWithErrorText3 = text
653654
case .noValueForKey:
654655
break

Tests/DataLoaderTests/DataLoaderAbuseTests.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
@testable import DataLoader
21
import NIOPosix
32
import XCTest
43

4+
@testable import DataLoader
5+
56
/// Provides descriptive error messages for API abuse
67
class DataLoaderAbuseTests: XCTestCase {
78
func testFuntionWithNoValues() throws {
@@ -30,7 +31,7 @@ class DataLoaderAbuseTests: XCTestCase {
3031
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
3132
}
3233

33-
let identityLoader = DataLoader<Int, Int>() { _ in
34+
let identityLoader = DataLoader<Int, Int> { _ in
3435
eventLoopGroup.next().makeSucceededFuture([])
3536
}
3637

@@ -48,7 +49,7 @@ class DataLoaderAbuseTests: XCTestCase {
4849
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
4950
}
5051

51-
let identityLoader = DataLoader<Int, Int>() { keys in
52+
let identityLoader = DataLoader<Int, Int> { keys in
5253
var results = [DataLoaderFutureValue<Int>]()
5354

5455
for key in keys {

Tests/DataLoaderTests/DataLoaderAsyncTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
@testable import DataLoader
21
import NIOPosix
32
import XCTest
43

4+
@testable import DataLoader
5+
56
#if compiler(>=5.5) && canImport(_Concurrency)
67

78
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)

Tests/DataLoaderTests/DataLoaderTests.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
@testable import DataLoader
21
import NIOCore
32
import NIOPosix
43
import XCTest
54

5+
@testable import DataLoader
6+
67
/// Primary API
78
final class DataLoaderTests: XCTestCase {
89
/// Builds a really really simple data loader'
@@ -32,7 +33,7 @@ final class DataLoaderTests: XCTestCase {
3233
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
3334
}
3435

35-
let identityLoader = DataLoader<Int, Int>() { keys in
36+
let identityLoader = DataLoader<Int, Int> { keys in
3637
let results = keys.map { DataLoaderFutureValue.success($0) }
3738

3839
return eventLoopGroup.next().makeSucceededFuture(results)

0 commit comments

Comments
 (0)