Skip to content

Commit

Permalink
refactor: Fix swift lint warnings (#60)
Browse files Browse the repository at this point in the history
* Fix swift lint warnings

* second attempt

* Move file length disables

* Move file length and body disables
  • Loading branch information
Calibretto authored Sep 13, 2023
1 parent 21973d5 commit 125ed50
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 28 deletions.
5 changes: 2 additions & 3 deletions Sources/ConfidenceProvider/Apply/FlagApplierWithRetries.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ final class FlagApplierWithRetries: FlagApplier {
private func triggerBatch() async {
async let cacheData = await cacheDataInteractor.cache
await cacheData.resolveEvents.forEach { resolveEvent in
let appliesToSend = resolveEvent.events.filter { entry in
return entry.status == .created
}.chunk(size: 20)
let appliesToSend = resolveEvent.events.filter { $0.status == .created }
.chunk(size: 20)

guard appliesToSend.isEmpty == false else {
return
Expand Down
1 change: 0 additions & 1 deletion Sources/ConfidenceProvider/Cache/CacheDataActor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ protocol CacheDataActor: Actor {

/// Sets Resolve Apply Event `status` property.
func setEventStatus(resolveToken: String, status: ApplyEventStatus) -> CacheData

}
6 changes: 4 additions & 2 deletions Sources/ConfidenceProvider/Cache/Models/CacheData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ struct CacheData: Codable {
mutating func setEventStatus(resolveToken: String, name: String, status: ApplyEventStatus = .sent) {
let flagEventIndexes = flagEventIndex(resolveToken: resolveToken, name: name)
guard let resolveIndex = flagEventIndexes.resolveEventIndex,
let flagIndex = flagEventIndexes.flagEventIndex else {
let flagIndex = flagEventIndexes.flagEventIndex
else {
return
}

Expand Down Expand Up @@ -116,7 +117,8 @@ struct CacheData: Codable {

func flagEvent(resolveToken: String, name: String) -> FlagApply? {
guard let resolveTokenIndex = resolveEventIndex(resolveToken: resolveToken),
let flagEventIndex = applyEventIndex(resolveToken: resolveToken, name: name) else {
let flagEventIndex = applyEventIndex(resolveToken: resolveToken, name: name)
else {
return nil
}

Expand Down
15 changes: 10 additions & 5 deletions Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,13 @@ extension ConfidenceFeatureProvider {

/// Creates the `ConfidenceFeatureProvider` according to the settings specified in the builder.
public func build() -> ConfidenceFeatureProvider {
let flagApplier = flagApplier ?? FlagApplierWithRetries(
httpClient: NetworkClient(region: options.region),
storage: DefaultStorage(filePath: "applier.flags.cache"),
options: options
)
let flagApplier =
flagApplier
?? FlagApplierWithRetries(
httpClient: NetworkClient(region: options.region),
storage: DefaultStorage(filePath: "applier.flags.cache"),
options: options
)

let client = RemoteConfidenceClient(
options: options,
Expand Down Expand Up @@ -506,3 +508,6 @@ extension DispatchQueue: DispatchQueueType {
async(group: nil, qos: .unspecified, flags: [], execute: work)
}
}

// swiftlint:enable type_body_length
// swiftlint:enable file_length
16 changes: 8 additions & 8 deletions Sources/ConfidenceProvider/Http/NetworkClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ final class NetworkClient: HttpClient {
retry: Retry = .none
) {
self.session =
session
?? {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = timeout
configuration.httpAdditionalHeaders = defaultHeaders
session
?? {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = timeout
configuration.httpAdditionalHeaders = defaultHeaders

return URLSession(configuration: configuration)
}()
return URLSession(configuration: configuration)
}()

self.headers = defaultHeaders
self.retry = retry
Expand All @@ -57,7 +57,7 @@ final class NetworkClient: HttpClient {

do {
let httpClientResult: HttpClientResponse<T> =
try self.buildResponse(response: response, data: data)
try self.buildResponse(response: response, data: data)
completion(.success(httpClientResult))
} catch {
completion(.failure(error))
Expand Down
2 changes: 1 addition & 1 deletion Sources/ConfidenceProvider/Utils/Array+Chunks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
extension Array {
func chunk(size: Int) -> [[Element]] {
return stride(from: 0, to: count, by: size).map {
Array(self[$0 ..< Swift.min($0 + size, count)])
Array(self[$0..<Swift.min($0 + size, count)])
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// swiftlint:disable type_body_length
// swiftlint:disable file_length
import Foundation
import OpenFeature
import XCTest

@testable import ConfidenceProvider

// swiftlint:disable file_length
// swiftlint:disable type_body_length
@available(macOS 13.0, iOS 16.0, *)
class ConfidenceFeatureProviderTest: XCTestCase {
private var flagApplier = FlagApplierMock()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// swiftlint:disable type_body_length
// swiftlint:disable file_length
import Foundation
import OpenFeature
import XCTest
Expand Down Expand Up @@ -64,7 +66,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
await applier.apply(flagName: "flag3", resolveToken: "token1")

let cacheData = await cacheDataInteractor.cache

// Then http client sends 3 post requests
XCTAssertEqual(httpClient.postCallCounter, 3)
XCTAssertEqual(cacheData.resolveEvents.count, 1)
Expand All @@ -86,7 +88,6 @@ class FlagApplierWithRetriesTest: XCTestCase {
networkExpectation.expectedFulfillmentCount = 3
httpClient.expectation = networkExpectation


// When 3 apply calls are issued with different flag names
await applier.apply(flagName: "flag1", resolveToken: "token1")
await applier.apply(flagName: "flag2", resolveToken: "token1")
Expand Down Expand Up @@ -136,7 +137,6 @@ class FlagApplierWithRetriesTest: XCTestCase {
storageExpectation.expectedFulfillmentCount = 10
prefilledStorage.saveExpectation = storageExpectation


// When flag applier is initialised
_ = FlagApplierWithRetries(
httpClient: httpClient,
Expand Down Expand Up @@ -167,7 +167,6 @@ class FlagApplierWithRetriesTest: XCTestCase {
storageExpectation.expectedFulfillmentCount = 10
prefilledStorage.saveExpectation = storageExpectation


// When flag applier is initialised
_ = FlagApplierWithRetries(
httpClient: partiallyFailingHttpClient,
Expand Down Expand Up @@ -360,7 +359,6 @@ class FlagApplierWithRetriesTest: XCTestCase {
await applier.apply(flagName: "flag2", resolveToken: "token1")
await applier.apply(flagName: "flag3", resolveToken: "token1")


// Then 1 resolve event record is written to disk
let storedData = try XCTUnwrap(storage.load(defaultValue: CacheData.empty()))
let data = try XCTUnwrap(storedData.resolveEvents.first { $0.resolveToken == "token1" })
Expand Down Expand Up @@ -440,7 +438,6 @@ class FlagApplierWithRetriesTest: XCTestCase {
XCTAssertEqual(newResolveEvent.events[0].status, .created)
}


func testApplyOffline_previoslyStoredData_100records() async throws {
// Given flag applier set up with offline http client
// And storage that has previously stored 100 records with different tokens
Expand Down

0 comments on commit 125ed50

Please sign in to comment.