From 3169a43f4c24d01c64c90ab9537da284f587c726 Mon Sep 17 00:00:00 2001 From: Maksym Melnychok Date: Mon, 8 Feb 2016 07:50:35 -0800 Subject: Add dig() function Deprecates #try_get_value() --- lib/puppet/parser/functions/dig.rb | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 lib/puppet/parser/functions/dig.rb (limited to 'lib/puppet/parser/functions/dig.rb') diff --git a/lib/puppet/parser/functions/dig.rb b/lib/puppet/parser/functions/dig.rb new file mode 100644 index 0000000..a9aa770 --- /dev/null +++ b/lib/puppet/parser/functions/dig.rb @@ -0,0 +1,54 @@ +# +# dig.rb +# + +module Puppet::Parser::Functions + newfunction(:dig, :type => :rvalue, :doc => <<-EOS +Looks up into a complex structure of arrays and hashes and returns nil +or the default value if nothing was found. + +Path is an array of keys to be looked up in data argument. The function +will go down the structure and try to extract the required value. + +$data = { + 'a' => { + 'b' => [ + 'b1', + 'b2', + 'b3' ]}} + +$value = dig($data, ['a', 'b', '2'], 'not_found') +=> $value = 'b3' + +a -> first hash key +b -> second hash key +2 -> array index starting with 0 + +not_found -> (optional) will be returned if there is no value or the path +did not match. Defaults to nil. + +In addition to the required "path" argument, "dig" accepts default +argument. It will be returned if no value was found or a path component is +missing. And the fourth argument can set a variable path separator. + EOS + ) do |arguments| + # Two arguments are required + raise(Puppet::ParseError, "dig(): Wrong number of arguments " + + "given (#{arguments.size} for at least 2)") if arguments.size < 2 + + data, path, default = *arguments + + if !(data.is_a?(Hash) || data.is_a?(Array)) + raise(Puppet::ParseError, "dig(): first argument must be a hash or an array, " << + "given #{data.class.name}") + end + + unless path.is_a? Array + raise(Puppet::ParseError, "dig(): second argument must be an array, " << + "given #{path.class.name}") + end + + value = path.reduce(data) { |h, k| (h.is_a?(Hash) || h.is_a?(Array)) ? h[k] : break } + value.nil? ? default : value + end +end -- cgit v1.2.3