File: to_array_of_json_strings.rb

package info (click to toggle)
puppet-module-nova 27.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,068 kB
  • sloc: ruby: 11,144; python: 33; makefile: 10; sh: 10
file content (26 lines) | stat: -rw-r--r-- 737 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Convert input array of hashes (optionally JSON encoded)
# to a puppet Array of JSON encoded Strings.
Puppet::Functions.create_function(:to_array_of_json_strings) do
  def _array_of_hash?(list)
    return false unless list.class == Array
    list.each do |e|
      return false unless e.class == Hash
    end
    true
  end

  def to_array_of_json_strings(*args)
    if (args.size != 1) then
      raise Puppet::ParseError, 'to_array_of_json_strings(): Wrong number of arguments'
    end
    list = args[0]
    unless _array_of_hash?(list)
      raise Puppet::ParseError, "Syntax error: #{args[0]} is not an Array or JSON encoded String"
    end
    rv = []
    list.each do |e|
      rv.push(e.to_json)
    end
    return rv
  end
end