Skip to content

Loosen the restrictions of upcase and allow for recursion of the objects... #419

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
Mar 2, 2015
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
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ Takes a single string value as an argument. *Type*: rvalue

You can also use this with arrays. For example, `unique(["a","a","b","b","c","c"])` returns ["a","b","c"]. *Type*: rvalue

* `upcase`: Converts a string or an array of strings to uppercase. For example, `upcase("abcd")` returns 'ABCD'. *Type*: rvalue
* `upcase`: Converts an object, array or hash of objects that respond to upcase to uppercase. For example, `upcase("abcd")` returns 'ABCD'. *Type*: rvalue

* `uriescape`: Urlencodes a string or array of strings. Requires either a single string or an array as an input. *Type*: rvalue

Expand Down
10 changes: 5 additions & 5 deletions lib/puppet/parser/functions/upcase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ module Puppet::Parser::Functions
) do |arguments|

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

value = arguments[0]

unless value.is_a?(Array) || value.is_a?(String) || value.is_a?(Hash)
unless value.is_a?(Array) || value.is_a?(Hash) || value.respond_to?(:upcase)
raise(Puppet::ParseError, 'upcase(): Requires an ' +
'array, string or hash to work with')
'array, hash or object that responds to upcase in order to work')
end

if value.is_a?(Array)
# Numbers in Puppet are often string-encoded which is troublesome ...
Copy link
Contributor

Choose a reason for hiding this comment

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

The code to deal with the fact that numbers are often string-encoded seems to have been removed? Will this work with numbers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As long as it is passed as a String type it will work as it always has, that being said, with future parser it will fail if it is passed a Numeric type, but this was always going to be the case.

result = value.collect { |i| i.is_a?(String) ? i.upcase : i }
result = value.collect { |i| function_upcase([i]) }
elsif value.is_a?(Hash)
result = {}
value.each_pair do |k, v|
result.merge!({k.upcase => v.collect! { |p| p.upcase }})
result[function_upcase([k])] = function_upcase([v])
end
else
result = value.upcase
Expand Down
19 changes: 19 additions & 0 deletions spec/functions/upcase_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,23 @@ class AlsoString < String
scope.function_upcase([{'test' => %w(this that and other thing)}])
).to eq({'TEST' => %w(THIS THAT AND OTHER THING)})
end

if :test.respond_to?(:upcase)
it 'should accept hashes of symbols' do
expect(
scope.function_upcase([{:test => [:this, :that, :other]}])
).to eq({:TEST => [:THIS, :THAT, :OTHER]})
end
it 'should return upcase symbol' do
expect(
scope.function_upcase([:test])
).to eq(:TEST)
end
it 'should return mixed objects in upcease' do
expect(
scope.function_upcase([[:test, 'woot']])
).to eq([:TEST, 'WOOT'])

end
end
end