Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / functions / validate_legacy.rb
1 # @summary
2 #   Validate a value against both the target_type (new) and the previous_validation function (old).
3 Puppet::Functions.create_function(:validate_legacy) do
4   # The function checks a value against both the target_type (new) and the previous_validation function (old).
5   # @param scope
6   #   The main value that will be passed to the method
7   # @param target_type
8   # @param function_name
9   # @param value
10   # @param args
11   #   Any additional values that are to be passed to the method
12   # @return
13   #   A boolean value (`true` or `false`) returned from the called function.
14   dispatch :validate_legacy do
15     param 'Any', :scope
16     param 'Type', :target_type
17     param 'String', :function_name
18     param 'Any', :value
19     repeated_param 'Any', :args
20   end
21
22   # @param scope
23   #   The main value that will be passed to the method
24   # @param type_string
25   # @param function_name
26   # @param value
27   # @param args Any additional values that are to be passed to the method
28   # @return Legacy validation method
29   #
30   dispatch :validate_legacy_s do
31     param 'Any', :scope
32     param 'String', :type_string
33     param 'String', :function_name
34     param 'Any', :value
35     repeated_param 'Any', :args
36   end
37
38   # Workaround PUP-4438 (fixed: https://github.com/puppetlabs/puppet/commit/e01c4dc924cd963ff6630008a5200fc6a2023b08#diff-
39   #   c937cc584953271bb3d3b3c2cb141790R221) to support puppet < 4.1.0 and puppet < 3.8.1.
40   def call(scope, *args)
41     manipulated_args = [scope] + args
42     self.class.dispatcher.dispatch(self, scope, manipulated_args)
43   end
44
45   def validate_legacy_s(scope, type_string, *args)
46     t = Puppet::Pops::Types::TypeParser.new.parse(type_string, scope)
47     validate_legacy(scope, t, *args)
48   end
49
50   def validate_legacy(scope, target_type, function_name, value, *prev_args)
51     if assert_type(target_type, value)
52       if previous_validation(scope, function_name, value, *prev_args)
53         # Silently passes
54       else
55         Puppet.notice("Accepting previously invalid value for target type '#{target_type}'")
56       end
57     else
58       inferred_type = Puppet::Pops::Types::TypeCalculator.infer_set(value)
59       error_msg = Puppet::Pops::Types::TypeMismatchDescriber.new.describe_mismatch("validate_legacy(#{function_name})", target_type, inferred_type)
60       if previous_validation(scope, function_name, value, *prev_args)
61         call_function('deprecation', 'validate_legacy', error_msg)
62       else
63         call_function('fail', error_msg)
64       end
65     end
66   end
67
68   def previous_validation(scope, function_name, value, *prev_args)
69     # Call the previous validation function and catch any errors. Return true if no errors are thrown.
70
71     scope.send("function_#{function_name}".to_s, [value, *prev_args])
72     true
73   rescue Puppet::ParseError
74     false
75   end
76
77   def assert_type(type, value)
78     Puppet::Pops::Types::TypeCalculator.instance?(type, value)
79   end
80 end