comparison modules/stdlib/lib/puppet/parser/functions/merge.rb @ 272:c42fb28cff86

Update to a newer Python module This also pulls in an EPEL module (which we don't use) and a newer stdlib version.
author IBBoard <dev@ibboard.co.uk>
date Fri, 03 Jan 2020 19:56:04 +0000
parents addb0ea390a1
children d9352a684e62
comparison
equal deleted inserted replaced
271:c62728474654 272:c42fb28cff86
1 #
2 # merge.rb
3 #
1 module Puppet::Parser::Functions 4 module Puppet::Parser::Functions
2 newfunction(:merge, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args| 5 newfunction(:merge, :type => :rvalue, :doc => <<-'DOC') do |args|
3 Merges two or more hashes together and returns the resulting hash. 6 Merges two or more hashes together and returns the resulting hash.
4 7
5 For example: 8 For example:
6 9
7 $hash1 = {'one' => 1, 'two', => 2} 10 $hash1 = {'one' => 1, 'two', => 2}
10 # The resulting hash is equivalent to: 13 # The resulting hash is equivalent to:
11 # $merged_hash = {'one' => 1, 'two' => 'dos', 'three' => 'tres'} 14 # $merged_hash = {'one' => 1, 'two' => 'dos', 'three' => 'tres'}
12 15
13 When there is a duplicate key, the key in the rightmost hash will "win." 16 When there is a duplicate key, the key in the rightmost hash will "win."
14 17
15 ENDHEREDOC 18 DOC
16 19
17 if args.length < 2 20 if args.length < 2
18 raise Puppet::ParseError, ("merge(): wrong number of arguments (#{args.length}; must be at least 2)") 21 raise Puppet::ParseError, "merge(): wrong number of arguments (#{args.length}; must be at least 2)"
19 end 22 end
20 23
21 # The hash we accumulate into 24 # The hash we accumulate into
22 accumulator = Hash.new 25 accumulator = {}
23 # Merge into the accumulator hash 26 # Merge into the accumulator hash
24 args.each do |arg| 27 args.each do |arg|
25 next if arg.is_a? String and arg.empty? # empty string is synonym for puppet's undef 28 next if arg.is_a?(String) && arg.empty? # empty string is synonym for puppet's undef
26 unless arg.is_a?(Hash) 29 unless arg.is_a?(Hash)
27 raise Puppet::ParseError, "merge: unexpected argument type #{arg.class}, only expects hash arguments" 30 raise Puppet::ParseError, "merge: unexpected argument type #{arg.class}, only expects hash arguments"
28 end 31 end
29 accumulator.merge!(arg) 32 accumulator.merge!(arg)
30 end 33 end