From 4e030fe917ad56987f05ab148395329f720388e3 Mon Sep 17 00:00:00 2001 From: "Xu, He (Raymond)" Date: Wed, 14 Aug 2024 12:02:44 +1000 Subject: [PATCH] Added support for field exemptions for std.prune --- stdlib/std.jsonnet | 8 ++++---- test_suite/stdlib.jsonnet | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/stdlib/std.jsonnet b/stdlib/std.jsonnet index fccbe26d5..87cf16f6a 100644 --- a/stdlib/std.jsonnet +++ b/stdlib/std.jsonnet @@ -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 @@ -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, diff --git a/test_suite/stdlib.jsonnet b/test_suite/stdlib.jsonnet index fc1161ed5..3289a269b 100644 --- a/test_suite/stdlib.jsonnet +++ b/test_suite/stdlib.jsonnet @@ -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('{}'), {}) &&