1 | Puppet::Functions.create_function(:validate_legacy) do
|
---|
2 | # The function checks a value against both the target_type (new) and the previous_validation function (old).
|
---|
3 |
|
---|
4 | dispatch :validate_legacy do
|
---|
5 | param 'Any', :scope
|
---|
6 | param 'Type', :target_type
|
---|
7 | param 'String', :function_name
|
---|
8 | param 'Any', :value
|
---|
9 | repeated_param 'Any', :args
|
---|
10 | end
|
---|
11 |
|
---|
12 | dispatch :validate_legacy_s do
|
---|
13 | param 'Any', :scope
|
---|
14 | param 'String', :type_string
|
---|
15 | param 'String', :function_name
|
---|
16 | param 'Any', :value
|
---|
17 | repeated_param 'Any', :args
|
---|
18 | end
|
---|
19 |
|
---|
20 | # Workaround PUP-4438 (fixed: https://github.com/puppetlabs/puppet/commit/e01c4dc924cd963ff6630008a5200fc6a2023b08#diff-c937cc584953271bb3d3b3c2cb141790R221) to support puppet < 4.1.0 and puppet < 3.8.1.
|
---|
21 | def call(scope, *args)
|
---|
22 | manipulated_args = [scope] + args
|
---|
23 | self.class.dispatcher.dispatch(self, scope, manipulated_args)
|
---|
24 | end
|
---|
25 |
|
---|
26 | def validate_legacy_s(scope, type_string, *args)
|
---|
27 | t = Puppet::Pops::Types::TypeParser.new.parse(type_string, scope)
|
---|
28 | validate_legacy(scope, t, *args)
|
---|
29 | end
|
---|
30 |
|
---|
31 | def validate_legacy(scope, target_type, function_name, value, *prev_args)
|
---|
32 | if assert_type(target_type, value)
|
---|
33 | if previous_validation(scope, function_name, value, *prev_args)
|
---|
34 | # Silently passes
|
---|
35 | else
|
---|
36 | Puppet.notice("Accepting previously invalid value for target type '#{target_type}'")
|
---|
37 | end
|
---|
38 | else
|
---|
39 | inferred_type = Puppet::Pops::Types::TypeCalculator.infer_set(value)
|
---|
40 | error_msg = Puppet::Pops::Types::TypeMismatchDescriber.new.describe_mismatch("validate_legacy(#{function_name})", target_type, inferred_type)
|
---|
41 | if previous_validation(scope, function_name, value, *prev_args)
|
---|
42 | call_function('deprecation', 'validate_legacy', error_msg)
|
---|
43 | else
|
---|
44 | call_function('fail', error_msg)
|
---|
45 | end
|
---|
46 | end
|
---|
47 | end
|
---|
48 |
|
---|
49 | def previous_validation(scope, function_name, value, *prev_args)
|
---|
50 | # Call the previous validation function and catch any errors. Return true if no errors are thrown.
|
---|
51 | begin
|
---|
52 | scope.send("function_#{function_name}".to_s, [value, *prev_args])
|
---|
53 | true
|
---|
54 | rescue Puppet::ParseError
|
---|
55 | false
|
---|
56 | end
|
---|
57 | end
|
---|
58 |
|
---|
59 | def assert_type(type, value)
|
---|
60 | Puppet::Pops::Types::TypeCalculator.instance?(type, value)
|
---|
61 | end
|
---|
62 | end
|
---|