Skip to content

Commit

Permalink
fix #18: fixed issue with fallback value
Browse files Browse the repository at this point in the history
  • Loading branch information
malcommac committed Jan 26, 2022
1 parent ee182a0 commit a01d268
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions RealFlags/Sources/RealFlags/Classes/Flag/Flag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,18 @@ public struct Flag<Value: FlagProtocol>: FeatureFlagConfigurableProtocol, Identi
/// Return the value of the property by asking to the list of providers set.
/// If a `providerType` is passed only that type is read.
///
/// - Parameter providerType: provider type, if `nil` the providers list with `allowedProviders` is read.
/// - Returns: Value
/// - Parameters:
/// - providerType: provider to read, `nil` to read every set provider in order.
/// - fallback: `true` to return the fallback value if no value were found in any provider, `false`
/// to return `nil` in that case.
/// - Returns: `(value: Value?, source: FlagsProvider?)`
public func flagValue(from providerType: FlagsProvider.Type? = nil, fallback: Bool = true) -> (value: Value?, source: FlagsProvider?) {
if let value = computedValue?() {
return (value, nil) // value is obtained by dynamic function.
}

guard loader.instance != nil else {
return (defaultValue, nil) // no loader has been set, we want to get the fallback result.
return ( (fallback ? defaultValue : nil), nil) // no loader has been set, we want to get the fallback result.
}

let providersToQuery = providersWithTypes([providerType].compactMap({ $0 }))
Expand All @@ -169,7 +172,7 @@ public struct Flag<Value: FlagProtocol>: FeatureFlagConfigurableProtocol, Identi
}
}

return (nil, nil)
return ( (fallback ? defaultValue : nil), nil)
}

/// Change the default fallback value manually.
Expand Down

0 comments on commit a01d268

Please sign in to comment.