Skip to content

Commit

Permalink
Merge pull request #428 from radford/mysql-equates-dash-and-underscore
Browse files Browse the repository at this point in the history
mysql_deepmerge should treat underscore and dash equivalently, as mysql does
  • Loading branch information
Ashley Penney committed Jan 24, 2014
2 parents 3cd0938 + 16baff6 commit 3f9f0f6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
28 changes: 17 additions & 11 deletions lib/puppet/parser/functions/mysql_deepmerge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

14 changes: 14 additions & 0 deletions spec/unit/puppet/functions/mysql_deepmerge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3f9f0f6

Please sign in to comment.