Puppet Function: deep_implode

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

Overview

deep_implode()Any

Recursively flattens all keys of a hash into a dot-notated hash, deeply merging duplicate key values by natively combining them and returns the resulting hash.

That is confusing, look at the examples for more clarity.

For example:

$hash = {'top' => {'sub' => [1]}, 'top.sub' => [2] }
$flattened_hash = deep_implode($hash)
# The resulting hash is equivalent to:
# { 'top.sub' => [1, 2] }

When the function encounters array or hash values, they are concatenated or merged, respectively. When duplace paths for a key are generated, the function will prefer to retain keys with the longest root key.

Returns:

  • (Any)

    Hash



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
# File 'lib/puppet/parser/functions/deep_implode.rb', line 9

newfunction(
  :deep_implode,
  type: :rvalue,
  doc: <<-ENDHEREDOC) do |args|
  Recursively flattens all keys of a hash into a dot-notated
  hash, deeply merging duplicate key values by natively combining
  them and returns the resulting hash.

  That is confusing, look at the examples for more clarity.

  For example:

      $hash = {'top' => {'sub' => [1]}, 'top.sub' => [2] }
      $flattened_hash = deep_implode($hash)
      # The resulting hash is equivalent to:
      # { 'top.sub' => [1, 2] }

  When the function encounters array or hash values, they are
  concatenated or merged, respectively.
  When duplace paths for a key are generated, the function will prefer
  to retain keys with the longest root key.

  @return Hash
  ENDHEREDOC

  raise Puppet::ParseError, "deep_implode(): wrong number of arguments (#{args.length}; must be 1)" if args.length != 1

  arg = args[0]

  raise Puppet::ParseError, 'deep_implode: unexpected argument type, only expects hashes' unless arg.is_a? Hash

  return {} if arg.empty?

  Puppet_X::Elastic.deep_implode arg
end