4 module Puppet::Parser::Functions
10 DEPRECATED: This function has been replaced in Puppet 4.5.0.
12 Looks up into a complex structure of arrays and hashes and returns a value
13 or the default value if nothing was found.
15 Key can contain slashes to describe path components. The function will go down
16 the structure and try to extract the required value.
28 $value = dig44($data, ['a', 'b', '2'], 'not_found')
33 2 -> array index starting with 0
35 not_found -> (optional) will be returned if there is no value or the path
36 did not match. Defaults to nil.
38 In addition to the required "key" argument, the function accepts a default
39 argument. It will be returned if no value was found or a path component is
40 missing. And the fourth argument can set a variable path separator.
43 # Two arguments are required
44 raise(Puppet::ParseError, "dig44(): Wrong number of arguments given (#{arguments.size} for at least 2)") if arguments.size < 2
46 data, path, default = *arguments
48 raise(Puppet::ParseError, "dig44(): first argument must be a hash or an array, given #{data.class.name}") unless data.is_a?(Hash) || data.is_a?(Array)
49 raise(Puppet::ParseError, "dig44(): second argument must be an array, given #{path.class.name}") unless path.is_a? Array
51 value = path.reduce(data) do |structure, key|
52 break unless structure.is_a?(Hash) || structure.is_a?(Array)
53 if structure.is_a? Array
60 break if structure[key].nil? || structure[key] == :undef
63 value.nil? ? default : value