Skip to content

Commit 4120eb2

Browse files
authored
Merge pull request #1359 from puppetlabs/CONT-1023
(CONT-1023) - Enhancing deferrable_epp to support nested hash
2 parents 928ca48 + 40bcc4a commit 4120eb2

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-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.nested_values.any |$value| { $value.is_a(Deferred) } {
1010
Deferred(
1111
'inline_epp',
1212
[find_template($template).file, $variables],

lib/puppet/functions/nested_values.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
#
6+
# @example :
7+
# $hash = {
8+
# "key1" => "value1",
9+
# "key2" => { "key2.1" => "value2.1"}
10+
# }
11+
# $hash.nested_values
12+
#
13+
# Output : ["value1", "value2.1"]
14+
#
15+
Puppet::Functions.create_function(:nested_values) do
16+
dispatch :nested_values do
17+
param 'Hash', :options
18+
return_type 'Array'
19+
end
20+
21+
def nested_values(options)
22+
options.each_with_object([]) do |(_k, v), values|
23+
v.is_a?(Hash) ? values.concat(nested_values(v)) : (values << v)
24+
end
25+
end
26+
end

spec/functions/nested_values_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'nested_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({ 'key1' => 'value1', 'key2' => { 'key1' => 'value21', 'key2' => 'value22' }, 'key3' => 'value3' }).and_return(['value1', 'value21', 'value22', 'value3']) }
13+
it { is_expected.to run.with_params(2).and_raise_error(StandardError) }
14+
it { is_expected.to run.with_params(nil).and_raise_error(StandardError) }
15+
end

0 commit comments

Comments
 (0)