diff --git a/lib/puppet/parser/functions/dig.rb b/lib/puppet/parser/functions/dig.rb deleted file mode 100644 index 81b58e1b9..000000000 --- a/lib/puppet/parser/functions/dig.rb +++ /dev/null @@ -1,59 +0,0 @@ -# frozen_string_literal: true - -# -# dig.rb -# -module Puppet::Parser::Functions - newfunction(:dig, type: :rvalue, doc: <<-DOC - @summary - **DEPRECATED** Retrieves a value within multiple layers of hashes and arrays via an - array of keys containing a path. - - @return - The function goes through the structure by each path component and tries to return - the value at the end of the path. - - In addition to the required path argument, the function accepts the default argument. - It is returned if the path is not correct, if no value was found, or if any other error - has occurred. - - ```ruby - $data = { - 'a' => { - 'b' => [ - 'b1', - 'b2', - 'b3', - ] - } - } - - $value = dig($data, ['a', 'b', 2]) - # $value = 'b3' - - # with all possible options - $value = dig($data, ['a', 'b', 2], 'not_found') - # $value = 'b3' - - # using the default value - $value = dig($data, ['a', 'b', 'c', 'd'], 'not_found') - # $value = 'not_found' - ``` - - 1. `$data` The data structure we are working with. - 2. `['a', 'b', 2]` The path array. - 3. `not_found` The default value. It is returned if nothing is found. - - > **Note:* - **Deprecated** This function has been replaced with a built-in - [`dig`](https://puppet.com/docs/puppet/latest/function.html#dig) function as of - Puppet 4.5.0. Use [`dig44()`](#dig44) for backwards compatibility or use the new version. - DOC - ) do |arguments| - warning('dig() DEPRECATED: This function has been replaced in Puppet 4.5.0, please use dig44() for backwards compatibility or use the new version.') - unless Puppet::Parser::Functions.autoloader.loaded?(:dig44) - Puppet::Parser::Functions.autoloader.load(:dig44) - end - function_dig44(arguments) - end -end diff --git a/lib/puppet/parser/functions/dig44.rb b/lib/puppet/parser/functions/dig44.rb deleted file mode 100644 index c9e6b69de..000000000 --- a/lib/puppet/parser/functions/dig44.rb +++ /dev/null @@ -1,68 +0,0 @@ -# frozen_string_literal: true - -# -# dig44.rb -# -module Puppet::Parser::Functions - newfunction(:dig44, type: :rvalue, arity: -2, doc: <<-DOC - @summary - **DEPRECATED**: 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. - - ``` - $data = { - 'a' => { - 'b' => [ - 'b1', - 'b2', - 'b3', - ] - } - } - - $value = dig44($data, ['a', 'b', 2]) - # $value = 'b3' - - # with all possible options - $value = dig44($data, ['a', 'b', 2], 'not_found') - # $value = 'b3' - - # using the default value - $value = dig44($data, ['a', 'b', 'c', 'd'], 'not_found') - # $value = 'not_found' - ``` - - > **Note:* **Deprecated** This function has been replaced with a built-in - [`dig`](https://puppet.com/docs/puppet/latest/function.html#dig) function as of - Puppet 4.5.0. - - @return [String] 'not_found' will be returned if nothing is found - @return [Any] the value that was searched for - 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 - - 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| - break unless structure.is_a?(Hash) || structure.is_a?(Array) - if structure.is_a? Array - begin - key = Integer key - rescue - break - end - end - break if structure[key].nil? || structure[key] == :undef - structure[key] - end - value.nil? ? default : value - end -end diff --git a/lib/puppet/parser/functions/try_get_value.rb b/lib/puppet/parser/functions/try_get_value.rb deleted file mode 100644 index 00fdf5b46..000000000 --- a/lib/puppet/parser/functions/try_get_value.rb +++ /dev/null @@ -1,58 +0,0 @@ -# frozen_string_literal: true - -# -# try_get_value.rb -# -module Puppet::Parser::Functions - newfunction(:try_get_value, type: :rvalue, arity: -2, doc: <<-DOC - @summary - **DEPRECATED:** this function is deprecated, please use dig() instead. - - @return - 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. - `` - $data = { - 'a' => { - 'b' => [ - 'b1', - 'b2', - 'b3', - ] - } - } - - $value = try_get_value($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. - / -> (optional) path delimiter. Defaults to '/'. - ``` - - In addition to the required "key" argument, "try_get_value" 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. - DOC - ) do |args| - warning('try_get_value() DEPRECATED: this function is deprecated, please use dig() instead.') - data = args[0] - path = args[1] || '' - default = args[2] - - if !(data.is_a?(Hash) || data.is_a?(Array)) || path == '' - return default || data - end - - separator = args[3] || '/' - path = path.split(separator).map { |key| (key =~ %r{^\d+$}) ? key.to_i : key } - function_dig([data, path, default]) - end -end diff --git a/spec/functions/dig44_spec.rb b/spec/functions/dig44_spec.rb deleted file mode 100644 index 576935f3f..000000000 --- a/spec/functions/dig44_spec.rb +++ /dev/null @@ -1,134 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe 'dig44' do - let(:undef_value) do - (Puppet::Util::Package.versioncmp(Puppet.version, '6.0.0') < 0) ? :undef : nil - end - - let(:data) do - { - 'a' => { - 'g' => '2', - 'e' => [ - 'f0', - 'f1', - { - 'x' => { - 'y' => 'z', - }, - }, - 'f3', - ], - }, - 'b' => true, - 'c' => false, - 'd' => '1', - 'e' => undef_value, - 'f' => nil, - } - end - - let(:utf8_data) do - { - 'ẵ' => { - 'в' => [ - '©', - 'ĝ', - 'に', - ], - }, - } - end - - context 'with single values' do - it 'exists' do - is_expected.not_to be_nil - end - - it 'requires two arguments' do - is_expected.to run.with_params.and_raise_error(ArgumentError, %r{Wrong number of arguments}) - end - - it 'fails if the data is not a structure' do - is_expected.to run.with_params('test', []).and_raise_error(Puppet::Error, %r{first argument must be a hash or an array}) - end - - it 'fails if the path is not an array' do - is_expected.to run.with_params({}, '').and_raise_error(Puppet::Error, %r{second argument must be an array}) - end - - it 'returns the value if the value is string' do - is_expected.to run.with_params(data, ['d'], 'default').and_return('1') - end - - it 'returns true if the value is true' do - is_expected.to run.with_params(data, ['b'], 'default').and_return(true) - end - - it 'returns false if the value is false' do - is_expected.to run.with_params(data, ['c'], 'default').and_return(false) - end - - it 'returns the default if the value is nil' do - is_expected.to run.with_params(data, ['f'], 'default').and_return('default') - end - - it 'returns the default if the value is :undef (same as nil)' do - is_expected.to run.with_params(data, ['e'], 'default').and_return('default') - end - - it 'returns the default if the path is not found' do - is_expected.to run.with_params(data, ['missing'], 'default').and_return('default') - end - end - - context 'with structured values' do - it 'is able to extract a deeply nested hash value' do - is_expected.to run.with_params(data, ['a', 'g'], 'default').and_return('2') - end - - it 'returns the default value if the path is too long' do - is_expected.to run.with_params(data, ['a', 'g', 'c', 'd'], 'default').and_return('default') - end - - it 'supports an array index (number) in the path' do - is_expected.to run.with_params(data, ['a', 'e', 1], 'default').and_return('f1') - end - - it 'supports an array index (string) in the path' do - is_expected.to run.with_params(data, ['a', 'e', '1'], 'default').and_return('f1') - end - - it 'returns the default value if an array index is not a number' do - is_expected.to run.with_params(data, ['a', 'b', 'c'], 'default').and_return('default') - end - - it 'returns the default value if and index is out of array length' do - is_expected.to run.with_params(data, ['a', 'e', '5'], 'default').and_return('default') - end - - it 'is able to path though both arrays and hashes' do - is_expected.to run.with_params(data, ['a', 'e', '2', 'x', 'y'], 'default').and_return('z') - end - - it 'returns "nil" if value is not found and no default value is provided' do - is_expected.to run.with_params(data, ['a', '1']).and_return(nil) - end - end - - context 'with internationalization (i18N) values' do - it 'is able to return a unicode character' do - is_expected.to run.with_params(utf8_data, ['ẵ', 'в', 0]).and_return('©') - end - - it 'is able to return a utf8 character' do - is_expected.to run.with_params(utf8_data, ['ẵ', 'в', 1]).and_return('ĝ') - end - - it 'is able to return a double byte character' do - is_expected.to run.with_params(utf8_data, ['ẵ', 'в', 2]).and_return('に') - end - end -end diff --git a/spec/functions/dig_spec.rb b/spec/functions/dig_spec.rb deleted file mode 100644 index a11ab7ec6..000000000 --- a/spec/functions/dig_spec.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe 'dig' do - it 'exists' do - expect(Puppet::Parser::Functions.function('dig')).to eq('function_dig') - end - - it 'gives a deprecation warning when called' do - expect(scope).to receive(:warning).with('dig() DEPRECATED: This function has been replaced in Puppet 4.5.0, please use dig44() for backwards compatibility or use the new version.') - scope.function_dig([{}, []]) - end -end diff --git a/spec/functions/try_get_value_spec.rb b/spec/functions/try_get_value_spec.rb deleted file mode 100644 index 2b9b96c65..000000000 --- a/spec/functions/try_get_value_spec.rb +++ /dev/null @@ -1,114 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe 'try_get_value' do - let(:data) do - { - 'a' => { - 'g' => '2', - 'e' => [ - 'f0', - 'f1', - { - 'x' => { - 'y' => 'z', - }, - }, - 'f3', - ], - }, - 'b' => true, - 'c' => false, - 'd' => '1', - } - end - - context 'with single values' do - it 'exists' do - is_expected.not_to eq(nil) - end - - it 'is able to return a single value' do - is_expected.to run.with_params('test').and_return('test') - end - - it 'uses the default value if data is a single value and path is present' do - is_expected.to run.with_params('test', 'path', 'default').and_return('default') - end - - it 'returns default if there is no data' do - is_expected.to run.with_params(nil, nil, 'default').and_return('default') - end - - it 'is able to use data structures as default values' do - is_expected.to run.with_params('test', 'path', data).and_return(data) - end - end - - context 'with structure values' do - it 'is able to extracts a single hash value' do - is_expected.to run.with_params(data, 'd', 'default').and_return('1') - end - - it 'is able to extract a deeply nested hash value' do - is_expected.to run.with_params(data, 'a/g', 'default').and_return('2') - end - - it 'returns the default value if the path is not found' do - is_expected.to run.with_params(data, 'missing', 'default').and_return('default') - end - - it 'returns the default value if the path is too long' do - is_expected.to run.with_params(data, 'a/g/c/d', 'default').and_return('default') - end - - it 'supports an array index in the path' do - is_expected.to run.with_params(data, 'a/e/1', 'default').and_return('f1') - end - - it 'returns the default value if an array index is not a number' do - is_expected.to run.with_params(data, 'a/b/c', 'default').and_return('default') - end - - it 'returns the default value if and index is out of array length' do - is_expected.to run.with_params(data, 'a/e/5', 'default').and_return('default') - end - - it 'is able to path though both arrays and hashes' do - is_expected.to run.with_params(data, 'a/e/2/x/y', 'default').and_return('z') - end - - it 'is able to return "true" value: default' do - is_expected.to run.with_params(data, 'b', 'default').and_return(true) - end - - it 'is able to return "true" value' do - is_expected.to run.with_params(data, 'm', true).and_return(true) - end - - it 'is able to return "false" value: default' do - is_expected.to run.with_params(data, 'c', 'default').and_return(false) - end - - it 'is able to return "false" value' do - is_expected.to run.with_params(data, 'm', false).and_return(false) - end - - it 'returns "nil" if value is not found and no default value is provided' do - is_expected.to run.with_params(data, 'a/1').and_return(nil) - end - - it 'is able to use a custom path separator' do - is_expected.to run.with_params(data, 'a::g', 'default', '::').and_return('2') - end - - it 'is able to use a custom path separator: default' do - is_expected.to run.with_params(data, 'a::c', 'default', '::').and_return('default') - end - - it 'is able to throw an error with incorrect params' do - is_expected.to run.with_params.and_raise_error(ArgumentError, %r{Wrong number of arguments}i) - end - end -end