diff --git a/lib/puppet/parser/functions/mysql_deepmerge.rb b/lib/puppet/parser/functions/mysql_deepmerge.rb index dadad540c..aca9c7a3d 100644 --- a/lib/puppet/parser/functions/mysql_deepmerge.rb +++ b/lib/puppet/parser/functions/mysql_deepmerge.rb @@ -12,6 +12,8 @@ module Puppet::Parser::Functions When there is a duplicate key that is a hash, they are recursively merged. When there is a duplicate key that is not a hash, the key in the rightmost hash will "win." + When there are conficting uses of dashes and underscores in two keys (which mysql would otherwise equate), + the rightmost style will win. ENDHEREDOC @@ -36,17 +38,21 @@ module Puppet::Parser::Functions end end +def has_normalized!(hash, key) + return true if hash.has_key?( key ) + return false unless key.match(/-|_/) + other_key = key.include?('-') ? key.gsub( '-', '_' ) : key.gsub( '_', '-' ) + return false unless hash.has_key?( other_key ) + hash[key] = hash.delete( other_key ) + return true; +end + def overlay( hash1, hash2 ) - hash2.each do |key, value| - if( value.is_a?(Hash) ) - if( ! hash1.has_key?( key ) or ! hash1[key].is_a?(Hash)) - hash1[key] = value - else - overlay( hash1[key], value ) - end - else - hash1[key] = value - end + hash2.each do |key, value| + if(has_normalized!( hash1, key ) and value.is_a?(Hash) and hash1[key].is_a?(Hash)) + overlay( hash1[key], value ) + else + hash1[key] = value end + end end - diff --git a/spec/unit/puppet/functions/mysql_deepmerge_spec.rb b/spec/unit/puppet/functions/mysql_deepmerge_spec.rb index 92577b184..fa9c72b78 100644 --- a/spec/unit/puppet/functions/mysql_deepmerge_spec.rb +++ b/spec/unit/puppet/functions/mysql_deepmerge_spec.rb @@ -73,5 +73,19 @@ hash['key1'].should == { 'a' => 1, 'b' => 99 } hash['key2'].should == { 'c' => 3 } end + + it 'should equate keys mod dash and underscore' do + hash = scope.function_mysql_deepmerge([{ 'a-b-c' => 1 } , { 'a_b_c' => 10 }]) + hash['a_b_c'].should == 10 + hash.should_not have_key('a-b-c') + end + + it 'should keep style of the last when keys are euqal mod dash and underscore' do + hash = scope.function_mysql_deepmerge([{ 'a-b-c' => 1, 'b_c_d' => { 'c-d-e' => 2, 'e-f-g' => 3 }} , { 'a_b_c' => 10, 'b-c-d' => { 'c_d_e' => 12 } }]) + hash['a_b_c'].should == 10 + hash.should_not have_key('a-b-c') + hash['b-c-d'].should == { 'e-f-g' => 3, 'c_d_e' => 12 } + hash.should_not have_key('b_c_d') + end end end