Skip to content
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

CATU-01 - Add suport to list #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions jsonmask/mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,27 @@ def apply_json_mask(data, json_mask, is_negated=False, depth=1, max_depth=None):
for key, subdata in data.items():

if should_include_variable(key, json_mask, is_negated=is_negated):
next_json_mask = json_mask.get(key, {})

if isinstance(subdata, list):
allowed_data[key] = [
apply_json_mask(
entry,
next_json_mask,
is_negated=is_negated,
depth=depth + 1,
max_depth=max_depth,
)
if isinstance(entry, dict)
else entry
for entry in subdata
]
continue
# Terminal data
if not isinstance(subdata, dict):
allowed_data[key] = subdata
continue

next_json_mask = json_mask.get(key, {})

# Dead ends in the mask indicate that want
# everything nested below this
if not next_json_mask:
Expand Down
3 changes: 3 additions & 0 deletions jsonmask/tests/test_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def test_apply_json_mask():
({'a': {'b': 1}, 'b': {'asdf': 2}}, 'a,b/c', {'a': {'b': 1}, 'b': {}}, {'b': {'asdf': 2}},),
({'a': {'b': 1}, 'b': {'asdf': 2}}, 'a,b/asdf', ORIGINAL, {'b': {}},),
({'a': {'b': 1}, 'b': {'asdf': 2}}, 'a,c/c', {'a': {'b': 1}}, {'b': {'asdf': 2}},),
({'a': [1,2,3]}, 'a', ORIGINAL, EMPTY,),
({'a': [{'b': 1, 'c': [1,2,3]}]}, 'a(b)', {'a': [{'b':1}]}, {'a': [{'c': [1,2,3]}]},),
({'a': [{'b': 1, 'c': [{'d': 1}, {'d':2, 'e': 3}]}]}, 'a(c(e))', {'a': [{'c':[{},{'e':3}]}]}, {'a': [{'b':1, 'c': [{'d':1}, {'d':2}]}]},),
]

for index, (data, _mask, expected_result, expected_result_when_negated,) in enumerate(tests, start=1):
Expand Down