Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/prefab-cloud-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ module Prefab
require 'prefab/prefab'
require 'prefab/murmer3'
require 'prefab/javascript_stub'
require 'prefab/migration_fallback'
9 changes: 7 additions & 2 deletions lib/prefab/config_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions lib/prefab/migration_fallback.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion lib/prefab/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -111,6 +113,7 @@ module DATASOURCES

@sse_sources = @sources
@config_sources = @sources
@migration_fallback = Prefab::MigrationFallback.new(migration_fallback)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to make this assignment conditional based on the presence of migration_fallback


@telemetry_destination = @sources.select do |source|
source.start_with?('https://') && (source.include?("belt") || source.include?("suspenders"))
Expand Down
9 changes: 9 additions & 0 deletions test/test_config_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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