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

feat: add improved prune() function #29

Open
wants to merge 2 commits 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
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ in the future, but also provides a place for less general, yet useful utilities.
* [inspect](inspect.md)
* [jsonpath](jsonpath.md)
* [number](number.md)
* [reflect](reflect.md)
* [string](string.md)
* [url](url.md)
25 changes: 25 additions & 0 deletions docs/reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
permalink: /reflect/
---

# reflect

```jsonnet
local reflect = import "github.com/jsonnet-libs/xtd/reflect.libsonnet"
```

`reflect` implements helper functions for processing json data.

## Index

* [`fn prune(a, recurse=true, only_null=false)`](#fn-prune)

## Fields

### fn prune

```ts
prune(a, recurse=true, only_null=false)
```

`prune` works the same as `std.prune` but with the options to disable recursion and only pruning `null` values.
1 change: 1 addition & 0 deletions main.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';
inspect: (import './inspect.libsonnet'),
jsonpath: (import './jsonpath.libsonnet'),
number: (import './number.libsonnet'),
reflect: (import './reflect.libsonnet'),
string: (import './string.libsonnet'),
url: (import './url.libsonnet'),
}
47 changes: 47 additions & 0 deletions reflect.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';

{
'#': d.pkg(
name='reflect',
url='github.com/jsonnet-libs/xtd/reflect.libsonnet',
help='`reflect` implements helper functions for processing json data.',
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
help='`reflect` implements helper functions for processing json data.',
help='`reflect` implements helper functions for processing JSON data.',

),

'#prune':: d.fn(
'`prune` works the same as `std.prune` but with the options to disable recursion and only pruning `null` values.',
[
d.arg('a', d.T.any),
d.arg('recurse', d.T.bool, default=true),
d.arg('only_null', d.T.bool, default=false),
]
),
prune(a, recurse=true, only_null=false)::
local isContent(b) =
if b == null
then false
else if only_null
then true
else if std.isArray(b)
then std.length(b) > 0
else if std.isObject(b)
then std.length(b) > 0
else true;
local doRecursion(x) =
if recurse
then self.prune(x, only_null=only_null)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think it's more common to see that the recursive call uses the exact same arguments as the initial call because it explicitly demonstrates that the recursive call is gonna continue the same behavior.

Suggested change
then self.prune(x, only_null=only_null)
then self.prune(x, recurse, only_null)

else x;
if std.isArray(a)
then [
doRecursion(x)
for x in a
if isContent(doRecursion(x))
]
else if std.isObject(a)
then {
[x]: doRecursion(a[x])
for x in std.objectFields(a)
if isContent(doRecursion(a[x]))
}
else
a,
}
42 changes: 42 additions & 0 deletions test/reflect_test.jsonnet
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness, I think that all primitives need to exist in the test objects or at least zero-length arrays to demonstrate that the same std.prune behavior is preserved.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
local reflect = import '../reflect.libsonnet';
local test = import 'github.com/jsonnet-libs/testonnet/main.libsonnet';

local obj = {
a: {},
b: null,
c: {
d: {},
e: null,
},
};

test.new(std.thisFile)

+ test.case.new(
name='prune: vanilla',
test=test.expect.eq(
actual=reflect.prune(obj), // true/false
expected={},
)
)
+ test.case.new(
name='prune: no recurse',
test=test.expect.eq(
actual=reflect.prune(obj, recurse=false), // false/false
expected={ c: { d: {}, e: null } },
)
)
+ test.case.new(
name='prune: only null',
test=test.expect.eq(
actual=reflect.prune(obj, only_null=true), // true/true
expected={ a: {}, c: { d: {} } },
)
)
+ test.case.new(
name='prune: no recurse && only null',
test=test.expect.eq(
actual=reflect.prune(obj, recurse=false, only_null=true), // false/true
expected={ a: {}, c: { d: {}, e: null } },
)
)
Loading