Skip to content

Commit

Permalink
Make diffs to be always tuples, but ignore the input sequence types
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Vasilyev committed Apr 22, 2019
1 parent f8cbadf commit be900bf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
17 changes: 12 additions & 5 deletions kopf/structs/diffs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ def resolve(d: Mapping, path: DiffPath):

def reduce_iter(d: Diff, path: DiffPath) -> Generator[DiffItem, None, None]:
for op, field, old, new in d:
if field[:len(path)] == path:
yield (op, field[len(path):], old, new)
if not path or tuple(field[:len(path)]) == tuple(path):
yield (op, tuple(field[len(path):]), old, new)


def reduce(d: Diff, path: DiffPath) -> Diff:
return type(d)(reduce_iter(d, path))
return tuple(reduce_iter(d, path))


def diff(a: Any, b: Any, path: DiffPath = ()) -> Generator[DiffItem, None, None]:
def diff_iter(a: Any, b: Any, path: DiffPath = ()) -> Generator[DiffItem, None, None]:
"""
Calculate the diff between two dicts.
Expand Down Expand Up @@ -60,6 +60,13 @@ def diff(a: Any, b: Any, path: DiffPath = ()) -> Generator[DiffItem, None, None]
for key in a_keys - b_keys:
yield ('remove', path+(key,), a[key], None)
for key in a_keys & b_keys:
yield from diff(a[key], b[key], path=path+(key,))
yield from diff_iter(a[key], b[key], path=path+(key,))
else:
yield ('change', path, a, b)


def diff(a: Any, b: Any, path: DiffPath = ()) -> Diff:
"""
Same as `diff`, but returns the whole tuple instead of iterator.
"""
return tuple(diff_iter(a, b, path=path))
2 changes: 1 addition & 1 deletion kopf/structs/lastseen.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def is_state_changed(body):
def get_state_diffs(body):
old = retreive_state(body)
new = get_state(body)
return old, new, list(diff(old, new))
return old, new, diff(old, new)


def retreive_state(body):
Expand Down

0 comments on commit be900bf

Please sign in to comment.