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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 Puppet::Functions.create_function(:validate_legacy) do
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
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
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 dispatch :validate_legacy do
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 param 'Any', :scope
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 param 'Type', :target_type
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 param 'String', :function_name
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 param 'Any', :value
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 repeated_param 'Any', :args
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 end
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
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
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 dispatch :validate_legacy_s do
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 param 'Any', :scope
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 param 'String', :type_string
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 param 'String', :function_name
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 param 'Any', :value
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 repeated_param 'Any', :args
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 end
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 # Workaround PUP-4438 (fixed: https://github.com/puppetlabs/puppet/commit/e01c4dc924cd963ff6630008a5200fc6a2023b08#diff-
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 # c937cc584953271bb3d3b3c2cb141790R221) to support puppet < 4.1.0 and puppet < 3.8.1.
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 def call(scope, *args)
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 manipulated_args = [scope] + args
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 self.class.dispatcher.dispatch(self, scope, manipulated_args)
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 end
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 def validate_legacy_s(scope, type_string, *args)
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46 t = Puppet::Pops::Types::TypeParser.new.parse(type_string, scope)
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 validate_legacy(scope, t, *args)
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 end
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 def validate_legacy(scope, target_type, function_name, value, *prev_args)
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 if assert_type(target_type, value)
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52 if previous_validation(scope, function_name, value, *prev_args)
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 # Silently passes
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 else
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 Puppet.notice("Accepting previously invalid value for target type '#{target_type}'")
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56 end
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 else
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 inferred_type = Puppet::Pops::Types::TypeCalculator.infer_set(value)
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 error_msg = Puppet::Pops::Types::TypeMismatchDescriber.new.describe_mismatch("validate_legacy(#{function_name})", target_type, inferred_type)
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 if previous_validation(scope, function_name, value, *prev_args)
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 call_function('deprecation', 'validate_legacy', error_msg)
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 else
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63 call_function('fail', error_msg)
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64 end
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65 end
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66 end
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
67
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
68 def previous_validation(scope, function_name, value, *prev_args)
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69 # Call the previous validation function and catch any errors. Return true if no errors are thrown.
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
70
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
71 scope.send("function_#{function_name}".to_s, [value, *prev_args])
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
72 true
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
73 rescue Puppet::ParseError
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
74 false
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
75 end
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
76
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
77 def assert_type(type, value)
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
78 Puppet::Pops::Types::TypeCalculator.instance?(type, value)
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
79 end
c42fb28cff86 Update to a newer Python module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
80 end