allow for lists, sets and dicts to default to None, now return empty type in post processing

remove defaults from inhertiable fieldattributes to allow for proper detection and override
pull/11767/head
Brian Coca 9 years ago
parent 3e13dfd7e8
commit 8aa732e0a4

@ -51,8 +51,8 @@ class Base:
_vars = FieldAttribute(isa='dict', default=dict()) _vars = FieldAttribute(isa='dict', default=dict())
# flags and misc. settings # flags and misc. settings
_environment = FieldAttribute(isa='list', default=[]) _environment = FieldAttribute(isa='list')
_no_log = FieldAttribute(isa='bool', default=False) _no_log = FieldAttribute(isa='bool')
def __init__(self): def __init__(self):
@ -292,7 +292,9 @@ class Base:
elif attribute.isa == 'bool': elif attribute.isa == 'bool':
value = boolean(value) value = boolean(value)
elif attribute.isa == 'list': elif attribute.isa == 'list':
if not isinstance(value, list): if value is None:
value = []
elif not isinstance(value, list):
value = [ value ] value = [ value ]
if attribute.listof is not None: if attribute.listof is not None:
for item in value: for item in value:
@ -302,12 +304,18 @@ class Base:
if item is None or item.strip() == "": if item is None or item.strip() == "":
raise AnsibleParserError("the field '%s' is required, and cannot have empty values" % (name,), obj=self.get_ds()) raise AnsibleParserError("the field '%s' is required, and cannot have empty values" % (name,), obj=self.get_ds())
elif attribute.isa == 'set': elif attribute.isa == 'set':
if not isinstance(value, (list, set)): if value is None:
value = [ value ] value = set()
if not isinstance(value, set): else:
value = set(value) if not isinstance(value, (list, set)):
elif attribute.isa == 'dict' and not isinstance(value, dict): value = [ value ]
raise TypeError("%s is not a dictionary" % value) if not isinstance(value, set):
value = set(value)
elif attribute.isa == 'dict':
if value is None:
value = dict()
elif not isinstance(value, dict):
raise TypeError("%s is not a dictionary" % value)
# and assign the massaged value back to the attribute field # and assign the massaged value back to the attribute field
setattr(self, name, value) setattr(self, name, value)

Loading…
Cancel
Save