Skip to content
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

mysql_deepmerge should treat underscore and dash equivalently, as mysql does #428

Merged
Merged
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
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why's that indented?


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