diff --git a/lib/prefab-cloud-ruby.rb b/lib/prefab-cloud-ruby.rb index cc49601..85b81b9 100644 --- a/lib/prefab-cloud-ruby.rb +++ b/lib/prefab-cloud-ruby.rb @@ -54,3 +54,4 @@ module Prefab require 'prefab/prefab' require 'prefab/murmer3' require 'prefab/javascript_stub' +require 'prefab/migration_fallback' \ No newline at end of file diff --git a/lib/prefab/config_client.rb b/lib/prefab/config_client.rb index 055c328..98b2e5b 100644 --- a/lib/prefab/config_client.rb +++ b/lib/prefab/config_client.rb @@ -81,7 +81,7 @@ def get(key, default = NO_DEFAULT_PROVIDED, properties = NO_DEFAULT_PROVIDED) if evaluation evaluation.report_and_return(@base_client.evaluation_summary_aggregator) else - handle_default(key, default) + handle_default(key, context, default) end end @@ -95,7 +95,12 @@ def raw(key) @config_resolver.raw(key) end - def handle_default(key, default) + def handle_default(key, context, default) + if @options.migration_fallback + + return @options.migration_fallback.get(key, context, default) + end + return default if default != NO_DEFAULT_PROVIDED raise Prefab::Errors::MissingDefaultError, key if @options.on_no_default == Prefab::Options::ON_NO_DEFAULT::RAISE diff --git a/lib/prefab/migration_fallback.rb b/lib/prefab/migration_fallback.rb new file mode 100644 index 0000000..b0cf2f4 --- /dev/null +++ b/lib/prefab/migration_fallback.rb @@ -0,0 +1,12 @@ +class Prefab::MigrationFallback + LOG = Prefab::InternalLogger.new(self) + + def initialize(closure) + @closure = closure + end + + def get(key, context, default) + LOG.debug "Migration fallback for key: #{key}" + @closure.call(key, context, default) + end +end diff --git a/lib/prefab/options.rb b/lib/prefab/options.rb index 4cf7b68..c2dd1c6 100644 --- a/lib/prefab/options.rb +++ b/lib/prefab/options.rb @@ -20,6 +20,7 @@ class Options attr_reader :datafile attr_reader :global_context attr_accessor :is_fork + attr_reader :migration_fallback module ON_INITIALIZATION_FAILURE RAISE = :raise @@ -69,7 +70,8 @@ module DATASOURCES datafile: ENV['PREFAB_DATAFILE'], x_datafile: nil, # DEPRECATED in favor of `datafile` x_use_local_cache: false, - global_context: {} + global_context: {}, + migration_fallback: nil ) @api_key = api_key @namespace = namespace @@ -111,6 +113,7 @@ module DATASOURCES @sse_sources = @sources @config_sources = @sources + @migration_fallback = Prefab::MigrationFallback.new(migration_fallback) @telemetry_destination = @sources.select do |source| source.start_with?('https://') && (source.include?("belt") || source.include?("suspenders")) diff --git a/test/test_config_client.rb b/test/test_config_client.rb index 24880f9..03e4aa5 100644 --- a/test/test_config_client.rb +++ b/test/test_config_client.rb @@ -105,4 +105,13 @@ def test_cache_path_respects_xdg end end + def test_migration_fallback + options = Prefab::Options.new( + migration_fallback: ->(key, context, default) { "fallback value" }, + prefab_datasources: Prefab::Options::DATASOURCES::LOCAL_ONLY, + ) + + config_client = Prefab::ConfigClient.new(MockBaseClient.new(options), 10) + assert_equal "fallback value", config_client.get('anything') + end end