Skip to content

Commit

Permalink
Added support for field exemptions for std.prune
Browse files Browse the repository at this point in the history
  • Loading branch information
raymond-xu-gtg committed Aug 14, 2024
1 parent 913281d commit 4e030fe
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 4 additions & 4 deletions stdlib/std.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -1611,7 +1611,7 @@ limitations under the License.
local arr = std.split(f, '/');
std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),

prune(a)::
prune(a, ex=[])::
local isContent(b) =
if b == null then
false
Expand All @@ -1622,11 +1622,11 @@ limitations under the License.
else
true;
if std.isArray(a) then
[std.prune(x) for x in a if isContent($.prune(x))]
[$.prune(x, ex) for x in a if isContent($.prune(x, ex))]
else if std.isObject(a) then {
[x]: $.prune(a[x])
[x]: $.prune(a[x], ex)
for x in std.objectFields(a)
if isContent(std.prune(a[x]))
if std.member(ex, x) || isContent($.prune(a[x], ex))
} else
a,

Expand Down
2 changes: 2 additions & 0 deletions test_suite/stdlib.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,8 @@ std.assertEqual(std.prune([[], {}, null]), []) &&
std.assertEqual(std.prune({ a: [[], {}, null], b: { a: [], b: {}, c: null } }), {}) &&
std.assertEqual(std.prune([[[], {}, null], { a: [], b: {}, c: null }]), []) &&
std.assertEqual(std.prune({ a: [{ b: true }] }), { a: [{ b: true }] }) &&
std.assertEqual(std.prune({ a: [{ b: [] }] }, ['b']), { a: [{ b: [] }] }) &&


std.assertEqual(std.parseJson('"foo"'), 'foo') &&
std.assertEqual(std.parseJson('{}'), {}) &&
Expand Down

0 comments on commit 4e030fe

Please sign in to comment.