forked from Invicton-Labs/terraform-null-deepmerge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
depth.tmpl
57 lines (55 loc) · 2.3 KB
/
depth.tmpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
locals {
mod0toparse = [
for map_idx in range(0, length(var.maps)):
[{
path = [],
value = var.maps[map_idx]
}]
]
%{ for depth in range(0, max_depth + 1) ~}
mod${depth} = [
for map_idx in range(0, length(var.maps)):
{
fields = concat([], [
for item in ${depth == 0 ? "local.mod${depth}toparse[map_idx]" : "local.mod${depth - 1}[map_idx].remaining"}:
[
for key in keys(item["value"]):
{
key = jsonencode(concat(item["path"], [key])),
path = concat(item["path"], [key]),
value = item["value"][key],
is_final = item["value"][key] == null || try(tolist(item["value"][key]), toset(item["value"][key]), tonumber(item["value"][key]), tobool(item["value"][key]), tostring(item["value"][key]), null) != null
}
]
]...)
remaining = concat([], [
for item in ${depth == 0 ? "local.mod${depth}toparse[map_idx]" : "local.mod${depth - 1}[map_idx].remaining"}:
[
for key in keys(item["value"]):
{
path = concat(item["path"], [key]),
value = item["value"][key]
}
if item["value"][key] != null && try(tolist(item["value"][key]), toset(item["value"][key]), tonumber(item["value"][key]), tobool(item["value"][key]), tostring(item["value"][key]), null) == null
]
]...)
}
]
%{ endfor ~}
modules = [
%{ for depth in range(0, max_depth + 1) ~}
local.mod${depth},
%{ endfor ~}
]
m${max_depth + 1} = {}
%{ for depth in range(max_depth, 0, -1) ~}
m${depth} = {
for field in lookup(local.merged_fields_by_depth, ${depth - 1}, {}):
field.key => {final_val = field.value, sub_val = {
for subfield in lookup(local.merged_fields_by_depth, ${depth}, {}):
subfield.path[length(subfield.path) - 1] => lookup(local.m${depth + 1}, subfield.key, subfield.value)
if slice(subfield.path, 0, length(subfield.path) - 1) == field.path
}}[field.is_final ? "final_val" : "sub_val"]
}
%{ endfor ~}
}