-
Notifications
You must be signed in to change notification settings - Fork 10
feat(meta_provider): add multi-provider evaluation strategies #102
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
Merged
Changes from all commits
Commits
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
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
92 changes: 39 additions & 53 deletions
92
providers/openfeature-meta_provider/lib/openfeature/meta_provider.rb
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
41 changes: 41 additions & 0 deletions
41
providers/openfeature-meta_provider/lib/openfeature/meta_provider/strategy/base.rb
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,41 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module OpenFeature | ||
| class MetaProvider | ||
| module Strategy | ||
| class Base | ||
| MATCHED_PROVIDER_KEY = "matched_provider" | ||
|
|
||
| def resolve(providers:, default_value:, &fetch_block) | ||
| raise NotImplementedError, "#{self.class}#resolve must be implemented" | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def add_provider_metadata(details, provider) | ||
| with_merged_metadata(details, MATCHED_PROVIDER_KEY => provider.metadata.name) | ||
| end | ||
|
|
||
| def with_merged_metadata(details, extra_metadata) | ||
| SDK::Provider::ResolutionDetails.new( | ||
| value: details.value, | ||
| reason: details.reason, | ||
| variant: details.variant, | ||
| error_code: details.error_code, | ||
| error_message: details.error_message, | ||
| flag_metadata: (details.flag_metadata || {}).merge(extra_metadata) | ||
| ) | ||
| end | ||
|
|
||
| def default_error_result(default_value, error_message: nil) | ||
| SDK::Provider::ResolutionDetails.new( | ||
| value: default_value, | ||
| error_code: SDK::Provider::ErrorCode::GENERAL, | ||
| reason: SDK::Provider::Reason::ERROR, | ||
| error_message: error_message | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
38 changes: 38 additions & 0 deletions
38
providers/openfeature-meta_provider/lib/openfeature/meta_provider/strategy/comparison.rb
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,38 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module OpenFeature | ||
| class MetaProvider | ||
| module Strategy | ||
| class Comparison < Base | ||
| def resolve(providers:, default_value:, &fetch_block) | ||
| results = [] | ||
| errors = [] | ||
|
|
||
| providers.each do |provider| | ||
| details = fetch_block.call(provider) | ||
|
|
||
| if details.error_code.nil? | ||
| results << add_provider_metadata(details, provider) | ||
| else | ||
| errors << {provider: provider.metadata.name, error_code: details.error_code} | ||
| end | ||
| rescue => e | ||
| errors << {provider: provider.metadata.name, error_code: e.message} | ||
| end | ||
|
|
||
| return default_error_result(default_value, error_message: "All providers failed") if results.empty? | ||
|
|
||
| if results.all? { |r| r.value == results.first.value } | ||
| with_merged_metadata(results.first, "comparison_result" => "unanimous") | ||
| else | ||
| mismatch_details = results.map { |r| "#{r.flag_metadata[MATCHED_PROVIDER_KEY]}=#{r.value.inspect}" }.join(", ") | ||
| default_error_result( | ||
| default_value, | ||
| error_message: "Providers disagree: #{mismatch_details}" | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
24 changes: 24 additions & 0 deletions
24
providers/openfeature-meta_provider/lib/openfeature/meta_provider/strategy/first_match.rb
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,24 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module OpenFeature | ||
| class MetaProvider | ||
| module Strategy | ||
| class FirstMatch < Base | ||
| def resolve(providers:, default_value:, &fetch_block) | ||
| successful_details = providers.each do |provider| | ||
| details = add_provider_metadata(fetch_block.call(provider), provider) | ||
| break details if details.error_code.nil? | ||
| rescue | ||
| next | ||
| end | ||
|
|
||
| if successful_details.is_a?(SDK::Provider::ResolutionDetails) | ||
| successful_details | ||
| else | ||
| default_error_result(default_value) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
26 changes: 26 additions & 0 deletions
26
...ders/openfeature-meta_provider/lib/openfeature/meta_provider/strategy/first_successful.rb
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,26 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module OpenFeature | ||
| class MetaProvider | ||
| module Strategy | ||
| class FirstSuccessful < Base | ||
| def resolve(providers:, default_value:, &fetch_block) | ||
| providers.each do |provider| | ||
| details = add_provider_metadata(fetch_block.call(provider), provider) | ||
|
|
||
| return details if details.error_code.nil? | ||
| next if details.error_code == SDK::Provider::ErrorCode::FLAG_NOT_FOUND | ||
| return details | ||
| rescue => e | ||
| return default_error_result( | ||
| default_value, | ||
| error_message: "Provider #{provider.metadata.name} raised: #{e.message}" | ||
| ) | ||
| end | ||
|
|
||
| default_error_result(default_value, error_message: "No provider found a value for the flag") | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
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 |
|---|---|---|
|
|
@@ -2,6 +2,6 @@ | |
|
|
||
| module OpenFeature | ||
| class MetaProvider | ||
| VERSION = "0.0.7" | ||
| VERSION = "0.1.0" | ||
| end | ||
| end | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
errorsarray is initialized and populated, but its value is never used. This adds unnecessary complexity and can be confusing for future maintainers. The logic can be simplified by removing theerrorsarray and only collecting successfulresults, while continuing to ignore providers that return an error or raise an exception.