Skip to content

Improve parseyaml function #509

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

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 7 additions & 8 deletions lib/puppet/parser/functions/parseyaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@
#

module Puppet::Parser::Functions
newfunction(:parseyaml, :type => :rvalue, :doc => <<-EOS
newfunction(:parseyaml, :type => :rvalue, :arity => -2, :doc => <<-EOS
This function accepts YAML as a string and converts it into the correct
Puppet structure.

The optional second argument can be used to pass a default value that will
be returned if the parsing of YAML string have failed.
EOS
) do |arguments|

if (arguments.size != 1) then
raise(Puppet::ParseError, "parseyaml(): Wrong number of arguments "+
"given #{arguments.size} for 1")
end

require 'yaml'

YAML::load(arguments[0])

data = YAML::load(arguments[0])
data = arguments[1] unless data
data
Copy link
Contributor

Choose a reason for hiding this comment

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

L17-19 could just be YAML::load(arguments[0]) || arguments[1] .

Copy link

Choose a reason for hiding this comment

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

+1

end
end

Expand Down
7 changes: 5 additions & 2 deletions spec/functions/parseyaml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

describe 'parseyaml' do
it { is_expected.not_to eq(nil) }
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
it { is_expected.to run.with_params().and_raise_error(ArgumentError, /wrong number of arguments/i) }
it { is_expected.to run.with_params('["one", "two", "three"]').and_return(['one', 'two', 'three']) }
it { is_expected.to run.with_params('', 'default_value').and_return('default_value') }
it { is_expected.to run.with_params('', { 'a' => '1' }).and_return({ 'a' => '1' }) }
it { is_expected.to run.with_params("---\na: '1'\n").and_return({ 'a' => '1' }) }
it { is_expected.to run.with_params("---\na: '1'\n", 'default_value').and_return({ 'a' => '1' }) }
context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do
it { is_expected.to run.with_params('["one"').and_raise_error(Psych::SyntaxError) }
end
Expand Down