From e80f380d9fc3b3f669b2106e2fadd24759a9d79e Mon Sep 17 00:00:00 2001 From: Patryk Piwowarczyk <108664461+PatrykPiwowarczyk@users.noreply.github.com> Date: Fri, 6 Oct 2023 08:26:00 +0200 Subject: [PATCH] chore: fix lint issues. (#50) --- Sources/FeaturevisorSDK/Bucket.swift | 13 +- Sources/FeaturevisorSDK/Conditions.swift | 20 +- .../FeaturevisorSDK/Instance+Refresh.swift | 3 +- .../FeaturevisorSDK/Instance+Segments.swift | 10 +- Sources/FeaturevisorSDK/Instance.swift | 3 +- .../KeyedDecodingContainer+Stringified.swift | 3 +- .../FeaturevisorSDKTests/InstanceTests.swift | 18 +- .../FeaturevisorTypesTests.swift | 180 +++++++++--------- 8 files changed, 134 insertions(+), 116 deletions(-) diff --git a/Sources/FeaturevisorSDK/Bucket.swift b/Sources/FeaturevisorSDK/Bucket.swift index 056b017..681c0f8 100644 --- a/Sources/FeaturevisorSDK/Bucket.swift +++ b/Sources/FeaturevisorSDK/Bucket.swift @@ -4,15 +4,16 @@ import MurmurHash_Swift internal enum Bucket { - private static let HASH_SEED: UInt32 = 1 - private static let MAX_HASH_VALUE: Double = pow(2, 32) // 2^32 - private static let MAX_BUCKETED_NUMBER = 100000 // 100% * 1000 to include three decimal places in the same integer value + private static let hashSeed: UInt32 = 1 + private static let maxHashValue: Double = pow(2, 32) // 2^32 + private static let maxSegmentNumber = 100000 + // 100% * 1000 to include three decimal places in the same integer value static func resolveNumber(forKey bucketKey: BucketKey) -> Int { - let hashValue = MurmurHash3.x86_32.digest(bucketKey, seed: HASH_SEED) - let ratio = Double(hashValue) / MAX_HASH_VALUE + let hashValue = MurmurHash3.x86_32.digest(bucketKey, seed: hashSeed) + let ratio = Double(hashValue) / maxHashValue - return Int(floor(ratio * Double(MAX_BUCKETED_NUMBER))) + return Int(floor(ratio * Double(maxSegmentNumber))) } } diff --git a/Sources/FeaturevisorSDK/Conditions.swift b/Sources/FeaturevisorSDK/Conditions.swift index 17fa70c..ade2b2b 100644 --- a/Sources/FeaturevisorSDK/Conditions.swift +++ b/Sources/FeaturevisorSDK/Conditions.swift @@ -21,7 +21,7 @@ public func conditionIsMatched(condition: PlainCondition, context: Context) -> B switch (valueInAttributes, valueInCondition) { // boolean, boolean - case let (.boolean(valueInAttributes), .boolean(valueInCondition)): + case (.boolean(let valueInAttributes), .boolean(let valueInCondition)): switch op { case .equals: return valueInAttributes == valueInCondition @@ -32,7 +32,7 @@ public func conditionIsMatched(condition: PlainCondition, context: Context) -> B } // string, string - case let (.string(valueInAttributes), .string(valueInCondition)): + case (.string(let valueInAttributes), .string(let valueInCondition)): if String(op.rawValue).starts(with: "semver") { // @TODO: handle semver comparisons here @@ -99,7 +99,7 @@ public func conditionIsMatched(condition: PlainCondition, context: Context) -> B } // date, string - case let (.date(valueInAttributes), .string(valueInCondition)): + case (.date(let valueInAttributes), .string(let valueInCondition)): switch op { case .before: let dateInAttributes = valueInAttributes @@ -125,7 +125,7 @@ public func conditionIsMatched(condition: PlainCondition, context: Context) -> B } // integer, integer - case let (.integer(valueInAttributes), .integer(valueInCondition)): + case (.integer(let valueInAttributes), .integer(let valueInCondition)): switch op { case .equals: return valueInAttributes == valueInCondition @@ -144,7 +144,7 @@ public func conditionIsMatched(condition: PlainCondition, context: Context) -> B } // double, double - case let (.double(valueInAttributes), .double(valueInCondition)): + case (.double(let valueInAttributes), .double(let valueInCondition)): switch op { case .equals: return valueInAttributes == valueInCondition @@ -170,25 +170,25 @@ public func conditionIsMatched(condition: PlainCondition, context: Context) -> B public func allConditionsAreMatched(condition: Condition, context: Context) -> Bool { switch condition { - case let .plain(condition): + case .plain(let condition): return conditionIsMatched(condition: condition, context: context) - case let .multiple(condition): + case .multiple(let condition): return condition.allSatisfy { condition in allConditionsAreMatched(condition: condition, context: context) } - case let .and(condition): + case .and(let condition): return condition.and.allSatisfy { condition in allConditionsAreMatched(condition: condition, context: context) } - case let .or(condition): + case .or(let condition): return condition.or.contains { condition in allConditionsAreMatched(condition: condition, context: context) } - case let .not(condition): + case .not(let condition): return !condition.not.allSatisfy { condition in allConditionsAreMatched(condition: condition, context: context) } diff --git a/Sources/FeaturevisorSDK/Instance+Refresh.swift b/Sources/FeaturevisorSDK/Instance+Refresh.swift index 468a9e9..935f199 100644 --- a/Sources/FeaturevisorSDK/Instance+Refresh.swift +++ b/Sources/FeaturevisorSDK/Instance+Refresh.swift @@ -21,7 +21,8 @@ extension FeaturevisorInstance { try? fetchDatafileContent( from: datafileUrl, - handleDatafileFetch: handleDatafileFetch) { [weak self] result in + handleDatafileFetch: handleDatafileFetch + ) { [weak self] result in guard let self else { return } diff --git a/Sources/FeaturevisorSDK/Instance+Segments.swift b/Sources/FeaturevisorSDK/Instance+Segments.swift index b577241..c96a359 100644 --- a/Sources/FeaturevisorSDK/Instance+Segments.swift +++ b/Sources/FeaturevisorSDK/Instance+Segments.swift @@ -33,7 +33,7 @@ extension FeaturevisorInstance { ) -> Bool { switch groupSegments { - case let .plain(segmentKey): + case .plain(let segmentKey): if segmentKey == "*" { return true } @@ -44,7 +44,7 @@ extension FeaturevisorInstance { return false - case let .multiple(groupSegments): + case .multiple(let groupSegments): return groupSegments.allSatisfy { groupSegment in allGroupSegmentsAreMatched( groupSegments: groupSegment, @@ -53,7 +53,7 @@ extension FeaturevisorInstance { ) } - case let .and(andGroupSegment): + case .and(let andGroupSegment): return andGroupSegment.and.allSatisfy { groupSegment in allGroupSegmentsAreMatched( groupSegments: groupSegment, @@ -62,7 +62,7 @@ extension FeaturevisorInstance { ) } - case let .or(orGroupSegment): + case .or(let orGroupSegment): return orGroupSegment.or.contains { groupSegment in allGroupSegmentsAreMatched( groupSegments: groupSegment, @@ -71,7 +71,7 @@ extension FeaturevisorInstance { ) } - case let .not(notGroupSegment): + case .not(let notGroupSegment): return !notGroupSegment.not.allSatisfy { groupSegment in allGroupSegmentsAreMatched( groupSegments: groupSegment, diff --git a/Sources/FeaturevisorSDK/Instance.swift b/Sources/FeaturevisorSDK/Instance.swift index cbbd06b..4518dea 100644 --- a/Sources/FeaturevisorSDK/Instance.swift +++ b/Sources/FeaturevisorSDK/Instance.swift @@ -229,7 +229,8 @@ public class FeaturevisorInstance { try fetchDatafileContent( from: datafileUrl, - handleDatafileFetch: handleDatafileFetch) { [weak self] result in + handleDatafileFetch: handleDatafileFetch + ) { [weak self] result in switch result { case .success(let datafileContent): self?.datafileReader = DatafileReader(datafileContent: datafileContent) diff --git a/Sources/FeaturevisorTypes/Extensions/KeyedDecodingContainer+Stringified.swift b/Sources/FeaturevisorTypes/Extensions/KeyedDecodingContainer+Stringified.swift index 0bf642a..58e2b52 100644 --- a/Sources/FeaturevisorTypes/Extensions/KeyedDecodingContainer+Stringified.swift +++ b/Sources/FeaturevisorTypes/Extensions/KeyedDecodingContainer+Stringified.swift @@ -26,7 +26,8 @@ extension KeyedDecodingContainer { func decodeStringifiedIfPresent( _ type: T.Type, - forKey key: KeyedDecodingContainer.Key) throws -> T? where T: Decodable { + forKey key: KeyedDecodingContainer.Key + ) throws -> T? where T: Decodable { guard self.contains(key) else { return nil diff --git a/Tests/FeaturevisorSDKTests/InstanceTests.swift b/Tests/FeaturevisorSDKTests/InstanceTests.swift index c521749..3e1a265 100644 --- a/Tests/FeaturevisorSDKTests/InstanceTests.swift +++ b/Tests/FeaturevisorSDKTests/InstanceTests.swift @@ -689,7 +689,14 @@ class FeaturevisorInstanceTests: XCTestCase { key: "myKey", bucketBy: .single("userId"), variations: [], - required: [.withVariation(.init(key: "requiredKey", variation: "control"))], // different variation + required: [ + .withVariation( + .init( + key: "requiredKey", + variation: "control" + ) + ) + ], // different variation traffic: [ Traffic( key: "1", @@ -754,7 +761,14 @@ class FeaturevisorInstanceTests: XCTestCase { key: "myKey", bucketBy: .single("userId"), variations: [], - required: [.withVariation(.init(key: "requiredKey", variation: "treatment"))], // desired variation + required: [ + .withVariation( + .init( + key: "requiredKey", + variation: "treatment" + ) + ) + ], // desired variation traffic: [ Traffic( key: "1", diff --git a/Tests/FeaturevisorTypesTests/FeaturevisorTypesTests.swift b/Tests/FeaturevisorTypesTests/FeaturevisorTypesTests.swift index 59bbf98..66b62ae 100644 --- a/Tests/FeaturevisorTypesTests/FeaturevisorTypesTests.swift +++ b/Tests/FeaturevisorTypesTests/FeaturevisorTypesTests.swift @@ -50,27 +50,27 @@ final class FeaturevisorTypesTests: XCTestCase { XCTAssertEqual(feature1.traffic.count, 2) XCTAssertEqual(feature1.force.count, 1) - let variation1_1 = feature1.variations[0] - XCTAssertEqual(variation1_1.value, "control") - XCTAssertNil(variation1_1.description) - XCTAssertEqual(variation1_1.weight, 33.34) - XCTAssertEqual(variation1_1.variables!.count, 1) - - let variable1_1 = variation1_1.variables![0] - XCTAssertEqual(variable1_1.key, "hero") + let variation11 = feature1.variations[0] + XCTAssertEqual(variation11.value, "control") + XCTAssertNil(variation11.description) + XCTAssertEqual(variation11.weight, 33.34) + XCTAssertEqual(variation11.variables!.count, 1) + + let variable11 = variation11.variables![0] + XCTAssertEqual(variable11.key, "hero") XCTAssertEqual( - variable1_1.value, + variable11.value, .object([ "title": .string("Hero Title for B"), "subtitle": .string("Hero Subtitle for B"), "alignment": .string("center for B"), ]) ) - XCTAssertEqual(variable1_1.overrides!.count, 1) + XCTAssertEqual(variable11.overrides!.count, 1) - let override1_1 = variable1_1.overrides![0] + let override11 = variable11.overrides![0] XCTAssertEqual( - override1_1.value, + override11.value, .object([ "title": .string("Hero Title for B in DE or CH"), "subtitle": .string("Hero Subtitle for B in DE of CH"), @@ -78,67 +78,67 @@ final class FeaturevisorTypesTests: XCTestCase { ]) ) XCTAssertEqual( - override1_1.segments, + override11.segments, .or( OrGroupSegment(or: [.plain("germany"), .plain("switzerland")]) ) ) - let variation1_2 = feature1.variations[1] - XCTAssertEqual(variation1_2.value, "treatment") - XCTAssertNil(variation1_2.description) - XCTAssertEqual(variation1_2.weight, 33.33) - XCTAssertNil(variation1_2.variables) - - let variation1_3 = feature1.variations[2] - XCTAssertEqual(variation1_3.value, "anotherTreatment") - XCTAssertNil(variation1_3.description) - XCTAssertEqual(variation1_3.weight, 33.33) - XCTAssertNil(variation1_3.variables) - - let traffic1_1 = feature1.traffic[0] - XCTAssertEqual(traffic1_1.key, "1") - XCTAssertNil(traffic1_1.enabled) - XCTAssertEqual(traffic1_1.percentage, 100000) - XCTAssertEqual(traffic1_1.variation, nil) - XCTAssertEqual(traffic1_1.segments, .multiple([.plain("myAccount")])) - XCTAssertNil(traffic1_1.variables) - XCTAssertEqual(traffic1_1.allocation.count, 3) - - let allocation1_1 = traffic1_1.allocation[0] - XCTAssertEqual(allocation1_1.variation, "control") - XCTAssertEqual(allocation1_1.range.start, 0) - XCTAssertEqual(allocation1_1.range.end, 33340) - - let allocation1_2 = traffic1_1.allocation[1] - XCTAssertEqual(allocation1_2.variation, "treatment") - XCTAssertEqual(allocation1_2.range.start, 33340) - XCTAssertEqual(allocation1_2.range.end, 66670) - - let allocation1_3 = traffic1_1.allocation[2] - XCTAssertEqual(allocation1_3.variation, "anotherTreatment") - XCTAssertEqual(allocation1_3.range.start, 66670) - XCTAssertEqual(allocation1_3.range.end, 100000) - - let traffic1_2 = feature1.traffic[1] - XCTAssertEqual(traffic1_2.key, "2") - XCTAssertNil(traffic1_2.enabled) - XCTAssertEqual(traffic1_2.percentage, 0) - XCTAssertEqual(traffic1_2.variation, nil) - XCTAssertEqual(traffic1_2.segments, .plain("*")) - XCTAssertNil(traffic1_2.variables) - XCTAssertEqual(traffic1_2.allocation.count, 0) - - let variablesSchema1_1 = feature1.variablesSchema[0] - XCTAssertEqual(variablesSchema1_1.key, "color") - XCTAssertEqual(variablesSchema1_1.type, .string) - XCTAssertEqual(variablesSchema1_1.defaultValue, .string("red")) - - let variablesSchema1_2 = feature1.variablesSchema[1] - XCTAssertEqual(variablesSchema1_2.key, "hero") - XCTAssertEqual(variablesSchema1_2.type, .object) + let variation12 = feature1.variations[1] + XCTAssertEqual(variation12.value, "treatment") + XCTAssertNil(variation12.description) + XCTAssertEqual(variation12.weight, 33.33) + XCTAssertNil(variation12.variables) + + let variation13 = feature1.variations[2] + XCTAssertEqual(variation13.value, "anotherTreatment") + XCTAssertNil(variation13.description) + XCTAssertEqual(variation13.weight, 33.33) + XCTAssertNil(variation13.variables) + + let traffic11 = feature1.traffic[0] + XCTAssertEqual(traffic11.key, "1") + XCTAssertNil(traffic11.enabled) + XCTAssertEqual(traffic11.percentage, 100000) + XCTAssertEqual(traffic11.variation, nil) + XCTAssertEqual(traffic11.segments, .multiple([.plain("myAccount")])) + XCTAssertNil(traffic11.variables) + XCTAssertEqual(traffic11.allocation.count, 3) + + let allocation11 = traffic11.allocation[0] + XCTAssertEqual(allocation11.variation, "control") + XCTAssertEqual(allocation11.range.start, 0) + XCTAssertEqual(allocation11.range.end, 33340) + + let allocation12 = traffic11.allocation[1] + XCTAssertEqual(allocation12.variation, "treatment") + XCTAssertEqual(allocation12.range.start, 33340) + XCTAssertEqual(allocation12.range.end, 66670) + + let allocation13 = traffic11.allocation[2] + XCTAssertEqual(allocation13.variation, "anotherTreatment") + XCTAssertEqual(allocation13.range.start, 66670) + XCTAssertEqual(allocation13.range.end, 100000) + + let traffic12 = feature1.traffic[1] + XCTAssertEqual(traffic12.key, "2") + XCTAssertNil(traffic12.enabled) + XCTAssertEqual(traffic12.percentage, 0) + XCTAssertEqual(traffic12.variation, nil) + XCTAssertEqual(traffic12.segments, .plain("*")) + XCTAssertNil(traffic12.variables) + XCTAssertEqual(traffic12.allocation.count, 0) + + let variablesSchema11 = feature1.variablesSchema[0] + XCTAssertEqual(variablesSchema11.key, "color") + XCTAssertEqual(variablesSchema11.type, .string) + XCTAssertEqual(variablesSchema11.defaultValue, .string("red")) + + let variablesSchema12 = feature1.variablesSchema[1] + XCTAssertEqual(variablesSchema12.key, "hero") + XCTAssertEqual(variablesSchema12.type, .object) XCTAssertEqual( - variablesSchema1_2.defaultValue, + variablesSchema12.defaultValue, .object([ "title": .string("Hero Title"), "subtitle": .string("Hero Subtitle"), @@ -146,10 +146,10 @@ final class FeaturevisorTypesTests: XCTestCase { ]) ) - let force1_1 = feature1.force[0] - XCTAssertTrue(force1_1.enabled!) + let force11 = feature1.force[0] + XCTAssertTrue(force11.enabled!) XCTAssertEqual( - force1_1.conditions, + force11.conditions, .and( AndCondition(and: [ .plain( @@ -169,9 +169,9 @@ final class FeaturevisorTypesTests: XCTestCase { ]) ) ) - XCTAssertNil(force1_1.segments) - XCTAssertEqual(force1_1.variables, ["bar": .string("yoooooo")]) - XCTAssertEqual(force1_1.variation, "treatment") + XCTAssertNil(force11.segments) + XCTAssertEqual(force11.variables, ["bar": .string("yoooooo")]) + XCTAssertEqual(force11.variation, "treatment") let feature2 = result.features[1] XCTAssertEqual(feature2.key, "f_foo") @@ -181,23 +181,23 @@ final class FeaturevisorTypesTests: XCTestCase { XCTAssertEqual(feature2.variations.count, 0) XCTAssertEqual(feature2.traffic.count, 2) - let traffic2_1 = feature2.traffic[0] - XCTAssertEqual(traffic2_1.key, "1") - XCTAssertNil(traffic2_1.enabled) - XCTAssertEqual(traffic2_1.percentage, 50000) - XCTAssertEqual(traffic2_1.variation, nil) - XCTAssertEqual(traffic2_1.segments, .multiple([.plain("myAccount")])) - XCTAssertNil(traffic2_1.variables) - XCTAssertEqual(traffic2_1.allocation.count, 0) - - let traffic2_2 = feature2.traffic[1] - XCTAssertEqual(traffic2_2.key, "2") - XCTAssertNil(traffic2_2.enabled) - XCTAssertEqual(traffic2_2.percentage, 0) - XCTAssertEqual(traffic2_2.variation, nil) - XCTAssertEqual(traffic2_2.segments, .plain("*")) - XCTAssertNil(traffic2_2.variables) - XCTAssertEqual(traffic2_2.allocation.count, 0) + let traffic21 = feature2.traffic[0] + XCTAssertEqual(traffic21.key, "1") + XCTAssertNil(traffic21.enabled) + XCTAssertEqual(traffic21.percentage, 50000) + XCTAssertEqual(traffic21.variation, nil) + XCTAssertEqual(traffic21.segments, .multiple([.plain("myAccount")])) + XCTAssertNil(traffic21.variables) + XCTAssertEqual(traffic21.allocation.count, 0) + + let traffic22 = feature2.traffic[1] + XCTAssertEqual(traffic22.key, "2") + XCTAssertNil(traffic22.enabled) + XCTAssertEqual(traffic22.percentage, 0) + XCTAssertEqual(traffic22.variation, nil) + XCTAssertEqual(traffic22.segments, .plain("*")) + XCTAssertNil(traffic22.variables) + XCTAssertEqual(traffic22.allocation.count, 0) let attribute1 = result.attributes[0] XCTAssertEqual(attribute1.key, "chapter")