mysql_deepmerge should treat underscore and dash equivalently, as mysql does
This commit is contained in:
parent
f348e12a37
commit
16baff686c
2 changed files with 32 additions and 12 deletions
|
@ -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 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
|
||||
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(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
|
||||
|
|
|
@ -73,5 +73,19 @@ describe Puppet::Parser::Functions.function(:mysql_deepmerge) do
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue