Skip to content

(CONT-1023) - Moving nested_values function under stdlib namespace #1363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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