|
1 |
| -# @summary Recursively merges two or more hashes together and returns the resulting hash. |
| 1 | +# @summary Recursively merges two or more hashes together, normalises keys with differing use of dashesh and underscores, |
| 2 | +# then returns the resulting hash. |
2 | 3 | #
|
3 | 4 | # @example
|
4 | 5 | # $hash1 = {'one' => 1, 'two' => 2, 'three' => { 'four' => 4 } }
|
5 | 6 | # $hash2 = {'two' => 'dos', 'three' => { 'five' => 5 } }
|
6 |
| -# $merged_hash = mysql::deepmerge($hash1, $hash2) |
| 7 | +# $merged_hash = mysql::normalise_and_deepmerge($hash1, $hash2) |
7 | 8 | # # The resulting hash is equivalent to:
|
8 | 9 | # # $merged_hash = { 'one' => 1, 'two' => 'dos', 'three' => { 'four' => 4, 'five' => 5 } }
|
9 | 10 | #
|
10 | 11 | # - When there is a duplicate key that is a hash, they are recursively merged.
|
11 | 12 | # - When there is a duplicate key that is not a hash, the key in the rightmost hash will "win."
|
12 | 13 | # - When there are conficting uses of dashes and underscores in two keys (which mysql would otherwise equate), the rightmost style will win.
|
13 | 14 | #
|
14 |
| -Puppet::Functions.create_function(:'mysql::deepmerge') do |
15 |
| - def deepmerge(*args) |
| 15 | +Puppet::Functions.create_function(:'mysql::normalise_and_deepmerge') do |
| 16 | + def normalise_and_deepmerge(*args) |
16 | 17 | if args.length < 2
|
17 |
| - raise Puppet::ParseError, _('mysql::deepmerge(): wrong number of arguments (%{args_length}; must be at least 2)') % { args_length: args.length } |
| 18 | + raise Puppet::ParseError, _('mysql::normalise_and_deepmerge(): wrong number of arguments (%{args_length}; must be at least 2)') % { args_length: args.length } |
18 | 19 | end
|
19 | 20 |
|
20 | 21 | result = {}
|
21 | 22 | args.each do |arg|
|
22 | 23 | next if arg.is_a?(String) && arg.empty? # empty string is synonym for puppet's undef
|
23 | 24 | # If the argument was not a hash, skip it.
|
24 | 25 | unless arg.is_a?(Hash)
|
25 |
| - raise Puppet::ParseError, _('mysql::deepmerge: unexpected argument type %{arg_class}, only expects hash arguments.') % { args_class: args.class } |
| 26 | + raise Puppet::ParseError, _('mysql::normalise_and_deepmerge: unexpected argument type %{arg_class}, only expects hash arguments.') % { args_class: args.class } |
26 | 27 | end
|
27 | 28 |
|
28 | 29 | # We need to make a copy of the hash since it is frozen by puppet
|
|
0 commit comments