Mercurial > repos > other > Puppet
annotate modules/stdlib/lib/puppet/functions/validate_legacy.rb @ 424:4a2ee7e3b110
Update stdlib in case it fixed deprecation
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 09 Oct 2022 10:34:32 +0100 |
parents | d9352a684e62 |
children | adf6fe9bbc17 |
rev | line source |
---|---|
275
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
1 # @summary |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
2 # Validate a value against both the target_type (new) and the previous_validation function (old). |
272 | 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). | |
275
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
5 # @param scope |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
6 # The main value that will be passed to the method |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
7 # @param target_type |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
8 # @param function_name |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
9 # @param value |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
10 # @param args |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
11 # Any additional values that are to be passed to the method |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
12 # @return |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
13 # A boolean value (`true` or `false`) returned from the called function. |
272 | 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 | |
275
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
22 # @param scope |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
23 # The main value that will be passed to the method |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
24 # @param type_string |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
25 # @param function_name |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
26 # @param value |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
27 # @param args Any additional values that are to be passed to the method |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
28 # @return Legacy validation method |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
29 # |
272 | 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 |