From e0a1b6af68917d69a2423d1060955fa1fa4121da Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 15 Jul 2021 14:55:31 +0200 Subject: [PATCH] arg_validator: make ArgValidatorDeprecated a subclass of ArgValidator ArgValidatorDeprecated is used as a nested validator, it also should subtype ArgValidator base class. Technially, in python you can do duck typing and get away with a different class that merely behaves sufficiently similar. But why? That way, we also can set default_value=ArgValidator.MISSING. As a result, we no longer need to special case ArgValidatorDeprecated at various places, because the default value will indicate that it should not be present. Co-authored-by: Till Maas Signed-off-by: Thomas Haller --- .../network_lsr/argument_validator.py | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/module_utils/network_lsr/argument_validator.py b/module_utils/network_lsr/argument_validator.py index 09a8ec0..38169d2 100644 --- a/module_utils/network_lsr/argument_validator.py +++ b/module_utils/network_lsr/argument_validator.py @@ -311,11 +311,22 @@ class ArgValidatorBool(ArgValidator): raise ValidationError(name, "must be an boolean but is '%s'" % (value)) -class ArgValidatorDeprecated: +class ArgValidatorDeprecated(ArgValidator): + """ + ArgValidatorDeprecated is only used as a marker to indicate that a setting is deprecated + by another setting. The validator that contains a deprecated setting is responsible for + processing this and the replacement setting needs to perform the validation. + """ + def __init__(self, name, deprecated_by): - self.name = name + ArgValidator.__init__(self, name, default_value=ArgValidator.MISSING) self.deprecated_by = deprecated_by + def _validate_impl(self, value, name): + raise MyError( + "Deprecated settings need to be validated by the replacement setting." + ) + class ArgValidatorDict(ArgValidator): def __init__( @@ -359,8 +370,6 @@ class ArgValidatorDict(ArgValidator): for (setting, validator) in self.nested.items(): if setting in seen_keys: continue - if isinstance(validator, ArgValidatorDeprecated): - continue if validator.required: raise ValidationError(name, "missing required key '%s'" % (setting)) if not self.all_missing_during_validate: @@ -1717,11 +1726,9 @@ class ArgValidator_DictConnection(ArgValidatorDict): for name in self.VALID_FIELDS: if name in result: continue - validator = self.nested[name] - if not isinstance(validator, ArgValidatorDeprecated): - value = validator.get_default_value() - if value is not ArgValidator.MISSING: - result[name] = value + value = self.nested[name].get_default_value() + if value is not ArgValidator.MISSING: + result[name] = value return result