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

Add support for lists/arrays #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
57 changes: 31 additions & 26 deletions jsonmask/mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def apply_json_mask(data, json_mask, is_negated=False, depth=1, max_depth=None):
"""Take a data structure and compiled JSON mask, and remove unwanted data.

:data: The Python dictionary you want to prune
:data: The data you want to prune
:json_mask: The compiled jsonmask indicating which data to keep
and which to discard
:is_negated: If True, membership in the json_mask indicates removal
Expand All @@ -22,7 +22,7 @@ def apply_json_mask(data, json_mask, is_negated=False, depth=1, max_depth=None):
:max_depth: Integer that, if supplied, sets a maximum depth on the
supplied `json_mask`

:Returns: dict
:Returns: pruned data

"""

Expand All @@ -32,36 +32,41 @@ def apply_json_mask(data, json_mask, is_negated=False, depth=1, max_depth=None):
if isinstance(json_mask, string_types):
json_mask = parse_fields(json_mask)

allowed_data = {}
for key, subdata in data.items():

if should_include_variable(key, json_mask, is_negated=is_negated):

# 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:
allowed_data[key] = subdata
continue

allowed_data.setdefault(key, {})
allowed_data[key].update(
apply_json_mask(
if isinstance(data, list):
allowed_list = []
for item in data:
allowed_list.append(apply_json_mask(
item,
json_mask,
is_negated=is_negated,
depth=depth + 1,
max_depth=max_depth,
))
return allowed_list

if isinstance(data, dict):
allowed_data = {}
for key, subdata in data.items():
if should_include_variable(key, json_mask, is_negated=is_negated):
next_json_mask = json_mask.get(key, {})
# Dead ends in the mask indicate that want
# everything nested below this
if not next_json_mask:
allowed_data[key] = subdata
continue

allowed_data.setdefault(key, {})
allowed_data[key] = apply_json_mask(
subdata,
next_json_mask,
is_negated=is_negated,
depth=depth + 1,
max_depth=max_depth,
),
)
)

return allowed_data

return allowed_data
return data


def is_structure_wildcard(structure):
Expand Down