Skip to content

Commit

Permalink
refactor: Clearer code paths
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Nov 7, 2024
1 parent 1cd12a7 commit bfe59c7
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions Sources/Confidence/FlagEvaluation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,12 @@ extension FlagResolution {
)
}

if resolvedFlag.resolveReason == .targetingKeyError {
return Evaluation(
value: defaultValue,
variant: nil,
reason: .targetingKeyError,
errorCode: .invalidContext,
errorMessage: "Invalid targeting key"
)
if let evaluation = checkBackendErrrs(resolvedFlag: resolvedFlag, defaultValue: defaultValue) {
return evaluation
}

guard let value = resolvedFlag.value else {
// No backend error, but nil value returned. This can happend with "noSegmentMatch" or "archived", for example
Task {
await flagApplier?.apply(flagName: parsedKey.flag, resolveToken: self.resolveToken)
}
Expand Down Expand Up @@ -133,6 +128,30 @@ extension FlagResolution {
}
// swiftlint:enable function_body_length

private func checkBackendErrrs<T>(resolvedFlag: ResolvedValue, defaultValue: T) -> Evaluation<T>? {
if resolvedFlag.resolveReason == .targetingKeyError {
return Evaluation(
value: defaultValue,
variant: nil,
reason: .targetingKeyError,
errorCode: .invalidContext,
errorMessage: "Invalid targeting key"
)
} else if (resolvedFlag.resolveReason == .error

Check failure on line 140 in Sources/Confidence/FlagEvaluation.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Control Statement Violation: `if`, `for`, `guard`, `switch`, `while`, and `catch` statements shouldn't unnecessarily wrap their conditionals or arguments in parentheses (control_statement)
|| resolvedFlag.resolveReason == .unknown

Check failure on line 141 in Sources/Confidence/FlagEvaluation.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Indentation Width Violation: Code should be indented using one tab or 4 spaces (indentation_width)
|| resolvedFlag.resolveReason == .unspecified) {
return Evaluation(
value: defaultValue,
variant: nil,
reason: .error,
errorCode: .evaluationError,
errorMessage: "Unknown error from backend"
)
} else {
return nil
}
}

// swiftlint:disable:next cyclomatic_complexity
private func getTyped<T>(value: ConfidenceValue) -> T? {
if let value = self as? T {
Expand Down

0 comments on commit bfe59c7

Please sign in to comment.