Puppet Function: dig44

Defined in:
modules/stdlib/lib/puppet/parser/functions/dig44.rb
Function type:
Ruby 3.x API

Overview

dig44()String, Any

Key can contain slashes to describe path components. The function will go down the structure and try to extract the required value.

“` $data = {

'a' => {
  'b' => [
    'b1',
    'b2',
    'b3',
  ]
}

}

$value = dig44($data, ['a', 'b', 2]) # $value = 'b3'

# with all possible options $value = dig44($data, ['a', 'b', 2], 'not_found') # $value = 'b3'

# using the default value $value = dig44($data, ['a', 'b', 'c', 'd'], 'not_found') # $value = 'not_found' “`

> *Note: *Deprecated* This function has been replaced with a built-in

[`dig`](https://puppet.com/docs/puppet/latest/function.html#dig) function as of
Puppet 4.5.0.

Returns:

  • (String)

    'not_found' will be returned if nothing is found

  • (Any)

    the value that was searched for



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'modules/stdlib/lib/puppet/parser/functions/dig44.rb', line 5

newfunction(
  :dig44,
  :type => :rvalue,
  :arity => -2,
  :doc => <<-DOC
  @summary
    **DEPRECATED**: Looks up into a complex structure of arrays and hashes and returns a value
    or the default value if nothing was found.

  Key can contain slashes to describe path components. The function will go down
  the structure and try to extract the required value.

  ```
  $data = {
    'a' => {
      'b' => [
        'b1',
        'b2',
        'b3',
      ]
    }
  }

  $value = dig44($data, ['a', 'b', 2])
  # $value = 'b3'

  # with all possible options
  $value = dig44($data, ['a', 'b', 2], 'not_found')
  # $value = 'b3'

  # using the default value
  $value = dig44($data, ['a', 'b', 'c', 'd'], 'not_found')
  # $value = 'not_found'
  ```

  > **Note:* **Deprecated** This function has been replaced with a built-in
    [`dig`](https://puppet.com/docs/puppet/latest/function.html#dig) function as of
    Puppet 4.5.0.

  @return [String] 'not_found' will be returned if nothing is found
  @return [Any] the value that was searched for
DOC
) do |arguments|
  # Two arguments are required
  raise(Puppet::ParseError, "dig44(): Wrong number of arguments given (#{arguments.size} for at least 2)") if arguments.size < 2

  data, path, default = *arguments

  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)
  raise(Puppet::ParseError, "dig44(): second argument must be an array, given #{path.class.name}") unless path.is_a? Array

  value = path.reduce(data) do |structure, key|
    break unless structure.is_a?(Hash) || structure.is_a?(Array)
    if structure.is_a? Array
      begin
        key = Integer key
      rescue
        break
      end
    end
    break if structure[key].nil? || structure[key] == :undef
    structure[key]
  end
  value.nil? ? default : value
end