Skip to content

Commit bbb29a4

Browse files
authored
Merge pull request #824 from vrtdev/feature/new_function_sprintf_hash
(MODULES-5680) Added new function sprintf_hash to allow using named references
2 parents ee12ea4 + 083be71 commit bbb29a4

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,6 +1681,24 @@ Returns the number of elements in a string, an array or a hash. This function wi
16811681
16821682
*Type*: rvalue.
16831683
1684+
#### `sprintf_hash`
1685+
1686+
Perform printf-style formatting with named references of text.
1687+
1688+
The first paameter is format string describing how the rest of the parameters in the hash
1689+
should be formatted. See the documentation for the `Kernel::sprintf` function in Ruby for
1690+
all the details.
1691+
1692+
*Example:*
1693+
1694+
```puppet
1695+
$output = sprintf_hash('String: %<foo>s / number converted to binary: %<number>b',
1696+
{ 'foo' => 'a string', 'number' => 5 })
1697+
# $output = 'String: a string / number converted to binary: 101'
1698+
```
1699+
1700+
*Type*: rvalue
1701+
16841702
#### `sort`
16851703
16861704
Sorts strings and arrays lexically.

lib/puppet/functions/sprintf_hash.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Uses sprintf with named references.
2+
#
3+
# The first parameter is format string describing how the rest of the parameters in the hash
4+
# should be formatted. See the documentation for the `Kernel::sprintf` function in Ruby for
5+
# all the details.
6+
#
7+
# In the given argument hash with parameters, all keys are converted to symbols so they work
8+
# with the `sprintf` function.
9+
#
10+
# @example Format a string and number
11+
# $output = sprintf_hash('String: %<foo>s / number converted to binary: %<number>b',
12+
# { 'foo' => 'a string', 'number' => 5 })
13+
# # $output = 'String: a string / number converted to binary: 101'
14+
#
15+
Puppet::Functions.create_function(:sprintf_hash) do
16+
# @param format The format to use.
17+
# @param arguments Hash with parameters.
18+
# @return The formatted string.
19+
dispatch :sprintf_hash do
20+
param 'String', :format
21+
param 'Hash', :arguments
22+
# Disabled for now. This gives issues on puppet 4.7.1.
23+
# return_type 'String'
24+
end
25+
26+
def sprintf_hash(format, arguments)
27+
Kernel.sprintf(format, Hash[arguments.map { |(k, v)| [k.to_sym, v] }])
28+
end
29+
end

spec/functions/sprintf_hash_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'spec_helper'
2+
3+
describe 'sprintf_hash' do
4+
it 'exists' do
5+
is_expected.not_to eq(nil)
6+
end
7+
8+
context 'validate param count' do
9+
it 'fails with no arguments' do
10+
is_expected.to run.with_params.and_raise_error(ArgumentError, %r{expects 2 arguments}i)
11+
end
12+
it 'fails with 1 argument' do
13+
is_expected.to run.with_params('').and_raise_error(ArgumentError, %r{expects 2 arguments}i)
14+
end
15+
it 'fails with too many arguments' do
16+
is_expected.to run.with_params('', '', '').and_raise_error(ArgumentError, %r{expects 2 arguments}i)
17+
end
18+
end
19+
20+
context 'validate param type' do
21+
it 'fails with wrong format type' do
22+
is_expected.to run.with_params(false, {}).and_raise_error(ArgumentError, %r{parameter 'format' expects a String value}i)
23+
end
24+
it 'fails with wrong arguments type' do
25+
is_expected.to run.with_params('', false).and_raise_error(ArgumentError, %r{parameter 'arguments' expects a Hash value}i)
26+
end
27+
end
28+
29+
it 'prints formats with name placeholders' do
30+
is_expected.to run.with_params('string %<foo>s and integer %<bar>b', 'foo' => '_foo_', 'bar' => 5).and_return('string _foo_ and integer 101')
31+
end
32+
end

0 commit comments

Comments
 (0)