Skip to content

Commit f38c42a

Browse files
authored
Merge pull request #1363 from puppetlabs/CONT-1023-review-comments
(CONT-1023) - Moving nested_values function under stdlib namespace
2 parents 2c816ed + b225099 commit f38c42a

File tree

4 files changed

+29
-31
lines changed

4 files changed

+29
-31
lines changed

Diff for: functions/deferrable_epp.pp

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

Diff for: lib/puppet/functions/nested_values.rb

-26
This file was deleted.

Diff for: lib/puppet/functions/stdlib/nested_values.rb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
# @summary Get list of nested values from given hash
4+
# This function will return list of nested Hash values and returns list of values in form of Array
5+
#
6+
# @example Example Usage:
7+
# $hash = {
8+
# "key1" => "value1",
9+
# "key2" => { "key2.1" => "value2.1"},
10+
# "key3" => "value3"
11+
# }
12+
# $data = $hash.stdlib::nested_values
13+
# #Output : ["value1", "value2.1", "value3"]
14+
Puppet::Functions.create_function(:'stdlib::nested_values') do
15+
# @param hash A (nested) hash
16+
# @return All the values found in the input hash included those deeply nested.
17+
dispatch :nested_values do
18+
param 'Hash', :hash
19+
return_type 'Array'
20+
end
21+
22+
def nested_values(hash)
23+
hash.each_with_object([]) do |(_k, v), values|
24+
v.is_a?(Hash) ? values.concat(nested_values(v)) : (values << v)
25+
end
26+
end
27+
end

Diff for: spec/functions/nested_values_spec.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
require 'spec_helper'
44

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
5+
describe 'stdlib::nested_values' do
96
it { is_expected.to run.with_params({}).and_return([]) }
107
it { is_expected.to run.with_params({ 'key' => 'value' }).and_return(['value']) }
118
it { is_expected.to run.with_params({ 'key' => { 'key1' => 'value1', 'key2' => 'value2' } }).and_return(['value1', 'value2']) }

0 commit comments

Comments
 (0)