-
Notifications
You must be signed in to change notification settings - Fork 6
feat: add multiprovider #78
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
Merged
fabriziodemaria
merged 16 commits into
open-feature:main
from
jescriba:feat/add-multiprovider
Sep 15, 2025
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e2d33ad
Add MultiProvider
jescriba df29538
Resolve lint errors and run swift format
jescriba 06b24a9
Add unit tests for multiprovider
jescriba 4863f2f
Resolve lint and format
jescriba 254cb55
Updates returning an error vs throwing an error pattern in multiprovi…
jescriba 57e54a2
Try iPhone 16 to resolve missing sim issues in CI
jescriba a74e30f
Remove unused event handler and address gemini review comment
jescriba f527a7b
Update multiprovider name and FirstMatchStrategy to FirstFoundStrategy
jescriba 65f9af2
Add developer experience tests for multiprovider
jescriba 62c01ad
Run lint fixes
jescriba cf1baae
Update README for multiprovider
jescriba b94c311
Update name back to FirstMatchStratey
jescriba 9d4f798
Fixes renaming with proper name for FirstMatchStrategy and FirstSucce…
jescriba 68cb156
Update FirstSuccessStrategy error code depending on provider error
jescriba 4aae450
Merge branch 'main' into feat/add-multiprovider
jescriba 40f040e
Resolve provider tests when merging in event details work
jescriba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
Sources/OpenFeature/Provider/MultiProvider/FirstMatchStrategy.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/// FirstMatchStrategy is a strategy that evaluates a feature flag across multiple providers | ||
/// and returns the first result. Skips providers that indicate they had no value due to flag not found. | ||
/// If any provider returns an error result other than flag not found, the error is returned. | ||
final public class FirstMatchStrategy: Strategy { | ||
public init() {} | ||
|
||
public func evaluate<T>( | ||
providers: [FeatureProvider], | ||
key: String, | ||
defaultValue: T, | ||
evaluationContext: EvaluationContext?, | ||
flagEvaluation: FlagEvaluation<T> | ||
) throws -> ProviderEvaluation<T> where T: AllowedFlagValueType { | ||
for provider in providers { | ||
do { | ||
let eval = try flagEvaluation(provider)(key, defaultValue, evaluationContext) | ||
if eval.errorCode != ErrorCode.flagNotFound { | ||
return eval | ||
} | ||
} catch OpenFeatureError.flagNotFoundError { | ||
continue | ||
} catch let error as OpenFeatureError { | ||
return ProviderEvaluation( | ||
value: defaultValue, | ||
reason: Reason.error.rawValue, | ||
errorCode: error.errorCode(), | ||
errorMessage: error.description | ||
) | ||
} catch { | ||
throw error | ||
} | ||
} | ||
|
||
return ProviderEvaluation( | ||
value: defaultValue, | ||
reason: Reason.defaultReason.rawValue, | ||
errorCode: ErrorCode.flagNotFound | ||
) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
Sources/OpenFeature/Provider/MultiProvider/FirstSuccessfulStrategy.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/// FirstSuccessfulStrategy is a strategy that evaluates a feature flag across multiple providers | ||
/// and returns the first result. Similar to `FirstMatchStrategy` but does not bubble up individual provider errors. | ||
/// If no provider successfully responds, it will return an error. | ||
final public class FirstSuccessfulStrategy: Strategy { | ||
public func evaluate<T>( | ||
providers: [FeatureProvider], | ||
key: String, | ||
defaultValue: T, | ||
evaluationContext: EvaluationContext?, | ||
flagEvaluation: FlagEvaluation<T> | ||
) throws -> ProviderEvaluation<T> where T: AllowedFlagValueType { | ||
var flagNotFound = false | ||
for provider in providers { | ||
do { | ||
let eval = try flagEvaluation(provider)(key, defaultValue, evaluationContext) | ||
if eval.errorCode == nil { | ||
return eval | ||
} else if eval.errorCode == ErrorCode.flagNotFound { | ||
flagNotFound = true | ||
} | ||
} catch OpenFeatureError.flagNotFoundError { | ||
flagNotFound = true | ||
} catch { | ||
continue | ||
} | ||
} | ||
|
||
let errorCode = flagNotFound ? ErrorCode.flagNotFound : ErrorCode.general | ||
return ProviderEvaluation( | ||
value: defaultValue, | ||
reason: Reason.defaultReason.rawValue, | ||
errorCode: errorCode | ||
) | ||
} | ||
} |
130 changes: 130 additions & 0 deletions
130
Sources/OpenFeature/Provider/MultiProvider/MultiProvider.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import Combine | ||
import Foundation | ||
|
||
/// A provider that combines multiple providers into a single provider. | ||
public class MultiProvider: FeatureProvider { | ||
public var hooks: [any Hook] { | ||
[] | ||
} | ||
|
||
public static let name = "MultiProvider" | ||
public var metadata: ProviderMetadata | ||
|
||
private let providers: [FeatureProvider] | ||
private let strategy: Strategy | ||
|
||
/// Initialize a MultiProvider with a list of providers and a strategy. | ||
/// - Parameters: | ||
/// - providers: A list of providers to evaluate. | ||
/// - strategy: A strategy to evaluate the providers. Defaults to FirstMatchStrategy. | ||
public init( | ||
providers: [FeatureProvider], | ||
strategy: Strategy = FirstMatchStrategy() | ||
) { | ||
self.providers = providers | ||
self.strategy = strategy | ||
metadata = MultiProviderMetadata(providers: providers) | ||
} | ||
|
||
public func initialize(initialContext: EvaluationContext?) async throws { | ||
try await withThrowingTaskGroup(of: Void.self) { group in | ||
for provider in providers { | ||
group.addTask { | ||
try await provider.initialize(initialContext: initialContext) | ||
} | ||
} | ||
try await group.waitForAll() | ||
} | ||
} | ||
|
||
public func onContextSet(oldContext: EvaluationContext?, newContext: EvaluationContext) async throws { | ||
try await withThrowingTaskGroup(of: Void.self) { group in | ||
for provider in providers { | ||
group.addTask { | ||
try await provider.onContextSet(oldContext: oldContext, newContext: newContext) | ||
} | ||
} | ||
try await group.waitForAll() | ||
} | ||
} | ||
|
||
public func getBooleanEvaluation(key: String, defaultValue: Bool, context: EvaluationContext?) throws | ||
-> ProviderEvaluation<Bool> | ||
{ | ||
return try strategy.evaluate( | ||
providers: providers, | ||
key: key, | ||
defaultValue: defaultValue, | ||
evaluationContext: context | ||
) { provider in | ||
provider.getBooleanEvaluation(key:defaultValue:context:) | ||
} | ||
} | ||
|
||
public func getStringEvaluation(key: String, defaultValue: String, context: EvaluationContext?) throws | ||
-> ProviderEvaluation<String> | ||
{ | ||
return try strategy.evaluate( | ||
providers: providers, | ||
key: key, | ||
defaultValue: defaultValue, | ||
evaluationContext: context | ||
) { provider in | ||
provider.getStringEvaluation(key:defaultValue:context:) | ||
} | ||
} | ||
|
||
public func getIntegerEvaluation(key: String, defaultValue: Int64, context: EvaluationContext?) throws | ||
-> ProviderEvaluation<Int64> | ||
{ | ||
return try strategy.evaluate( | ||
providers: providers, | ||
key: key, | ||
defaultValue: defaultValue, | ||
evaluationContext: context | ||
) { provider in | ||
provider.getIntegerEvaluation(key:defaultValue:context:) | ||
} | ||
} | ||
|
||
public func getDoubleEvaluation(key: String, defaultValue: Double, context: EvaluationContext?) throws | ||
-> ProviderEvaluation<Double> | ||
{ | ||
return try strategy.evaluate( | ||
providers: providers, | ||
key: key, | ||
defaultValue: defaultValue, | ||
evaluationContext: context | ||
) { provider in | ||
provider.getDoubleEvaluation(key:defaultValue:context:) | ||
} | ||
} | ||
|
||
public func getObjectEvaluation(key: String, defaultValue: Value, context: EvaluationContext?) throws | ||
-> ProviderEvaluation<Value> | ||
{ | ||
return try strategy.evaluate( | ||
providers: providers, | ||
key: key, | ||
defaultValue: defaultValue, | ||
evaluationContext: context | ||
) { provider in | ||
provider.getObjectEvaluation(key:defaultValue:context:) | ||
} | ||
} | ||
|
||
public func observe() -> AnyPublisher<ProviderEvent?, Never> { | ||
return Publishers.MergeMany(providers.map { $0.observe() }).eraseToAnyPublisher() | ||
} | ||
|
||
public struct MultiProviderMetadata: ProviderMetadata { | ||
public var name: String? | ||
|
||
init(providers: [FeatureProvider]) { | ||
name = "MultiProvider: " + providers.map { | ||
$0.metadata.name ?? "Provider" | ||
} | ||
.joined(separator: ", ") | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/// FlagEvaluation is a function that evaluates a feature flag and returns a ProviderEvaluation. | ||
/// It is used to evaluate a feature flag across multiple providers using the strategy's logic. | ||
public typealias FlagEvaluation<T> = (FeatureProvider) -> ( | ||
_ key: String, _ defaultValue: T, _ evaluationContext: EvaluationContext? | ||
) throws -> ProviderEvaluation<T> where T: AllowedFlagValueType | ||
|
||
/// Strategy interface defines how multiple feature providers should be evaluated | ||
/// to determine the final result for a feature flag evaluation. | ||
/// Different strategies can implement different logic for combining or selecting | ||
/// results from multiple providers. | ||
public protocol Strategy { | ||
func evaluate<T>( | ||
providers: [FeatureProvider], | ||
key: String, | ||
defaultValue: T, | ||
evaluationContext: EvaluationContext?, | ||
flagEvaluation: FlagEvaluation<T> | ||
) throws -> ProviderEvaluation<T> where T: AllowedFlagValueType | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.