-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1363 from puppetlabs/CONT-1023-review-comments
(CONT-1023) - Moving nested_values function under stdlib namespace
- Loading branch information
Showing
4 changed files
with
29 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters