Puppet Function: clj_map

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

Overview

clj_map()Any

This converts a hash into a string containing a serialized clojure map.

Example:

The following puppet structure:

=> 9

Will yield the string:

9

Arguments: $hash

Returns:

  • (Any)


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

newfunction(:clj_map, :type => :rvalue, :doc => <<-EOS
This converts a hash into a string containing a serialized clojure map.

Example:

The following puppet structure:

{'foo' => 9}

Will yield the string:

{:foo 9}

Arguments: $hash
  EOS
) do |arguments|

  if arguments.empty?
    return []
  end

  if arguments.length == 1
    if ! arguments[0].kind_of?(Hash)
      raise(Puppet::Error, "clj_map(): argument must be a hash")
    end
  else
    raise(Puppet::Error, "clj_map(): only one argument accepted")
  end
  
  inner = []
  arguments[0].sort.each do |k,v|
    inner.push(":#{k} #{v}")
  end
  result = '{' + inner.join(" ") + ')'
  return result
end