Skip to content

(CONT-1023) - Enhancing deferrable_epp to support nested hash #1359

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 2 commits into from
May 31, 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.any |$key, $value| { $value.is_a(Deferred) } {
if $variables.nested_values.any |$value| { $value.is_a(Deferred) } {
Deferred(
'inline_epp',
[find_template($template).file, $variables],
Expand Down
26 changes: 26 additions & 0 deletions lib/puppet/functions/nested_values.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

# This function will return list of Hash values, the return value will be Array
# NOTE : This function is expecting only Hash and return value will be Array
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two lines could be reworded to remove the repetition about the return value.

Copy link
Contributor Author

@Ramesh7 Ramesh7 May 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed here

#
# @example :
# $hash = {
# "key1" => "value1",
# "key2" => { "key2.1" => "value2.1"}
# }
# $hash.nested_values
#
# Output : ["value1", "value2.1"]
#
Puppet::Functions.create_function(:nested_values) do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happened to all new functions being namespaced??

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest namespacing it without creating a shim as this hasn't made it to a release yet.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damned! Yes, to all of this 😪

dispatch :nested_values do
param 'Hash', :options
return_type 'Array'
end

def nested_values(options)
options.each_with_object([]) do |(_k, v), values|
v.is_a?(Hash) ? values.concat(nested_values(v)) : (values << v)
end
end
end
15 changes: 15 additions & 0 deletions spec/functions/nested_values_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments should not have made it in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in #1363.

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']) }
it { is_expected.to run.with_params({ 'key1' => 'value1', 'key2' => { 'key1' => 'value21', 'key2' => 'value22' }, 'key3' => 'value3' }).and_return(['value1', 'value21', 'value22', 'value3']) }
it { is_expected.to run.with_params(2).and_raise_error(StandardError) }
it { is_expected.to run.with_params(nil).and_raise_error(StandardError) }
end