Skip to content

Instantly share code, notes, and snippets.

@mkmik
Created May 16, 2018 19:45
Show Gist options
  • Save mkmik/8964d8bab7a5d24f946a6d9f668d3b1e to your computer and use it in GitHub Desktop.
Save mkmik/8964d8bab7a5d24f946a6d9f668d3b1e to your computer and use it in GitHub Desktop.
// 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