diff --git a/providers/openfeature-meta_provider/Gemfile.lock b/providers/openfeature-meta_provider/Gemfile.lock index 5412384..5ad1502 100644 --- a/providers/openfeature-meta_provider/Gemfile.lock +++ b/providers/openfeature-meta_provider/Gemfile.lock @@ -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 @@ -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) diff --git a/providers/openfeature-meta_provider/lib/open_feature/sdk/provider/meta_provider.rb b/providers/openfeature-meta_provider/lib/open_feature/sdk/provider/meta_provider.rb deleted file mode 100644 index 6b6155e..0000000 --- a/providers/openfeature-meta_provider/lib/open_feature/sdk/provider/meta_provider.rb +++ /dev/null @@ -1,81 +0,0 @@ -# frozen_string_literal: true - -require_relative "version" - -module OpenFeature - module SDK - module Provider - # Used to pull from multiple providers. - class MetaProvider - # @param providers [Array] - # @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 - 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?(ResolutionDetails) - successful_details - else - ResolutionDetails.new(value: default_value, error_code: ErrorCode::GENERAL, reason: Reason::ERROR) - end - else - ResolutionDetails.new(value: default_value, error_code: ErrorCode::GENERAL, reason: "Unknown strategy for MetaProvider") - end - end - end - end - end -end diff --git a/providers/openfeature-meta_provider/lib/open_feature/sdk/provider/version.rb b/providers/openfeature-meta_provider/lib/open_feature/sdk/provider/version.rb deleted file mode 100644 index 07f0ea6..0000000 --- a/providers/openfeature-meta_provider/lib/open_feature/sdk/provider/version.rb +++ /dev/null @@ -1,9 +0,0 @@ -# frozen_string_literal: true - -module OpenFeature - module SDK - module Provider - VERSION = "0.0.1" - end - end -end \ No newline at end of file diff --git a/providers/openfeature-meta_provider/lib/openfeature/meta_provider.rb b/providers/openfeature-meta_provider/lib/openfeature/meta_provider.rb new file mode 100644 index 0000000..2360109 --- /dev/null +++ b/providers/openfeature-meta_provider/lib/openfeature/meta_provider.rb @@ -0,0 +1,75 @@ +# frozen_string_literal: true + +module OpenFeature + # Used to pull from multiple providers. + class MetaProvider + # @param providers [Array] + # @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 diff --git a/providers/openfeature-meta_provider/lib/openfeature/meta_provider_version.rb b/providers/openfeature-meta_provider/lib/openfeature/meta_provider_version.rb new file mode 100644 index 0000000..fa7ae5f --- /dev/null +++ b/providers/openfeature-meta_provider/lib/openfeature/meta_provider_version.rb @@ -0,0 +1,3 @@ +module OpenFeature + META_PROVIDER_VERSION = "0.0.1" +end diff --git a/providers/openfeature-meta_provider/openfeature-meta_provider.gemspec b/providers/openfeature-meta_provider/openfeature-meta_provider.gemspec index 4fa63b3..e15b934 100644 --- a/providers/openfeature-meta_provider/openfeature-meta_provider.gemspec +++ b/providers/openfeature-meta_provider/openfeature-meta_provider.gemspec @@ -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 = ["cncf-openfeature-contributors@lists.cncf.io"] diff --git a/providers/openfeature-meta_provider/spec/open_feature/sdk/provider/meta_provider_spec.rb b/providers/openfeature-meta_provider/spec/openfeature/meta_provider_spec.rb similarity index 98% rename from providers/openfeature-meta_provider/spec/open_feature/sdk/provider/meta_provider_spec.rb rename to providers/openfeature-meta_provider/spec/openfeature/meta_provider_spec.rb index e2ce587..12738d4 100644 --- a/providers/openfeature-meta_provider/spec/open_feature/sdk/provider/meta_provider_spec.rb +++ b/providers/openfeature-meta_provider/spec/openfeature/meta_provider_spec.rb @@ -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 diff --git a/providers/openfeature-meta_provider/spec/spec_helper.rb b/providers/openfeature-meta_provider/spec/spec_helper.rb index 04ca114..77e0e83 100644 --- a/providers/openfeature-meta_provider/spec/spec_helper.rb +++ b/providers/openfeature-meta_provider/spec/spec_helper.rb @@ -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| diff --git a/release-please-config.json b/release-please-config.json index ced9746..1020ece 100644 --- a/release-please-config.json +++ b/release-please-config.json @@ -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": [