Handle merging None into lists/dicts
Signed-off-by: martin f. krafft <madduck@madduck.net>
diff --git a/mergers/dict/recursive_policy_update.py b/mergers/dict/recursive_policy_update.py
index f3bcb3e..01dfccb 100644
--- a/mergers/dict/recursive_policy_update.py
+++ b/mergers/dict/recursive_policy_update.py
@@ -21,6 +21,9 @@
self._policy = policy
def merge(self, first, second):
+ if second is None:
+ return first
+
ret = first.copy()
for k,v in second.iteritems():
if k in ret:
diff --git a/mergers/dict/recursive_update.py b/mergers/dict/recursive_update.py
index ba94c1f..fc1d425 100644
--- a/mergers/dict/recursive_update.py
+++ b/mergers/dict/recursive_update.py
@@ -11,6 +11,9 @@
class DictRecursiveUpdate(BaseDictMerger):
def merge(self, first, second):
+ if second is None:
+ return first
+
ret = first.copy()
for k,v in second.iteritems():
if k in ret:
diff --git a/mergers/dict/tests/test_recursive_policy_update.py b/mergers/dict/tests/test_recursive_policy_update.py
index f44ada6..4b9486b 100644
--- a/mergers/dict/tests/test_recursive_policy_update.py
+++ b/mergers/dict/tests/test_recursive_policy_update.py
@@ -24,3 +24,8 @@
assert ret['one'][2] == 3
assert len(ret['two']['one']) == 4
assert ret['two']['one'][3] == 4
+
+ def test_merge_with_none(self):
+ first = {1:2,3:4}
+ ret = self.merger.merge(first, None)
+ assert ret == first
diff --git a/mergers/dict/tests/test_recursive_update.py b/mergers/dict/tests/test_recursive_update.py
index c1e8501..4410b68 100644
--- a/mergers/dict/tests/test_recursive_update.py
+++ b/mergers/dict/tests/test_recursive_update.py
@@ -43,3 +43,8 @@
assert len(ret['four'][i]) == 1
for k,v in ret['four'][i].iteritems():
assert k == v
+
+ def test_merge_with_none(self):
+ first = {1:2,3:4}
+ ret = self.merger.merge(first, None)
+ assert ret == first
diff --git a/mergers/dict/tests/test_update.py b/mergers/dict/tests/test_update.py
index 8e35c53..02702f7 100644
--- a/mergers/dict/tests/test_update.py
+++ b/mergers/dict/tests/test_update.py
@@ -20,3 +20,8 @@
assert len(ret) == 4
for k,v in ret.iteritems():
assert k == v
+
+ def test_merge_with_none(self):
+ first = {1:2,3:4}
+ ret = self.merger.merge(first, None)
+ assert ret == first
diff --git a/mergers/dict/update.py b/mergers/dict/update.py
index dd834f0..69e849d 100644
--- a/mergers/dict/update.py
+++ b/mergers/dict/update.py
@@ -11,6 +11,9 @@
class DictUpdate(BaseDictMerger):
def merge(self, first, second):
+ if second is None:
+ return first
+
ret = first.copy()
ret.update(second)
return ret
diff --git a/mergers/list/base.py b/mergers/list/base.py
index a7bbc17..ea73cdd 100644
--- a/mergers/list/base.py
+++ b/mergers/list/base.py
@@ -12,5 +12,7 @@
def merge(self, first, second):
first = [first] if not isinstance(first, list) else first[:]
+ if second is None:
+ return first
second = [second] if not isinstance(second, list) else second[:]
return self._combine(first, second)
diff --git a/mergers/list/extend.py b/mergers/list/extend.py
index f4acc35..5c44ac5 100644
--- a/mergers/list/extend.py
+++ b/mergers/list/extend.py
@@ -11,8 +11,9 @@
class ListExtend(BaseListMerger):
def _combine(self, first, second):
- if isinstance(second, list):
- first.extend(second)
- else:
- first.append(second)
+ if second is not None:
+ if isinstance(second, list):
+ first.extend(second)
+ else:
+ first.append(second)
return first
diff --git a/mergers/list/set.py b/mergers/list/set.py
index ddb0eb4..54770a3 100644
--- a/mergers/list/set.py
+++ b/mergers/list/set.py
@@ -11,7 +11,8 @@
class SetExtend(BaseListMerger):
def _combine(self, first, second):
- for i in second:
- if i not in first:
- first.append(i)
+ if second is not None:
+ for i in second:
+ if i not in first:
+ first.append(i)
return first
diff --git a/mergers/list/tests/test_extend.py b/mergers/list/tests/test_extend.py
index bdc40ca..7484e8e 100644
--- a/mergers/list/tests/test_extend.py
+++ b/mergers/list/tests/test_extend.py
@@ -64,3 +64,8 @@
l2 = [3,2,1]
target = l1 + l2
assert self._test_merge(l1, l2, target)
+
+ def test_merge_with_none(self):
+ first = [1,2,3]
+ ret = self.merger.merge(first, None)
+ assert ret == first
diff --git a/mergers/list/tests/test_set.py b/mergers/list/tests/test_set.py
index d3e8183..fa0e118 100644
--- a/mergers/list/tests/test_set.py
+++ b/mergers/list/tests/test_set.py
@@ -25,3 +25,8 @@
l2 = [3,2,1]
target = l1
assert self._test_merge(l1, l2, target)
+
+ def test_merge_with_none(self):
+ first = [1,2,3]
+ ret = self.merger.merge(first, None)
+ assert ret == first