Skip to content

Allow options injection for to_yaml #1137

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 1 commit into from
Nov 16, 2020
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
13 changes: 10 additions & 3 deletions lib/puppet/functions/to_yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@
# @summary
# Convert a data structure and output it as YAML
#
# @example how to output YAML
# @example How to output YAML
# # output yaml to a file
# file { '/tmp/my.yaml':
# ensure => file,
# content => to_yaml($myhash),
# }
# @example Use options control the output format
# file { '/tmp/my.yaml':
# ensure => file,
# content => to_yaml($myhash, {indentation: 4})
Copy link
Collaborator

Choose a reason for hiding this comment

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

{indentation: 4} doesn't look like a valid puppet hash to me. Or am I missing something?

# }
Puppet::Functions.create_function(:to_yaml) do
# @param data
# @param options
#
# @return [String]
dispatch :to_yaml do
param 'Any', :data
optional_param 'Hash', :options
end

def to_yaml(data)
data.to_yaml
def to_yaml(data, options = {})
data.to_yaml(options)
end
end
2 changes: 2 additions & 0 deletions spec/functions/to_yaml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@

it { is_expected.to run.with_params('‰').and_return("--- \"‰\"\n") }
it { is_expected.to run.with_params('∇').and_return("--- \"∇\"\n") }

it { is_expected.to run.with_params({ 'foo' => { 'bar' => true, 'baz' => false } }, :indentation => 4).and_return("---\nfoo:\n bar: true\n baz: false\n") }
end