Skip to content

Commit 41991b2

Browse files
author
Cocker Koch
committed
(PUP-11123) make Function unwrap more tolerant
- as there is no Function unwrap_if_necessary() we make the Function unwrap() more tolerant and let it return a non-sensitive Value just like it is
1 parent a557077 commit 41991b2

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/puppet/functions/unwrap.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Unwraps a Sensitive value and returns the wrapped object.
2+
# Returns the Value itself, if it is not Sensitive.
23
#
34
# @example Usage of unwrap
45
#
@@ -28,17 +29,31 @@
2829
# @since 4.0.0
2930
#
3031
Puppet::Functions.create_function(:unwrap) do
31-
dispatch :unwrap do
32+
dispatch :from_sensitive do
3233
param 'Sensitive', :arg
3334
optional_block_param
3435
end
3536

36-
def unwrap(arg)
37+
dispatch :from_any do
38+
param 'Any', :arg
39+
optional_block_param
40+
end
41+
42+
def from_sensitive(arg)
3743
unwrapped = arg.unwrap
3844
if block_given?
3945
yield(unwrapped)
4046
else
4147
unwrapped
4248
end
4349
end
50+
51+
def from_any(arg)
52+
unwrapped = arg
53+
if block_given?
54+
yield(unwrapped)
55+
else
56+
unwrapped
57+
end
58+
end
4459
end

spec/unit/functions/unwrap_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
expect(eval_and_collect_notices(code)).to eq(['unwrapped value is 12345'])
1616
end
1717

18+
it 'just returns a non-sensitive value' do
19+
code = <<-CODE
20+
$non_sensitive = "12345"
21+
notice("value is still ${non_sensitive.unwrap}")
22+
CODE
23+
expect(eval_and_collect_notices(code)).to eq(['value is still 12345'])
24+
end
25+
1826
it 'unwraps a sensitive value when given a code block' do
1927
code = <<-CODE
2028
$sensitive = Sensitive.new("12345")

0 commit comments

Comments
 (0)