Suggest different variables to use if we want to tunnel both v4 and v6
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / upcase.rb
1 #
2 #  upcase.rb
3 #  Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085.
4 #
5 module Puppet::Parser::Functions
6   newfunction(:upcase, :type => :rvalue, :doc => <<-DOC
7     Converts a string or an array of strings to uppercase.
8
9     *Examples:*
10
11         upcase("abcd")
12
13     Will return:
14
15         ABCD
16
17     Note: from Puppet 6.0.0, the compatible function with the same name in Puppet core
18     will be used instead of this function.
19   DOC
20              ) do |arguments|
21
22     raise(Puppet::ParseError, "upcase(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1
23
24     value = arguments[0]
25
26     unless value.is_a?(Array) || value.is_a?(Hash) || value.respond_to?(:upcase)
27       raise(Puppet::ParseError, 'upcase(): Requires an array, hash or object that responds to upcase in order to work')
28     end
29
30     if value.is_a?(Array)
31       # Numbers in Puppet are often string-encoded which is troublesome ...
32       result = value.map { |i| function_upcase([i]) }
33     elsif value.is_a?(Hash)
34       result = {}
35       value.each_pair do |k, v|
36         result[function_upcase([k])] = function_upcase([v])
37       end
38     else
39       result = value.upcase
40     end
41
42     return result
43   end
44 end
45
46 # vim: set ts=2 sw=2 et :