Skip to content

Commit

Permalink
refactor!: Simplify folder structure for meta provider
Browse files Browse the repository at this point in the history
Signed-off-by: Max VelDink <[email protected]>
  • Loading branch information
maxveldink committed Apr 29, 2024
1 parent 653036c commit 0228eab
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 99 deletions.
4 changes: 2 additions & 2 deletions providers/openfeature-meta_provider/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
openfeature-sdk-meta_provider (0.1.0)
openfeature-meta_provider (0.0.1)
openfeature-sdk (~> 0.3.0)

GEM
Expand Down Expand Up @@ -86,7 +86,7 @@ PLATFORMS

DEPENDENCIES
debug (~> 1.9.2)
openfeature-sdk-meta_provider!
openfeature-meta_provider!
rake (~> 13.0)
rspec (~> 3.12)
standard (~> 1.34)
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# frozen_string_literal: true

module OpenFeature
# Used to pull from multiple providers.
class MetaProvider
# @param providers [Array<Provider>]
# @param strategy [Symbol] When `:first_match`, returns first matched resolution. Providers will be searched
# in the order they were given. Defaults to `:first_match`.
def initialize(providers:, strategy: :first_match)
@providers = providers
@strategy = strategy
end

def metadata
SDK::Provider::ProviderMetadata.new(name: "MetaProvider: #{providers.map { |provider| provider.metadata.name }.join(", ")}")
end

def init
providers.each { |provider| provider.init }
end

def shutdown
providers.each(&:shutdown)
end

def fetch_boolean_value(flag_key:, default_value:, evaluation_context: nil)
fetch_from_sources(default_value:) do |provider|
provider.fetch_boolean_value(flag_key:, default_value:, evaluation_context:)
end
end

def fetch_number_value(flag_key:, default_value:, evaluation_context: nil)
fetch_from_sources(default_value:) do |provider|
provider.fetch_number_value(flag_key:, default_value:, evaluation_context:)
end
end

def fetch_object_value(flag_key:, default_value:, evaluation_context: nil)
fetch_from_sources(default_value:) do |provider|
provider.fetch_object_value(flag_key:, default_value:, evaluation_context:)
end
end

def fetch_string_value(flag_key:, default_value:, evaluation_context: nil)
fetch_from_sources(default_value:) do |provider|
provider.fetch_string_value(flag_key:, default_value:, evaluation_context:)
end
end

private

attr_reader :providers, :strategy

def fetch_from_sources(default_value:, &blk)
case strategy
when :first_match
successful_details = providers.each do |provider|
details = yield(provider)

break details if details.error_code.nil?
rescue
next
end

if successful_details.is_a?(SDK::Provider::ResolutionDetails)
successful_details
else
SDK::Provider::ResolutionDetails.new(value: default_value, error_code: SDK::Provider::ErrorCode::GENERAL, reason: SDK::Provider::Reason::ERROR)
end
else
SDK::Provider::ResolutionDetails.new(value: default_value, error_code: SDK::Provider::ErrorCode::GENERAL, reason: "Unknown strategy for MetaProvider")
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module OpenFeature
META_PROVIDER_VERSION = "0.0.1"
end
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# frozen_string_literal: true

require_relative "lib/open_feature/sdk/provider/version"
require_relative "lib/openfeature/meta_provider_version"

Gem::Specification.new do |spec|
spec.name = "openfeature-meta_provider"
spec.version = OpenFeature::SDK::Provider::VERSION
spec.version = OpenFeature::META_PROVIDER_VERSION
spec.authors = ["OpenFeature Authors"]
spec.email = ["[email protected]"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
end
end

RSpec.describe OpenFeature::SDK::Provider::MetaProvider do
RSpec.describe OpenFeature::MetaProvider do
subject(:meta_provider) { described_class.new(providers: [provider_one, provider_two]) }

let(:provider_one) do
Expand Down
2 changes: 1 addition & 1 deletion providers/openfeature-meta_provider/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "bundler/setup"
require "debug"
require "open_feature/sdk/provider/meta_provider"
require "openfeature/meta_provider"
require "open_feature/sdk"

RSpec.configure do |config|
Expand Down
10 changes: 7 additions & 3 deletions release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
"bump-minor-pre-major": true,
"bump-patch-for-minor-pre-major": true,
"versioning": "default",
"extra-files": ["README.md"]
"extra-files": [
"README.md"
]
},
"providers/openfeature-meta_provider": {
"package-name": "openfeature-meta_provider",
"version-file": "lib/open_feature/sdk/provider/version.rb",
"version-file": "lib/openfeature/meta_provider_version.rb",
"bump-minor-pre-major": true,
"bump-patch-for-minor-pre-major": true,
"versioning": "default",
"extra-files": ["README.md"]
"extra-files": [
"README.md"
]
}
},
"changelog-sections": [
Expand Down

0 comments on commit 0228eab

Please sign in to comment.