-
Notifications
You must be signed in to change notification settings - Fork 7
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
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.', | ||||||
), | ||||||
|
||||||
'#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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
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, | ||||||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 } }, | ||
) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: