Skip to content

feat: Configurably allow duplicates in FileDataSourceImpl #298

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion lib/ldclient-rb/impl/integrations/file_data_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def initialize(data_store, data_source_update_sink, logger, options={})
if @paths.is_a? String
@paths = [ @paths ]
end
@allow_duplicates = options[:allow_duplicates] || false
@auto_update = options[:auto_update]
@use_listen = @auto_update && @@have_listen && !options[:force_polling]
@poll_interval = options[:poll_interval] || 1
Expand Down Expand Up @@ -139,7 +140,7 @@ def add_item(all_data, kind, item)
items = all_data[kind]
raise ArgumentError, "Received unknown item kind #{kind[:namespace]} in add_data" if items.nil? # shouldn't be possible since we preinitialize the hash
key = item[:key].to_sym
unless items[key].nil?
unless items[key].nil? || @allow_duplicates
raise ArgumentError, "#{kind[:namespace]} key \"#{item[:key]}\" was used more than once"
end
items[key] = Model.deserialize(kind, item)
Expand Down
3 changes: 3 additions & 0 deletions lib/ldclient-rb/integrations/file_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ module FileData
# @option options [Float] :poll_interval The minimum interval, in seconds, between checks for
# file modifications - used only if auto_update is true, and if the native file-watching
# mechanism from 'listen' is not being used. The default value is 1 second.
# @option options [Boolean] :allow_duplicates Do not raise an error if using multiple files
# that contain the same flag or segment key. If this is true, the last value for a given key
# will be used. The default is false.
# @return an object that can be stored in {Config#data_source}
#
def self.data_source(options={})
Expand Down
27 changes: 27 additions & 0 deletions spec/integrations/file_data_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ module Integrations
EOF
}

let(:alternate_flag_only_json) { <<-EOF
{
"flags": {
"flag1": {
"key": "flag1",
"on": false,
"fallthrough": {
"variation": 2
},
"variations": [ "fall", "off", "on" ]
}
}
}
EOF
}

let(:segment_only_json) { <<-EOF
{
"segments": {
Expand Down Expand Up @@ -243,6 +259,17 @@ def with_data_source(options, initialize_to_valid = false)
end
end

it "allows duplicate keys and uses the last loaded version when allow-duplicates is true" do
file1 = make_temp_file(flag_only_json)
file2 = make_temp_file(alternate_flag_only_json)
with_data_source({ paths: [ file1.path, file2.path ], allow_duplicates: true }) do |ds|
ds.start
expect(@store.initialized?).to eq(true)
expect(@store.all(LaunchDarkly::FEATURES).keys).to_not eq([])
expect(@store.all(LaunchDarkly::FEATURES)[:flag1][:on]).to eq(false)
end
end

it "does not reload modified file if auto-update is off" do
file = make_temp_file(flag_only_json)

Expand Down
Loading