Skip to content

Commit dea98f1

Browse files
committed
(CONT-1023) - Enhancing deferrable_epp to support nested hash
1 parent 2c61c1b commit dea98f1

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

functions/deferrable_epp.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# have to explicitly pass the entire scope to the client.
77
#
88
function stdlib::deferrable_epp(String $template, Hash $variables) >> Variant[String, Sensitive[String], Deferred] {
9-
if $variables.any |$key, $value| { $value.is_a(Deferred) } {
9+
if $variables.hash_values.any |$value| { $value.is_a(Deferred) } {
1010
Deferred(
1111
'inline_epp',
1212
[find_template($template).file, $variables],
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
# This function will return list of Hash values, the return value will be Array
4+
# NOTE : This function is expecting only Hash and return value will be Array
5+
Puppet::Functions.create_function(:'stdlib::hash_values') do
6+
dispatch :hash_values do
7+
param 'Hash', :options
8+
return_type 'Array'
9+
end
10+
11+
def hash_values(options)
12+
fetch_values(options)
13+
end
14+
15+
def fetch_values(opts = {})
16+
opts.each_with_object([]) do |(_k, v), values|
17+
v.is_a?(Hash) ? values.concat(fetch_values(v)) : (values << v)
18+
end
19+
end
20+
end

spec/functions/hash_values_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'stdlib::hash_values' do
6+
# please note that these tests are examples only
7+
# you will need to replace the params and return value
8+
# with your expectations
9+
it { is_expected.to run.with_params({}).and_return([]) }
10+
it { is_expected.to run.with_params({ 'key' => 'value' }).and_return(['value']) }
11+
it { is_expected.to run.with_params({ 'key' => { 'key1' => 'value1', 'key2' => 'value2' } }).and_return(['value1', 'value2']) }
12+
it { is_expected.to run.with_params(2).and_raise_error(StandardError) }
13+
it { is_expected.to run.with_params(nil).and_raise_error(StandardError) }
14+
end

0 commit comments

Comments
 (0)