Skip to content

Commit

Permalink
Merge pull request #1363 from puppetlabs/CONT-1023-review-comments
Browse files Browse the repository at this point in the history
(CONT-1023) - Moving nested_values function under stdlib namespace
  • Loading branch information
alexjfisher committed Jun 1, 2023
2 parents 2c816ed + b225099 commit f38c42a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 31 deletions.
2 changes: 1 addition & 1 deletion functions/deferrable_epp.pp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# have to explicitly pass the entire scope to the client.
#
function stdlib::deferrable_epp(String $template, Hash $variables) >> Variant[String, Sensitive[String], Deferred] {
if $variables.nested_values.any |$value| { $value.is_a(Deferred) } {
if $variables.stdlib::nested_values.any |$value| { $value.is_a(Deferred) } {
Deferred(
'inline_epp',
[find_template($template).file, $variables],
Expand Down
26 changes: 0 additions & 26 deletions lib/puppet/functions/nested_values.rb

This file was deleted.

27 changes: 27 additions & 0 deletions lib/puppet/functions/stdlib/nested_values.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

# @summary Get list of nested values from given hash
# This function will return list of nested Hash values and returns list of values in form of Array
#
# @example Example Usage:
# $hash = {
# "key1" => "value1",
# "key2" => { "key2.1" => "value2.1"},
# "key3" => "value3"
# }
# $data = $hash.stdlib::nested_values
# #Output : ["value1", "value2.1", "value3"]
Puppet::Functions.create_function(:'stdlib::nested_values') do
# @param hash A (nested) hash
# @return All the values found in the input hash included those deeply nested.
dispatch :nested_values do
param 'Hash', :hash
return_type 'Array'
end

def nested_values(hash)
hash.each_with_object([]) do |(_k, v), values|
v.is_a?(Hash) ? values.concat(nested_values(v)) : (values << v)
end
end
end
5 changes: 1 addition & 4 deletions spec/functions/nested_values_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

require 'spec_helper'

describe 'nested_values' do
# please note that these tests are examples only
# you will need to replace the params and return value
# with your expectations
describe 'stdlib::nested_values' do
it { is_expected.to run.with_params({}).and_return([]) }
it { is_expected.to run.with_params({ 'key' => 'value' }).and_return(['value']) }
it { is_expected.to run.with_params({ 'key' => { 'key1' => 'value1', 'key2' => 'value2' } }).and_return(['value1', 'value2']) }
Expand Down

0 comments on commit f38c42a

Please sign in to comment.