ansble-terraform-jsonnet/lib/utils.libsonnet

33 lines
1.2 KiB
Plaintext

{
# adopted from stdlib source to handle array merges
# https://github.com/google/jsonnet/blob/4e67da2c015bb316158d3e52a47376b38a29a4ef/stdlib/std.jsonnet#L1473
merge (target, patch)::
if std.isObject(patch) then
local target_object =
if std.isObject(target) then target else {};
local target_fields =
if std.isObject(target_object) then std.objectFields(target_object) else [];
local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];
local both_fields = std.setUnion(target_fields, std.objectFields(patch));
{
[k]:
if !std.objectHas(patch, k) then
target_object[k]
else if !std.objectHas(target_object, k) then
$.merge(null, patch[k])
else
$.merge(target_object[k], patch[k])
for k in std.setDiff(both_fields, null_fields)
}
else if std.isArray(patch) then
if std.isArray(target) && target != [null] then
target + patch
else
patch
else
patch
}