puppet-nova/lib/puppet/functions/to_array_of_json_strings.rb
Takashi Kajinami 9b27968a04 Stop accepting encoded JSON string
... to avoid redundant parsing of json content from a given string.

This also allows us to add more strict validation later using Struct
data type.

Change-Id: I2a8bbd266d5e97a06b89c131bbd86e3710a68923
2025-03-17 01:43:38 +09:00

27 lines
737 B
Ruby

# 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