Skip to content

Commit dcef77a

Browse files
authored
Merge pull request #632 from MiamiOH/master
Handle array values in join_keys_to_values function
2 parents b527d3b + 17a49ba commit dcef77a

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

README.markdown

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,9 @@ Joins an array into a string using a separator. For example, `join(['a','b','c']
772772

773773
#### `join_keys_to_values`
774774

775-
Joins each key of a hash to that key's corresponding value with a separator. Keys and values are cast to strings. The return value is an array in which each element is one joined key/value pair. For example, `join_keys_to_values({'a'=>1,'b'=>2}, " is ")` results in ["a is 1","b is 2"]. *Type*: rvalue.
775+
Joins each key of a hash to that key's corresponding value with a separator. Keys are cast to strings.
776+
If values are arrays, multiple keys are added for each element.
777+
The return value is an array in which each element is one joined key/value pair. For example, `join_keys_to_values({'a'=>1,'b'=>[2,3]}, " is ")` results in ["a is 1","b is 2","b is 3"]. *Type*: rvalue.
776778

777779
#### `keys`
778780

lib/puppet/parser/functions/join_keys_to_values.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
module Puppet::Parser::Functions
66
newfunction(:join_keys_to_values, :type => :rvalue, :doc => <<-EOS
77
This function joins each key of a hash to that key's corresponding value with a
8-
separator. Keys and values are cast to strings. The return value is an array in
8+
separator. Keys are cast to strings. If values are arrays, multiple keys
9+
are added for each element. The return value is an array in
910
which each element is one joined key/value pair.
1011
1112
*Examples:*
1213
1314
join_keys_to_values({'a'=>1,'b'=>2}, " is ")
1415
1516
Would result in: ["a is 1","b is 2"]
17+
18+
join_keys_to_values({'a'=>1,'b'=>[2,3]}, " is ")
19+
20+
Would result in: ["a is 1","b is 2","b is 3"]
1621
EOS
1722
) do |arguments|
1823

@@ -38,8 +43,12 @@ module Puppet::Parser::Functions
3843

3944
# Join the keys to their values.
4045
hash.map do |k,v|
41-
String(k) + separator + String(v)
42-
end
46+
if v.is_a?(Array)
47+
v.map { |va| String(k) + separator + String(va) }
48+
else
49+
String(k) + separator + String(v)
50+
end
51+
end.flatten
4352

4453
end
4554
end

spec/functions/join_keys_to_values_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@
1616
result = subject.call([{ 'key1' => 'value1', 'key2' => 'value2' }, ':'])
1717
expect(result.sort).to eq(['key1:value1', 'key2:value2'].sort)
1818
end
19+
it 'should run join_keys_to_values(<hash with array value>, " ") and return the proper array' do
20+
result = subject.call([{ 'key1' => 'value1', 'key2' => ['value2', 'value3'] }, ' '])
21+
expect(result.sort).to eq(['key1 value1', 'key2 value2', 'key2 value3'].sort)
22+
end
1923
end

0 commit comments

Comments
 (0)