Skip to content

Commit

Permalink
Preserve keys that are only in original buildconfig, except those nes…
Browse files Browse the repository at this point in the history
…ted in 'strategy'
  • Loading branch information
bkabrda committed Oct 14, 2015
1 parent da5829a commit 9060ad0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
17 changes: 11 additions & 6 deletions osbs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,27 @@ def graceful_chain_get(d, *args):
return t


def buildconfig_update(orig, new):
def buildconfig_update(orig, new, remove_nonexistent_keys=False):
"""Performs update of given `orig` BuildConfig with values from `new` BuildConfig.
Both BuildConfigs have to be represented as `dict`s.
This function:
- adds all key/value pairs to `orig` from `new` that are missing
- replaces values in `orig` for keys that are in both
- removes key/value pairs from `orig` for keys that are not in `new`
- removes key/value pairs from `orig` for keys that are not in `new`,
but only in dicts nested inside `strategy` key
(see https://github.com/projectatomic/osbs-client/pull/273#issuecomment-148038314)
"""
if isinstance(orig, dict) and isinstance(new, dict):
missing = set(orig.keys()) - set(new.keys())
for k in missing:
orig.pop(k)
if remove_nonexistent_keys:
missing = set(orig.keys()) - set(new.keys())
for k in missing:
orig.pop(k)
for k, v in new.items():
if k == 'strategy':
remove_nonexistent_keys = True
if isinstance(orig.get(k, None), dict) and isinstance(v, dict):
buildconfig_update(orig[k], v)
buildconfig_update(orig[k], v, remove_nonexistent_keys)
else:
orig[k] = v

Expand Down
6 changes: 3 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@


def test_buildconfig_update():
x = {'a': 'a', 'b': {'b1': 'B1', 'b2': 'B2'}, 'd': 'D'}
y = {'a': 'A', 'b': {'b1': 'newB1', 'b3': 'B3'}, 'c': 'C'}
x = {'a': 'a', 'strategy': {'b1': 'B1', 'b2': 'B2', 'b11': {'x': 'y'}}, 'd': 'D'}
y = {'a': 'A', 'strategy': {'b1': 'newB1', 'b3': 'B3', 'b11': {}}, 'c': 'C'}
buildconfig_update(x, y)
assert x == {'a': 'A', 'b': {'b1': 'newB1', 'b3': 'B3'}, 'c': 'C'}
assert x == {'a': 'A', 'strategy': {'b1': 'newB1', 'b3': 'B3', 'b11': {}}, 'c': 'C', 'd': 'D'}


@pytest.mark.parametrize(('uri', 'humanish'), [
Expand Down

0 comments on commit 9060ad0

Please sign in to comment.