Merge pull request #32129 from terminalmage/issue32044

Support multiple valid option types when performing type checks
This commit is contained in:
Mike Place 2016-03-24 15:16:29 -06:00
commit bdd7ea89d5

View file

@ -62,7 +62,7 @@ FLO_DIR = os.path.join(
'daemons', 'flo')
VALID_OPTS = {
'master': str,
'master': (str, list),
'master_port': int,
'master_type': str,
'master_finger': str,
@ -722,26 +722,30 @@ def _validate_opts(opts):
Check that all of the types of values passed into the config are
of the right types
'''
def format_multi_opt(valid_type):
try:
num_types = len(valid_type)
except TypeError:
# Bare type name won't have a length, return the name of the type
# passed.
return valid_type.__name__
else:
if num_types == 1:
return valid_type.__name__
elif num_types > 1:
ret = ', '.join(x.__name__ for x in valid_type[:-1])
ret += ' or ' + valid_type[-1].__name__
errors = []
err = ('Key {0} with value {1} has an invalid type of {2}, a {3} is '
err = ('Key \'{0}\' with value {1} has an invalid type of {2}, a {3} is '
'required for this value')
for key, val in opts.items():
if key in VALID_OPTS:
if isinstance(VALID_OPTS[key](), list):
if isinstance(val, VALID_OPTS[key]):
continue
else:
errors.append(
err.format(key, val, type(val).__name__, 'list')
)
if isinstance(VALID_OPTS[key](), dict):
if isinstance(val, VALID_OPTS[key]):
continue
else:
errors.append(
err.format(key, val, type(val).__name__, 'dict')
)
else:
if isinstance(val, VALID_OPTS[key]):
continue
if hasattr(VALID_OPTS[key], '__call__'):
try:
VALID_OPTS[key](val)
if isinstance(val, (list, dict)):
@ -758,17 +762,24 @@ def _validate_opts(opts):
VALID_OPTS[key].__name__
)
)
except ValueError:
except (TypeError, ValueError):
errors.append(
err.format(key, val, type(val).__name__, VALID_OPTS[key])
)
except TypeError:
errors.append(
err.format(key, val, type(val).__name__, VALID_OPTS[key])
err.format(key,
val,
type(val).__name__,
VALID_OPTS[key].__name__)
)
continue
errors.append(
err.format(key,
val,
type(val).__name__,
format_multi_opt(VALID_OPTS[key].__name__))
)
for error in errors:
log.warning(error)
log.debug(error)
if errors:
return False
return True