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

functions: Add builtins functions to remove null or empty values #288

Open
wants to merge 2 commits into
base: develop
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
28 changes: 28 additions & 0 deletions jmespath/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,34 @@ def _func_max(self, arg):
else:
return None

@signature({'types': ['object'], "variadic": True}, {'types': ['boolean']})
def _func_remove_null(self, obj, recursive):
if recursive:
return self._func_remove_recursive(obj, None)
else:
return self._func_remove(obj, None)

@signature({'types': ['object'], "variadic": True}, {'types': ['boolean']})
def _func_remove_empty(self, obj, recursive):
if recursive:
return self._func_remove_recursive(obj, None, '')
else:
return self._func_remove(obj, None, '')

def _func_remove(self, obj, *args):
return {k: v for k, v in obj.items() if not v in list(args)}

def _func_remove_recursive(self, obj, *args):
ret_dict = {}
for k in obj:
if not obj[k] in list(args):
ret_dict[k] = obj[k]

if isinstance(obj[k], dict):
ret_dict[k] = self._func_remove_recursive(obj[k], *args)

return ret_dict

@signature({"types": ["object"], "variadic": True})
def _func_merge(self, *arguments):
merged = {}
Expand Down
31 changes: 30 additions & 1 deletion tests/compliance/functions.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,38 @@
"empty_list": [],
"empty_hash": {},
"objects": {"foo": "bar", "bar": "baz"},
"objectsNull": {"foo": "bar", "bar": null},
"objectsEmpty": {"foo": "", "bar": null, "baz": "baz"},
"objectsEmptyNested": {"foo": "", "bar": null, "baz": {"foo": "", "bar": "bar", "foe": null}},
"objectsNullNested": {"foo": "bar", "bar": null, "baz": { "bar": null, "foo": "foo"}},
"null_key": null
},
"cases": [
{
"expression": "remove_empty(objectsEmptyNested, `false`)",
"result": {"baz": {"foo": "", "bar": "bar", "foe": null}}
},

{
"expression": "remove_empty(objectsEmptyNested, `true`)",
"result": {"baz": {"bar": "bar"}}
},
{
"expression": "remove_empty(objectsEmpty, `false`)",
"result": {"baz": "baz"}
},
{
"expression": "remove_null(objectsNull, `false`)",
"result": {"foo": "bar"}
},
{
"expression": "remove_null(objectsNullNested, `false`)",
"result": {"foo": "bar", "baz": { "bar": null, "foo": "foo"}}
},
{
"expression": "remove_null(objectsNullNested, `true`)",
"result": {"foo": "bar", "baz": {"foo": "foo"}}
},
{
"expression": "abs(foo)",
"result": 1
Expand Down Expand Up @@ -189,7 +218,7 @@
},
{
"expression": "length(@)",
"result": 12
"result": 16
},
{
"expression": "length(strings[0])",
Expand Down