diff options
author | David Schmitt <david.schmitt@puppet.com> | 2016-09-06 11:18:48 +0100 |
---|---|---|
committer | David Schmitt <david.schmitt@puppet.com> | 2016-09-07 16:37:57 +0100 |
commit | f1edd2715a755573d7578839a3efe8473b79b5c5 (patch) | |
tree | e223708e8404e6e52d4c2fafef3322bfea7f2ee7 /lib | |
parent | 2b56ebd3e16112f8260519af0cd65a3d17fbfaeb (diff) |
(MODULES-3737) validate_legacy: refactoring
* validate_legacy now accepts undef values
* update the TypeMismatch message to include the original validate function name
* only notice, not warn, on newly allowed values
* changed previous_validation to function_name to avoid confusion with the function of the same name
* use deprecation() instead of warn(), when hitting a deprecated value
* prepare the tests and function for MODULES-3735
* rewrite validate_legacy tests to use new rspec-puppet
* move validate_re deprecation to puppet4 only
* adapt validate_re_spec
Diffstat (limited to 'lib')
-rw-r--r-- | lib/puppet/functions/validate_legacy.rb | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/lib/puppet/functions/validate_legacy.rb b/lib/puppet/functions/validate_legacy.rb index b1066e2..0ba6dd8 100644 --- a/lib/puppet/functions/validate_legacy.rb +++ b/lib/puppet/functions/validate_legacy.rb @@ -2,48 +2,49 @@ Puppet::Functions.create_function(:validate_legacy, Puppet::Functions::InternalF # The function checks a value against both the target_type (new) and the previous_validation function (old). dispatch :validate_legacy do + scope_param param 'Type', :target_type - param 'String', :previous_validation - param 'NotUndef', :value - optional_param 'Any', :args + param 'String', :function_name + param 'Any', :value + optional_repeated_param 'Any', :args end + dispatch :validate_legacy_s do scope_param param 'String', :type_string - param 'String', :previous_validation - param 'NotUndef', :value + param 'String', :function_name + param 'Any', :value optional_repeated_param 'Any', :args end def validate_legacy_s(scope, type_string, *args) t = Puppet::Pops::Types::TypeParser.new.parse(type_string, scope) - validate_legacy(t, *args) + validate_legacy(scope, t, *args) end - def validate_legacy(target_type, previous_validation, value, *prev_args) + def validate_legacy(scope, target_type, function_name, value, *prev_args) if assert_type(target_type, value) - if previous_validation(previous_validation, value, *prev_args) + if previous_validation(scope, function_name, value, *prev_args) # Silently passes else - Puppet.warn("Accepting previously invalid value for target_type '#{target_type}'") + Puppet.notice("Accepting previously invalid value for target type '#{target_type}'") end else caller_infos = caller.first.split(":") inferred_type = Puppet::Pops::Types::TypeCalculator.infer_set(value) - message = Puppet::Pops::Types::TypeMismatchDescriber.new.describe_mismatch(previous_validation, target_type, inferred_type) - error_msg = "#{message} : #{caller_infos[0]} : #{caller_infos[1]}" - if previous_validation(previous_validation, value, *prev_args) - Puppet.warn(error_msg) + error_msg = Puppet::Pops::Types::TypeMismatchDescriber.new.describe_mismatch("validate_legacy(#{function_name}) [#{caller_infos[0]}:#{caller_infos[1]}]", target_type, inferred_type) + if previous_validation(scope, function_name, value, *prev_args) + call_function('deprecation', 'validate_legacy', error_msg) else call_function('fail', error_msg) end end end - def previous_validation(previous_validation, value, *prev_args) + def previous_validation(scope, function_name, value, *prev_args) # Call the previous validation function and catch any errors. Return true if no errors are thrown. begin - call_function(previous_validation, value, *prev_args) + scope.send("function_#{function_name}".to_s, [value, *prev_args]) true rescue Puppet::ParseError false |