diff --git a/lib/puppet/functions/unwrap.rb b/lib/puppet/functions/unwrap.rb index 3abe9e8a3cd..c91e4cd860a 100644 --- a/lib/puppet/functions/unwrap.rb +++ b/lib/puppet/functions/unwrap.rb @@ -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 # @@ -28,12 +29,17 @@ # @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) @@ -41,4 +47,13 @@ def unwrap(arg) unwrapped end end + + def from_any(arg) + unwrapped = arg + if block_given? + yield(unwrapped) + else + unwrapped + end + end end diff --git a/spec/unit/functions/unwrap_spec.rb b/spec/unit/functions/unwrap_spec.rb index 8547768953a..cce814c2645 100644 --- a/spec/unit/functions/unwrap_spec.rb +++ b/spec/unit/functions/unwrap_spec.rb @@ -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")