diff --git a/README.md b/README.md index d76204f..708fdd1 100644 --- a/README.md +++ b/README.md @@ -218,6 +218,26 @@ Result: } ``` +- Exclude list item by index: +```python +exclude_body = ['items.1'] +``` +Result: +```json +{ + "meta": { + "api_version": "1.0.0", + "issue_date": "20230926" + }, + "items": [ + { + "id": "1_abc", + "name": "chair" + } + ] +} +``` + - Exclude string value by regular expression ```python exclude_body = ['items.*.id:\d+'] diff --git a/tests/unit/excluder_test.py b/tests/unit/excluder_test.py index e0d1024..89df374 100644 --- a/tests/unit/excluder_test.py +++ b/tests/unit/excluder_test.py @@ -47,12 +47,37 @@ [r"data:\d+"], {"data": 1}, id='cutting out a non-string value by a regular expression' - ), pytest.param( + ), + pytest.param( {"data": "1"}, [r"data:\D*"], {"data": "1"}, id='cutting out a value by a regular expression without matches' ), + pytest.param( + {"data": [{"id": 123}, {"id": 124}]}, + ["data.1"], + {"data": [{"id": 123}]}, + id='cutting list item by index' + ), + pytest.param( + {"data": [{"id": 123}, {"id": 124}]}, + ["data.-1"], + {"data": [{"id": 123}, {"id": 124}]}, + id='cutting list item by negative index' + ), + pytest.param( + {"data": [{"id": 123}, {"id": 124}]}, + ["data.2"], + {"data": [{"id": 123}, {"id": 124}]}, + id='cutting list item by non-existent index' + ), + pytest.param( + {"data": [{"id": 123}, {"id": 124}]}, + ["data.id"], + {"data": [{"id": 123}, {"id": 124}]}, + id='cutting list item by not numeric index' + ), ]) def test_exclude(data: Dict[str, Any], excludes: List[str], expected_data: Dict[str, Any]): filter_data(excludes, data) diff --git a/vedro_replay/exclude.py b/vedro_replay/exclude.py index 4e3e399..a4009d6 100644 --- a/vedro_replay/exclude.py +++ b/vedro_replay/exclude.py @@ -24,6 +24,8 @@ def _exclude(self, data: Dict[str, Any], excluded_path: List[str]) -> None: data[current_path_part] = result[0] else: del data[current_path_part] + elif isinstance(data, list) and current_path_part.isdigit() and len(data) > int(current_path_part): + data.pop(int(current_path_part)) return if isinstance(data, list) and current_path_part == '*':