Skip to content

(MODULES-5680) Added new function sprintf_hash to allow using named references #824

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
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,24 @@ Returns the number of elements in a string, an array or a hash. This function wi

*Type*: rvalue.

#### `sprintf_hash`

Perform printf-style formatting with named references of text.

The first paameter is format string describing how the rest of the parameters in the hash
should be formatted. See the documentation for the `Kernel::sprintf` function in Ruby for
all the details.

*Example:*

```puppet
$output = sprintf_hash('String: %<foo>s / number converted to binary: %<number>b',
{ 'foo' => 'a string', 'number' => 5 })
# $output = 'String: a string / number converted to binary: 101'
```

*Type*: rvalue

#### `sort`

Sorts strings and arrays lexically.
Expand Down
29 changes: 29 additions & 0 deletions lib/puppet/functions/sprintf_hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Uses sprintf with named references.
#
# The first parameter is format string describing how the rest of the parameters in the hash
# should be formatted. See the documentation for the `Kernel::sprintf` function in Ruby for
# all the details.
#
# In the given argument hash with parameters, all keys are converted to symbols so they work
# with the `sprintf` function.
#
# @example Format a string and number
# $output = sprintf_hash('String: %<foo>s / number converted to binary: %<number>b',
# { 'foo' => 'a string', 'number' => 5 })
# # $output = 'String: a string / number converted to binary: 101'
#
Puppet::Functions.create_function(:sprintf_hash) do
# @param format The format to use.
# @param arguments Hash with parameters.
# @return The formatted string.
dispatch :sprintf_hash do
param 'String', :format
param 'Hash', :arguments
# Disabled for now. This gives issues on puppet 4.7.1.
# return_type 'String'
end

def sprintf_hash(format, arguments)
Kernel.sprintf(format, Hash[arguments.map { |(k, v)| [k.to_sym, v] }])
end
end
32 changes: 32 additions & 0 deletions spec/functions/sprintf_hash_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'

describe 'sprintf_hash' do
it 'exists' do
is_expected.not_to eq(nil)
end

context 'validate param count' do
it 'fails with no arguments' do
is_expected.to run.with_params.and_raise_error(ArgumentError, %r{expects 2 arguments}i)
end
it 'fails with 1 argument' do
is_expected.to run.with_params('').and_raise_error(ArgumentError, %r{expects 2 arguments}i)
end
it 'fails with too many arguments' do
is_expected.to run.with_params('', '', '').and_raise_error(ArgumentError, %r{expects 2 arguments}i)
end
end

context 'validate param type' do
it 'fails with wrong format type' do
is_expected.to run.with_params(false, {}).and_raise_error(ArgumentError, %r{parameter 'format' expects a String value}i)
end
it 'fails with wrong arguments type' do
is_expected.to run.with_params('', false).and_raise_error(ArgumentError, %r{parameter 'arguments' expects a Hash value}i)
end
end

it 'prints formats with name placeholders' do
is_expected.to run.with_params('string %<foo>s and integer %<bar>b', 'foo' => '_foo_', 'bar' => 5).and_return('string _foo_ and integer 101')
end
end