-
Notifications
You must be signed in to change notification settings - Fork 1
support skipping last node by index if parent node is a list #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,8 @@ | |
SNAPSHOT_LOGGER = logging.getLogger(__name__) | ||
SNAPSHOT_LOGGER.setLevel(logging.DEBUG if os.environ.get("DEBUG_SNAPSHOT") else logging.WARNING) | ||
|
||
_SKIP_PLACEHOLDER_VALUE = "$__to_be_skipped__$" | ||
|
||
|
||
class SnapshotMatchResult: | ||
def __init__(self, a: dict, b: dict, key: str = ""): | ||
|
@@ -218,7 +220,7 @@ def _assert_all( | |
self.skip_verification_paths = skip_verification_paths or [] | ||
if skip_verification_paths: | ||
SNAPSHOT_LOGGER.warning( | ||
f"Snapshot verification disabled for paths: {skip_verification_paths}" | ||
"Snapshot verification disabled for paths: %s", skip_verification_paths | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see https://docs.astral.sh/ruff/rules/logging-f-string/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Added #19 for this. |
||
) | ||
|
||
if self.update: | ||
|
@@ -306,7 +308,7 @@ def _transform(self, tmp: dict) -> dict: | |
try: | ||
replaced_tmp[key] = json.loads(dumped_value) | ||
except JSONDecodeError: | ||
SNAPSHOT_LOGGER.error(f"could not decode json-string:\n{tmp}") | ||
SNAPSHOT_LOGGER.error("could not decode json-string:\n%s", tmp) | ||
return {} | ||
|
||
return replaced_tmp | ||
|
@@ -365,6 +367,23 @@ def build_full_path_nodes(field_match: DatumInContext): | |
|
||
return full_path_nodes[::-1][1:] # reverse the list and remove Root()/$ | ||
|
||
def _remove_placeholder(_tmp): | ||
"""Traverse the object and remove any values in a list that would be equal to the placeholder""" | ||
if isinstance(_tmp, dict): | ||
for k, v in _tmp.items(): | ||
if isinstance(v, dict): | ||
_remove_placeholder(v) | ||
elif isinstance(v, list): | ||
_tmp[k] = _remove_placeholder(v) | ||
elif isinstance(_tmp, list): | ||
return [ | ||
_remove_placeholder(item) for item in _tmp if item != _SKIP_PLACEHOLDER_VALUE | ||
] | ||
|
||
return _tmp | ||
|
||
has_placeholder = False | ||
|
||
for path in self.skip_verification_paths: | ||
matches = parse(path).find(tmp) or [] | ||
for m in matches: | ||
|
@@ -378,7 +397,24 @@ def build_full_path_nodes(field_match: DatumInContext): | |
helper = helper.get(p, None) | ||
if not helper: | ||
continue | ||
|
||
if ( | ||
isinstance(helper, dict) and full_path[-1] in helper.keys() | ||
): # might have been deleted already | ||
del helper[full_path[-1]] | ||
elif isinstance(helper, list): | ||
try: | ||
index = int(full_path[-1].lstrip("[").rstrip("]")) | ||
# we need to set a placeholder value as the skips are based on index | ||
# if we are to pop the values, the next skip index will have shifted and won't be correct | ||
helper[index] = _SKIP_PLACEHOLDER_VALUE | ||
has_placeholder = True | ||
except ValueError: | ||
SNAPSHOT_LOGGER.warning( | ||
"Snapshot skip path '%s' was not applied as it was invalid for that snapshot", | ||
path, | ||
exc_info=SNAPSHOT_LOGGER.isEnabledFor(logging.DEBUG), | ||
) | ||
|
||
if has_placeholder: | ||
_remove_placeholder(tmp) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed to a more specific name and value here, for the sake of readability in code and in debugging.