Skip to content

Commit 3f9f0f6

Browse files
author
Ashley Penney
committed
Merge pull request #428 from radford/mysql-equates-dash-and-underscore
mysql_deepmerge should treat underscore and dash equivalently, as mysql does
2 parents 3cd0938 + 16baff6 commit 3f9f0f6

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

lib/puppet/parser/functions/mysql_deepmerge.rb

+17-11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ module Puppet::Parser::Functions
1212
1313
When there is a duplicate key that is a hash, they are recursively merged.
1414
When there is a duplicate key that is not a hash, the key in the rightmost hash will "win."
15+
When there are conficting uses of dashes and underscores in two keys (which mysql would otherwise equate),
16+
the rightmost style will win.
1517
1618
ENDHEREDOC
1719

@@ -36,17 +38,21 @@ module Puppet::Parser::Functions
3638
end
3739
end
3840

41+
def has_normalized!(hash, key)
42+
return true if hash.has_key?( key )
43+
return false unless key.match(/-|_/)
44+
other_key = key.include?('-') ? key.gsub( '-', '_' ) : key.gsub( '_', '-' )
45+
return false unless hash.has_key?( other_key )
46+
hash[key] = hash.delete( other_key )
47+
return true;
48+
end
49+
3950
def overlay( hash1, hash2 )
40-
hash2.each do |key, value|
41-
if( value.is_a?(Hash) )
42-
if( ! hash1.has_key?( key ) or ! hash1[key].is_a?(Hash))
43-
hash1[key] = value
44-
else
45-
overlay( hash1[key], value )
46-
end
47-
else
48-
hash1[key] = value
49-
end
51+
hash2.each do |key, value|
52+
if(has_normalized!( hash1, key ) and value.is_a?(Hash) and hash1[key].is_a?(Hash))
53+
overlay( hash1[key], value )
54+
else
55+
hash1[key] = value
5056
end
57+
end
5158
end
52-

spec/unit/puppet/functions/mysql_deepmerge_spec.rb

+14
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,19 @@
7373
hash['key1'].should == { 'a' => 1, 'b' => 99 }
7474
hash['key2'].should == { 'c' => 3 }
7575
end
76+
77+
it 'should equate keys mod dash and underscore' do
78+
hash = scope.function_mysql_deepmerge([{ 'a-b-c' => 1 } , { 'a_b_c' => 10 }])
79+
hash['a_b_c'].should == 10
80+
hash.should_not have_key('a-b-c')
81+
end
82+
83+
it 'should keep style of the last when keys are euqal mod dash and underscore' do
84+
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 } }])
85+
hash['a_b_c'].should == 10
86+
hash.should_not have_key('a-b-c')
87+
hash['b-c-d'].should == { 'e-f-g' => 3, 'c_d_e' => 12 }
88+
hash.should_not have_key('b_c_d')
89+
end
7690
end
7791
end

0 commit comments

Comments
 (0)