Debian Codegraph @

lib/puppet/functions/to_array_of_json_strings.rb

raw
1 # Convert input array of hashes (optionally JSON encoded)
2 # to a puppet Array of JSON encoded Strings.
3 Puppet::Functions.create_function(:to_array_of_json_strings) do
4 def _array_of_hash?(list)
5 return false unless list.class == Array
6 list.each do |e|
7 return false unless e.class == Hash
8 end
9 true
10 end
11
12 def to_array_of_json_strings(*args)
13 if (args.size != 1) then
14 raise Puppet::ParseError, 'to_array_of_json_strings(): Wrong number of arguments'
15 end
16 list = args[0]
17 unless _array_of_hash?(list)
18 raise Puppet::ParseError, "Syntax error: #{args[0]} is not an Array or JSON encoded String"
19 end
20 rv = []
21 list.each do |e|
22 rv.push(e.to_json)
23 end
24 return rv
25 end
26 end