Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix lint issues. #50

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions Sources/FeaturevisorSDK/Bucket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
}
20 changes: 10 additions & 10 deletions Sources/FeaturevisorSDK/Conditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/FeaturevisorSDK/Instance+Refresh.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/FeaturevisorSDK/Instance+Segments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extension FeaturevisorInstance {
) -> Bool {

switch groupSegments {
case let .plain(segmentKey):
case .plain(let segmentKey):
if segmentKey == "*" {
return true
}
Expand All @@ -44,7 +44,7 @@ extension FeaturevisorInstance {

return false

case let .multiple(groupSegments):
case .multiple(let groupSegments):
return groupSegments.allSatisfy { groupSegment in
allGroupSegmentsAreMatched(
groupSegments: groupSegment,
Expand All @@ -53,7 +53,7 @@ extension FeaturevisorInstance {
)
}

case let .and(andGroupSegment):
case .and(let andGroupSegment):
return andGroupSegment.and.allSatisfy { groupSegment in
allGroupSegmentsAreMatched(
groupSegments: groupSegment,
Expand All @@ -62,7 +62,7 @@ extension FeaturevisorInstance {
)
}

case let .or(orGroupSegment):
case .or(let orGroupSegment):
return orGroupSegment.or.contains { groupSegment in
allGroupSegmentsAreMatched(
groupSegments: groupSegment,
Expand All @@ -71,7 +71,7 @@ extension FeaturevisorInstance {
)
}

case let .not(notGroupSegment):
case .not(let notGroupSegment):
return !notGroupSegment.not.allSatisfy { groupSegment in
allGroupSegmentsAreMatched(
groupSegments: groupSegment,
Expand Down
3 changes: 2 additions & 1 deletion Sources/FeaturevisorSDK/Instance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ extension KeyedDecodingContainer {

func decodeStringifiedIfPresent<T>(
_ type: T.Type,
forKey key: KeyedDecodingContainer<K>.Key) throws -> T? where T: Decodable {
forKey key: KeyedDecodingContainer<K>.Key
) throws -> T? where T: Decodable {

guard self.contains(key) else {
return nil
Expand Down
18 changes: 16 additions & 2 deletions Tests/FeaturevisorSDKTests/InstanceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
Loading