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