diff options
author | Thom May <thom@may.lt> | 2018-03-28 10:23:59 +0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-28 10:23:59 +0700 |
commit | 1775822934b6f42791cf46454027a3a17f14e726 (patch) | |
tree | 0581980aa9e0087de1ed1eeacce51deacf69edaf /lib | |
parent | 7a6bf1531f91d121a21493924905cde37592a48b (diff) | |
parent | 2dc3fde7cde3779511108cff212f8f4bf32a33ce (diff) | |
download | chef-1775822934b6f42791cf46454027a3a17f14e726.tar.gz |
Merge pull request #7037 from chef/lcg/nil-sets-default
Setting nil to properties with implicit nil sets default value
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef/mixin/params_validate.rb | 6 | ||||
-rw-r--r-- | lib/chef/property.rb | 21 |
2 files changed, 13 insertions, 14 deletions
diff --git a/lib/chef/mixin/params_validate.rb b/lib/chef/mixin/params_validate.rb index c955dd3b12..4ed4b3ab3b 100644 --- a/lib/chef/mixin/params_validate.rb +++ b/lib/chef/mixin/params_validate.rb @@ -131,10 +131,6 @@ class Chef private - def explicitly_allows_nil?(key, validation) - validation.has_key?(:is) && _pv_is({ key => nil }, key, validation[:is], raise_error: false) - end - def _validation_message(key, default) @validation_message.has_key?(key) ? @validation_message[key] : default end @@ -407,7 +403,7 @@ class Chef # x nil #=> invalid # ``` # - def _pv_is(opts, key, to_be, raise_error: true) + def _pv_is(opts, key, to_be) return true if !opts.has_key?(key.to_s) && !opts.has_key?(key.to_sym) value = _pv_opts_lookup(opts, key) to_be = [ to_be ].flatten(1) diff --git a/lib/chef/property.rb b/lib/chef/property.rb index b38ec24de6..3ad28fad10 100644 --- a/lib/chef/property.rb +++ b/lib/chef/property.rb @@ -367,7 +367,7 @@ class Chef # Otherwise, we have to validate it now. value = input_to_stored_value(resource, default, is_default: true) end - value = stored_value_to_output(resource, value, is_default: true) + value = stored_value_to_output(resource, value) # If the value is mutable (non-frozen), we set it on the instance # so that people can mutate it. (All constant default values are @@ -600,7 +600,9 @@ class Chef # @api private def explicitly_accepts_nil?(resource) options.has_key?(:coerce) || - (options.has_key?(:is) && resource.send(:_pv_is, { name => nil }, name, options[:is], raise_error: false)) + (options.has_key?(:is) && Chef::Mixin::ParamsValidate.send(:_pv_is, { name => nil }, name, options[:is])) + rescue Chef::Exceptions::ValidationFailed, Chef::Exceptions::CannotValidateStaticallyError + false end # @api private @@ -676,25 +678,26 @@ class Chef end def input_to_stored_value(resource, value, is_default: false) + if value.nil? && !is_default && !explicitly_accepts_nil?(resource) + value = default + end unless value.is_a?(DelayedEvaluator) - value = coerce_and_validate(resource, value, is_default: is_default) + value = coerce_and_validate(resource, value) end value end - def stored_value_to_output(resource, value, is_default: false) + def stored_value_to_output(resource, value) # Crack open lazy values before giving the result to the user if value.is_a?(DelayedEvaluator) value = exec_in_resource(resource, value) - value = coerce_and_validate(resource, value, is_default: is_default) + value = coerce_and_validate(resource, value) end value end - # Coerces and validates the value. If the value is a default, it will warn - # the user that invalid defaults are bad mmkay, and return it as if it were - # valid. - def coerce_and_validate(resource, value, is_default: false) + # Coerces and validates the value. + def coerce_and_validate(resource, value) result = coerce(resource, value) validate(resource, result) |