Created
May 16, 2018 19:45
-
-
Save mkmik/8964d8bab7a5d24f946a6d9f668d3b1e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// extensible takes a jsonnet value and if it's an object it recursively | |
// computes a version of the object where each of its fields is converted | |
// to the "+:" form. | |
local extensible(o) = if std.isObject(o) then { | |
[n]+: extensible(o[n]) | |
for n in std.objectFields(o) | |
} else o; | |
// merge takes two objects and merges them recursively. | |
local merge(a, b) = a + extensible(b); | |
local testCases = [ | |
[ | |
{ | |
a: 1, | |
}, | |
{ | |
b: 2, | |
}, | |
{ | |
a: 1, | |
b: 2, | |
}, | |
], | |
[ | |
{ | |
x: { | |
a: 1, | |
}, | |
}, | |
{ | |
x: { | |
b: 2, | |
}, | |
}, | |
{ | |
x: { | |
a: 1, | |
b: 2, | |
}, | |
}, | |
], | |
]; | |
local check = [ | |
std.assertEqual(merge(e[0], e[1]), e[2]) | |
for e in testCases | |
]; | |
check |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment