Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/puppet/functions/unwrap.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Unwraps a Sensitive value and returns the wrapped object.
# Returns the Value itself, if it is not Sensitive.
#
# @example Usage of unwrap
#
Expand Down Expand Up @@ -28,17 +29,31 @@
# @since 4.0.0
#
Puppet::Functions.create_function(:unwrap) do
dispatch :unwrap do
dispatch :from_sensitive do
param 'Sensitive', :arg
optional_block_param
end

def unwrap(arg)
dispatch :from_any do
param 'Any', :arg
optional_block_param
end

def from_sensitive(arg)
unwrapped = arg.unwrap
if block_given?
yield(unwrapped)
else
unwrapped
end
end

def from_any(arg)
unwrapped = arg
if block_given?
yield(unwrapped)
else
unwrapped
end
end
end
8 changes: 8 additions & 0 deletions spec/unit/functions/unwrap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
expect(eval_and_collect_notices(code)).to eq(['unwrapped value is 12345'])
end

it 'just returns a non-sensitive value' do
code = <<-CODE
$non_sensitive = "12345"
notice("value is still ${non_sensitive.unwrap}")
CODE
expect(eval_and_collect_notices(code)).to eq(['value is still 12345'])
end

it 'unwraps a sensitive value when given a code block' do
code = <<-CODE
$sensitive = Sensitive.new("12345")
Expand Down