Skip to content

Commit

Permalink
Fix unique merge of scalar values #264
Browse files Browse the repository at this point in the history
  • Loading branch information
oneiros committed Feb 16, 2024
1 parent 92f32e6 commit 41e36b6
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 5 deletions.
6 changes: 2 additions & 4 deletions app/models/hiera_data/lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,15 @@ def merge_values(values, strategy)
end

def unique_array_merge(values)
values.inject do |memo, value|
memo ||= []
values.inject([]) do |memo, value|
raise Hdm::Error, "Merge strategy `unique` is not applicable to a hash." if value.is_a?(Hash)

memo + Array(value)
end.uniq
end

def flat_hash_merge(values)
values.inject do |memo, value|
memo ||= {}
values.inject({}) do |memo, value|
raise Hdm::Error, "Merge strategy `hash` can only be used with hashes." unless value.is_a?(Hash)

value.merge(memo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ hdm_nested_hash:
hdm_mixed_types: 42
hdm_mixed_types2:
from_common: 42
hdm_integer_with_duplicates: 2
lookup_options:
'^hdm_duplicates':
merge: hash
Expand All @@ -31,4 +32,5 @@ lookup_options:
strategy: deep
hdm_mixed_types:
merge: deep

hdm_integer_with_duplicates:
merge: unique
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ hdm_nested_hash:
hdm_mixed_types:
from_node: 23
hdm_mixed_types2: 23
hdm_integer_with_duplicates: 99
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ hdm_simple_hash:
hdm_nested_hash:
nested_hash:
added_by_role: role
hdm_integer_with_duplicates: 99
6 changes: 6 additions & 0 deletions test/models/hiera_data/lookup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class LookupTest < ActiveSupport::TestCase
assert_equal expected, result
end

test "looking up integers with merge strategy `unique`" do
result = perform_lookup("hdm_integer_with_duplicates", merge_strategy: :unique)

assert_equal [99, 2], result
end

test "looking up a complex nested array with merge strategy `deep`" do
result = perform_lookup("hdm_nested_array", merge_strategy: :deep)
expected = [
Expand Down

0 comments on commit 41e36b6

Please sign in to comment.