Skip to content

Commit 9224143

Browse files
authored
Merge pull request #618 from ntpttr/fix/master/modules-3568
(MODULES-3568) Move dig to dig44 and deprecate dig
2 parents e723c7c + a2f980d commit 9224143

File tree

5 files changed

+152
-52
lines changed

5 files changed

+152
-52
lines changed

README.markdown

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ Returns the difference between two arrays. The returned array is a copy of the o
295295

296296
#### `dig`
297297

298+
DEPRECATED: This function has been replaced in Puppet 4.5.0, use dig44() for backwards compatibility or use the new version.
299+
298300
*Type*: rvalue.
299301

300302
Retrieves a value within multiple layers of hashes and arrays via an array of keys containing a path. The function goes through the structure by each path component and tries to return the value at the end of the path.
@@ -324,6 +326,42 @@ $value = dig($data, ['a', 'b', 'c', 'd'], 'not_found')
324326
# $value = 'not_found'
325327
~~~
326328

329+
1. **$data** The data structure we are working with.
330+
2. **['a', 'b', 2]** The path array.
331+
3. **'not_found'** The default value. It will be returned if nothing is found.
332+
(optional, defaults to *undef*)
333+
334+
#### `dig44`
335+
336+
*Type*: rvalue.
337+
338+
Retrieves a value within multiple layers of hashes and arrays via an array of keys containing a path. The function goes through the structure by each path component and tries to return the value at the end of the path.
339+
340+
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.
341+
342+
~~~ruby
343+
$data = {
344+
'a' => {
345+
'b' => [
346+
'b1',
347+
'b2',
348+
'b3',
349+
]
350+
}
351+
}
352+
353+
$value = dig44($data, ['a', 'b', 2])
354+
# $value = 'b3'
355+
356+
# with all possible options
357+
$value = dig44($data, ['a', 'b', 2], 'not_found')
358+
# $value = 'b3'
359+
360+
# using the default value
361+
$value = dig44($data, ['a', 'b', 'c', 'd'], 'not_found')
362+
# $value = 'not_found'
363+
~~~
364+
327365
1. **$data** The data structure we are working with.
328366
2. **['a', 'b', 2]** The path array.
329367
3. **'not_found'** The default value. It will be returned if nothing is found.

lib/puppet/parser/functions/dig.rb

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,13 @@
44

55
module Puppet::Parser::Functions
66
newfunction(:dig, :type => :rvalue, :doc => <<-EOS
7-
Looks up into a complex structure of arrays and hashes and returns nil
8-
or the default value if nothing was found.
9-
10-
Path is an array of keys to be looked up in data argument. The function
11-
will go down the structure and try to extract the required value.
12-
13-
$data = {
14-
'a' => {
15-
'b' => [
16-
'b1',
17-
'b2',
18-
'b3' ]}}
19-
20-
$value = dig($data, ['a', 'b', '2'], 'not_found')
21-
=> $value = 'b3'
22-
23-
a -> first hash key
24-
b -> second hash key
25-
2 -> array index starting with 0
26-
27-
not_found -> (optional) will be returned if there is no value or the path
28-
did not match. Defaults to nil.
29-
30-
In addition to the required "path" argument, "dig" accepts default
31-
argument. It will be returned if no value was found or a path component is
32-
missing. And the fourth argument can set a variable path separator.
7+
DEPRECATED: This function has been replaced in Puppet 4.5.0, please use dig44() for backwards compatibility or use the new version.
338
EOS
34-
) do |arguments|
35-
# Two arguments are required
36-
raise(Puppet::ParseError, "dig(): Wrong number of arguments " +
37-
"given (#{arguments.size} for at least 2)") if arguments.size < 2
38-
39-
data, path, default = *arguments
40-
41-
if !(data.is_a?(Hash) || data.is_a?(Array))
42-
raise(Puppet::ParseError, "dig(): first argument must be a hash or an array, " <<
43-
"given #{data.class.name}")
9+
) do |arguments|
10+
warning("dig() DEPRECATED: This function has been replaced in Puppet 4.5.0, please use dig44() for backwards compatibility or use the new version.")
11+
if ! Puppet::Parser::Functions.autoloader.loaded?(:dig44)
12+
Puppet::Parser::Functions.autoloader.load(:dig44)
4413
end
45-
46-
unless path.is_a? Array
47-
raise(Puppet::ParseError, "dig(): second argument must be an array, " <<
48-
"given #{path.class.name}")
49-
end
50-
51-
value = path.reduce(data) { |h, k| (h.is_a?(Hash) || h.is_a?(Array)) ? h[k] : break }
52-
value.nil? ? default : value
14+
function_dig44(arguments)
5315
end
5416
end

lib/puppet/parser/functions/dig44.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#
2+
# dig44.rb
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:dig44, :type => :rvalue, :doc => <<-EOS
7+
DEPRECATED: This function has been replaced in puppet 4.5.0.
8+
9+
Looks up into a complex structure of arrays and hashes and returns nil
10+
or the default value if nothing was found.
11+
12+
Path is an array of keys to be looked up in data argument. The function
13+
will go down the structure and try to extract the required value.
14+
15+
$data = {
16+
'a' => {
17+
'b' => [
18+
'b1',
19+
'b2',
20+
'b3' ]}}
21+
22+
$value = dig44($data, ['a', 'b', '2'], 'not_found')
23+
=> $value = 'b3'
24+
25+
a -> first hash key
26+
b -> second hash key
27+
2 -> array index starting with 0
28+
29+
not_found -> (optional) will be returned if there is no value or the path
30+
did not match. Defaults to nil.
31+
32+
In addition to the required "path" argument, "dig44" accepts default
33+
argument. It will be returned if no value was found or a path component is
34+
missing. And the fourth argument can set a variable path separator.
35+
EOS
36+
) do |arguments|
37+
# Two arguments are required
38+
raise(Puppet::ParseError, "dig44(): Wrong number of arguments " +
39+
"given (#{arguments.size} for at least 2)") if arguments.size < 2
40+
41+
data, path, default = *arguments
42+
43+
if !(data.is_a?(Hash) || data.is_a?(Array))
44+
raise(Puppet::ParseError, "dig44(): first argument must be a hash or an array, " <<
45+
"given #{data.class.name}")
46+
end
47+
48+
unless path.is_a? Array
49+
raise(Puppet::ParseError, "dig44(): second argument must be an array, " <<
50+
"given #{path.class.name}")
51+
end
52+
53+
value = path.reduce(data) { |h, k| (h.is_a?(Hash) || h.is_a?(Array)) ? h[k] : break }
54+
value.nil? ? default : value
55+
end
56+
end

spec/functions/dig44_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'spec_helper'
2+
3+
describe 'dig44' do
4+
it "should exist" do
5+
expect(Puppet::Parser::Functions.function("dig44")).to eq("function_dig44")
6+
end
7+
8+
it "should raise a ParseError if there are less than 2 arguments" do
9+
expect { scope.function_dig44([]) }.to raise_error(Puppet::ParseError)
10+
end
11+
12+
it "should raise a ParseError if the first argument isn't a hash or array" do
13+
expect { scope.function_dig44(['bad', []]) }.to raise_error(Puppet::ParseError)
14+
end
15+
16+
it "should raise a ParseError if the second argument isn't an array" do
17+
expect { scope.function_dig44([{}, 'bad']) }.to raise_error(Puppet::ParseError)
18+
end
19+
20+
it "should return an empty hash when given empty parameters" do
21+
result = scope.function_dig44([{}, []])
22+
expect(result).to(eq({}))
23+
end
24+
25+
it "should return value when given simple hash" do
26+
result = scope.function_dig44([{"a" => "b"}, ["a"]])
27+
expect(result).to(eq("b"))
28+
end
29+
30+
it "should find hash values two levels deep" do
31+
result = scope.function_dig44([{"a" => {"b" => "c"}}, ["a", "b"]])
32+
expect(result).to(eq("c"))
33+
end
34+
35+
it "should return default value if nothing was found" do
36+
result = scope.function_dig44([{}, ["a", "b"], "d"])
37+
expect(result).to(eq("d"))
38+
end
39+
40+
it "should work on booleans as well as strings" do
41+
result = scope.function_dig44([{"a" => false}, ["a"]])
42+
expect(result).to(eq(false))
43+
end
44+
end

spec/functions/dig_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
require 'spec_helper'
22

33
describe 'dig' do
4-
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
5-
it { is_expected.to run.with_params('bad', []).and_raise_error(Puppet::ParseError) }
6-
it { is_expected.to run.with_params({}, 'bad').and_raise_error(Puppet::ParseError) }
74

8-
it { is_expected.to run.with_params({}, []).and_return({}) }
9-
it { is_expected.to run.with_params({"a" => "b"}, ["a"]).and_return("b") }
10-
it { is_expected.to run.with_params({"a" => {"b" => "c"}}, ["a", "b"]).and_return("c") }
11-
it { is_expected.to run.with_params({}, ["a", "b"], "d").and_return("d") }
12-
it { is_expected.to run.with_params({"a" => false}, ["a"]).and_return(false) }
5+
it "should exist" do
6+
expect(Puppet::Parser::Functions.function("dig")).to eq("function_dig")
7+
end
8+
9+
it "should give a deprecation warning when called" do
10+
scope.expects(: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.")
11+
scope.function_dig([{}, []])
12+
end
1313
end

0 commit comments

Comments
 (0)