From a2f980d44d6703561769c5e0ef25a7f531417643 Mon Sep 17 00:00:00 2001 From: Nate Potter Date: Thu, 7 Jul 2016 21:10:22 -0700 Subject: (MODULES-3568) Move dig to dig44 and deprecate dig A new version of dig was introduced in Puppet 4.5.0 that isn't compatible with the stdlib version of dig. To maintain backwards compatibility and ensure that tests for stdlib aren't broken, this patch renames dig to dig44 and adds a deprecation warning to the stdlib dig function. --- lib/puppet/parser/functions/dig44.rb | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 lib/puppet/parser/functions/dig44.rb (limited to 'lib/puppet/parser/functions/dig44.rb') diff --git a/lib/puppet/parser/functions/dig44.rb b/lib/puppet/parser/functions/dig44.rb new file mode 100644 index 0000000..a7de363 --- /dev/null +++ b/lib/puppet/parser/functions/dig44.rb @@ -0,0 +1,56 @@ +# +# dig44.rb +# + +module Puppet::Parser::Functions + newfunction(:dig44, :type => :rvalue, :doc => <<-EOS +DEPRECATED: This function has been replaced in puppet 4.5.0. + +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 = dig44($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, "dig44" 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, "dig44(): 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, "dig44(): first argument must be a hash or an array, " << + "given #{data.class.name}") + end + + unless path.is_a? Array + raise(Puppet::ParseError, "dig44(): 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 From 789263cab5ca2f38face3399076f4d3be34b3d3d Mon Sep 17 00:00:00 2001 From: Dmitry Ilyin Date: Tue, 30 Aug 2016 15:12:29 -0500 Subject: Refactor dig44 function The current implementation of the dig44 function has the following problems: * Doesn't recognise Puppet4's :undef value as an empty value and doesn't return the default value. * Doesn't make a different between false and nil value and returns the default value for a non-empty false value --- lib/puppet/parser/functions/dig44.rb | 41 +++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 12 deletions(-) (limited to 'lib/puppet/parser/functions/dig44.rb') diff --git a/lib/puppet/parser/functions/dig44.rb b/lib/puppet/parser/functions/dig44.rb index a7de363..d703380 100644 --- a/lib/puppet/parser/functions/dig44.rb +++ b/lib/puppet/parser/functions/dig44.rb @@ -1,23 +1,30 @@ -# -# dig44.rb +# +# dig44.rb # module Puppet::Parser::Functions - newfunction(:dig44, :type => :rvalue, :doc => <<-EOS + newfunction( + :dig44, + :type => :rvalue, + :arity => -2, + :doc => <<-eos DEPRECATED: This function has been replaced in puppet 4.5.0. -Looks up into a complex structure of arrays and hashes and returns nil +Looks up into a complex structure of arrays and hashes and returns a value 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. +Key can contain slashes to describe path components. The function will go down +the structure and try to extract the required value. $data = { 'a' => { 'b' => [ 'b1', 'b2', - 'b3' ]}} + 'b3', + ] + } +} $value = dig44($data, ['a', 'b', '2'], 'not_found') => $value = 'b3' @@ -29,18 +36,18 @@ b -> second hash key 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, "dig44" accepts default +In addition to the required "key" argument, the function accepts a 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| + eos + ) do |arguments| # Two arguments are required raise(Puppet::ParseError, "dig44(): 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)) + unless data.is_a?(Hash) or data.is_a?(Array) raise(Puppet::ParseError, "dig44(): first argument must be a hash or an array, " << "given #{data.class.name}") end @@ -50,7 +57,17 @@ missing. And the fourth argument can set a variable path separator. "given #{path.class.name}") end - value = path.reduce(data) { |h, k| (h.is_a?(Hash) || h.is_a?(Array)) ? h[k] : break } + value = path.reduce(data) do |structure, key| + if structure.is_a? Hash or structure.is_a? Array + if structure.is_a? Array + key = Integer key rescue break + end + break if structure[key].nil? or structure[key] == :undef + structure[key] + else + break + end + end value.nil? ? default : value end end -- cgit v1.2.3 From 033d0180c7a4146fc66deef3710506475efedb16 Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Thu, 15 Sep 2016 18:14:23 +0100 Subject: Fix whitespace --- lib/puppet/parser/functions/dig44.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet/parser/functions/dig44.rb') diff --git a/lib/puppet/parser/functions/dig44.rb b/lib/puppet/parser/functions/dig44.rb index d703380..1e7c318 100644 --- a/lib/puppet/parser/functions/dig44.rb +++ b/lib/puppet/parser/functions/dig44.rb @@ -1,5 +1,5 @@ -# -# dig44.rb +# +# dig44.rb # module Puppet::Parser::Functions -- cgit v1.2.3