Skip to content

Commit

Permalink
Fix list loading for non-object lists
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarteberg committed Jan 6, 2019
1 parent 679e8cd commit 96d6150
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
18 changes: 11 additions & 7 deletions confiddler/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,19 +246,23 @@ def set_defaults_and_validate(validator, properties, instance, schema):
yield error

def fill_in_default_array_items(validator, items_schema, instance, schema):
new_items = []
for item in instance:
if "default" in items_schema:
default = copy.deepcopy(items_schema["default"])
if isinstance(default, dict):
if "default" in items_schema and isinstance(items_schema["default"], Mapping):
new_items = []
for item in instance:
if not isinstance(item, Mapping):
new_items.append(item)
else:
default = copy.deepcopy(items_schema["default"])
default = _Dict(default)
if item == {}:
# FIXME: Instead of a simple bool, it would be better to specify
# WHICH properties in this dict were copied from the default value.
default.from_default = True
default.update(item)
new_items.append(default)

instance.clear()
instance.extend(new_items)
instance.clear()
instance.extend(new_items)

# Descend into array list
for error in validate_items(validator, items_schema, instance, schema):
Expand Down
22 changes: 22 additions & 0 deletions confiddler/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,28 @@ def test_missing_required_property_no_default():
load_config(f, schema)


def test_load_list():
"""
Make sure lists can be loaded properly
(e.g. that it isn't overwritten with the default, etc.)
"""
schema = copy.deepcopy(TEST_SCHEMA)
schema['properties']['mylist'] = {
'type': 'array',
'items': {'type': 'string'},
'default': []
}

data = {'mylist': ['a', 'b', 'c']}

f = StringIO()
yaml.dump(data, f)
f.seek(0)

cfg = load_config(f, schema)
assert cfg['mylist'] == list('abc')


def test_emit_defaults():
schema = copy.deepcopy(TEST_SCHEMA)
defaults = emit_defaults(schema)
Expand Down

0 comments on commit 96d6150

Please sign in to comment.