From 0d9ce34564d72520407cb1dba9a785e2faec49b9 Mon Sep 17 00:00:00 2001 From: Joel Diaz Date: Tue, 28 Feb 2017 17:33:18 +0000 Subject: raise exceptions when walking through object path if we're given path a.b.c and the existing object is: a: b: - item1 raise an exception due to unexpected objects found while traversing the path (ie. b is a list, not a dict) also, add_entry assumes new dicts for each sub element when creating elements besides the final assignment value. doing something like a.b.c[0] = 12 where 'c' doesn't exist raises an exception add test cases to cover: access path that differs from existing object create new objects with an embedded list in the path create new object with a list at the end (define the end list in the passed in 'value' to avoid this exception) --- roles/lib_utils/library/yedit.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'roles/lib_utils/library') diff --git a/roles/lib_utils/library/yedit.py b/roles/lib_utils/library/yedit.py index b1d9d6869..a2ae6b4f6 100644 --- a/roles/lib_utils/library/yedit.py +++ b/roles/lib_utils/library/yedit.py @@ -311,7 +311,8 @@ class Yedit(object): continue elif data and not isinstance(data, dict): - return None + raise YeditException("Unexpected item type found while going through key " + + "path: {} (at key: {})".format(key, dict_key)) data[dict_key] = {} data = data[dict_key] @@ -320,7 +321,7 @@ class Yedit(object): int(arr_ind) <= len(data) - 1): data = data[int(arr_ind)] else: - return None + raise YeditException("Unexpected item type found while going through key path: {}".format(key)) if key == '': data = item @@ -334,6 +335,12 @@ class Yedit(object): elif key_indexes[-1][1] and isinstance(data, dict): data[key_indexes[-1][1]] = item + # didn't add/update to an existing list, nor add/update key to a dict + # so we must have been provided some syntax like a.b.c[] = "data" for a + # non-existent array + else: + raise YeditException("Error adding to object at path: {}".format(key)) + return data @staticmethod -- cgit v1.2.3