X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=3rdparty%2Fmodules%2Fstdlib%2Flib%2Fpuppet%2Fparser%2Ffunctions%2Fdig44.rb;fp=3rdparty%2Fmodules%2Fstdlib%2Flib%2Fpuppet%2Fparser%2Ffunctions%2Fdig44.rb;h=91724f43c06c3c9989280e5cf9ee12cdcd7e1436;hb=131e09855e065be940e104d9ab0f18940cc76257;hp=21c0a8ce2ce99f7c2e3765b8516139355ffb2aeb;hpb=407d322498f4fde815abf381007fbecfe5c10b2b;p=mirror%2Fdsa-puppet.git diff --git a/3rdparty/modules/stdlib/lib/puppet/parser/functions/dig44.rb b/3rdparty/modules/stdlib/lib/puppet/parser/functions/dig44.rb index 21c0a8ce2..91724f43c 100644 --- a/3rdparty/modules/stdlib/lib/puppet/parser/functions/dig44.rb +++ b/3rdparty/modules/stdlib/lib/puppet/parser/functions/dig44.rb @@ -1,69 +1,64 @@ # # dig44.rb # - module Puppet::Parser::Functions newfunction( - :dig44, - :type => :rvalue, - :arity => -2, - :doc => <<-eos -DEPRECATED: This function has been replaced in puppet 4.5.0. + :dig44, + :type => :rvalue, + :arity => -2, + :doc => <<-DOC + DEPRECATED: This function has been replaced in Puppet 4.5.0. -Looks up into a complex structure of arrays and hashes and returns a value -or the default value if nothing was found. + Looks up into a complex structure of arrays and hashes and returns a value + or the default value if nothing was found. -Key can contain slashes to describe path components. 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', - ] - } -} + $data = { + 'a' => { + 'b' => [ + 'b1', + 'b2', + 'b3', + ] + } + } -$value = dig44($data, ['a', 'b', '2'], 'not_found') -=> $value = '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 + 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. + 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 "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 + 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. + DOC ) 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 - 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 - - unless path.is_a? Array - raise(Puppet::ParseError, "dig44(): second argument must be an array, given #{path.class.name}") - end + raise(Puppet::ParseError, "dig44(): first argument must be a hash or an array, given #{data.class.name}") unless data.is_a?(Hash) || data.is_a?(Array) + raise(Puppet::ParseError, "dig44(): second argument must be an array, given #{path.class.name}") unless path.is_a? Array 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 + break unless structure.is_a?(Hash) || structure.is_a?(Array) + if structure.is_a? Array + begin + key = Integer key + rescue + break end - break if structure[key].nil? or structure[key] == :undef - structure[key] - else - break end + break if structure[key].nil? || structure[key] == :undef + structure[key] end value.nil? ? default : value end