Skip to content

Commit

Permalink
Merge pull request #25 from Originate/ck/flatten_map
Browse files Browse the repository at this point in the history
feat: Add flatten_map module
  • Loading branch information
chrizkim authored Mar 10, 2023
2 parents af3e546 + 356471a commit c074a00
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
51 changes: 51 additions & 0 deletions terraform/flatten_map/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# flatten_map

This module flattens a nested map structure into a single-level map (depth of
one), with the keys being converted to represent their path, delimited by a
provided separator. If no separator is provided, a period ("`.`") will be used
by default.

> WARNING: Due to Terraform not supporting recursion, this module is bounded to
> a nesting depth of 20. Values beyond 20 layers deep will not be returned
> (though it isn't difficult to reverse-engineer this module and extend it to
> support a larger depth if really needed).
## Example

Calling the module with:

```
module "example" {
source = "github.com/Originate/terraform-modules//terraform/flatten_map?ref=v1"
in = {
a = "foo"
b = {
d = 11
}
c = {
e = true
f = {
g = {
h = ["bar", "baz"]
}
}
}
}
separator = "/"
}
```

will give an output of:

```
out = {
"a" = "foo"
"b/d" = 11
"c/e" = true
"c/f/g/h" = [
"bar",
"baz",
]
}
```
44 changes: 44 additions & 0 deletions terraform/flatten_map/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
output "out" {
value = merge(
{ for k, v in var.in : k => v if can(tobool(v)) || can(tolist(v)) || can(tonumber(v)) || can(tostring(v)) }, [for k1, v1 in var.in : merge(
{ for k, v in v1 : join(var.separator, [k1, k]) => v if can(tobool(v)) || can(tolist(v)) || can(tonumber(v)) || can(tostring(v)) }, [for k2, v2 in v1 : merge(
{ for k, v in v2 : join(var.separator, [k1, k2, k]) => v if can(tobool(v)) || can(tolist(v)) || can(tonumber(v)) || can(tostring(v)) }, [for k3, v3 in v2 : merge(
{ for k, v in v3 : join(var.separator, [k1, k2, k3, k]) => v if can(tobool(v)) || can(tolist(v)) || can(tonumber(v)) || can(tostring(v)) }, [for k4, v4 in v3 : merge(
{ for k, v in v4 : join(var.separator, [k1, k2, k3, k4, k]) => v if can(tobool(v)) || can(tolist(v)) || can(tonumber(v)) || can(tostring(v)) }, [for k5, v5 in v4 : merge(
{ for k, v in v5 : join(var.separator, [k1, k2, k3, k4, k5, k]) => v if can(tobool(v)) || can(tolist(v)) || can(tonumber(v)) || can(tostring(v)) }, [for k6, v6 in v5 : merge(
{ for k, v in v6 : join(var.separator, [k1, k2, k3, k4, k5, k6, k]) => v if can(tobool(v)) || can(tolist(v)) || can(tonumber(v)) || can(tostring(v)) }, [for k7, v7 in v6 : merge(
{ for k, v in v7 : join(var.separator, [k1, k2, k3, k4, k5, k6, k7, k]) => v if can(tobool(v)) || can(tolist(v)) || can(tonumber(v)) || can(tostring(v)) }, [for k8, v8 in v7 : merge(
{ for k, v in v8 : join(var.separator, [k1, k2, k3, k4, k5, k6, k7, k8, k]) => v if can(tobool(v)) || can(tolist(v)) || can(tonumber(v)) || can(tostring(v)) }, [for k9, v9 in v8 : merge(
{ for k, v in v9 : join(var.separator, [k1, k2, k3, k4, k5, k6, k7, k8, k9, k]) => v if can(tobool(v)) || can(tolist(v)) || can(tonumber(v)) || can(tostring(v)) }, [for k10, v10 in v9 : merge(
{ for k, v in v10 : join(var.separator, [k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k]) => v if can(tobool(v)) || can(tolist(v)) || can(tonumber(v)) || can(tostring(v)) }, [for k11, v11 in v10 : merge(
{ for k, v in v11 : join(var.separator, [k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k11, k]) => v if can(tobool(v)) || can(tolist(v)) || can(tonumber(v)) || can(tostring(v)) }, [for k12, v12 in v11 : merge(
{ for k, v in v12 : join(var.separator, [k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k11, k12, k]) => v if can(tobool(v)) || can(tolist(v)) || can(tonumber(v)) || can(tostring(v)) }, [for k13, v13 in v12 : merge(
{ for k, v in v13 : join(var.separator, [k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k11, k12, k13, k]) => v if can(tobool(v)) || can(tolist(v)) || can(tonumber(v)) || can(tostring(v)) }, [for k14, v14 in v13 : merge(
{ for k, v in v14 : join(var.separator, [k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k11, k12, k13, k14, k]) => v if can(tobool(v)) || can(tolist(v)) || can(tonumber(v)) || can(tostring(v)) }, [for k15, v15 in v14 : merge(
{ for k, v in v15 : join(var.separator, [k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k11, k12, k13, k14, k15, k]) => v if can(tobool(v)) || can(tolist(v)) || can(tonumber(v)) || can(tostring(v)) }, [for k16, v16 in v15 : merge(
{ for k, v in v16 : join(var.separator, [k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k11, k12, k13, k14, k15, k16, k]) => v if can(tobool(v)) || can(tolist(v)) || can(tonumber(v)) || can(tostring(v)) }, [for k17, v17 in v16 : merge(
{ for k, v in v17 : join(var.separator, [k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k11, k12, k13, k14, k15, k16, k17, k]) => v if can(tobool(v)) || can(tolist(v)) || can(tonumber(v)) || can(tostring(v)) }, [for k18, v18 in v17 : merge(
{ for k, v in v18 : join(var.separator, [k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k11, k12, k13, k14, k15, k16, k17, k18, k]) => v if can(tobool(v)) || can(tolist(v)) || can(tonumber(v)) || can(tostring(v)) }, [for k19, v19 in v18 : merge(
{ for k, v in v19 : join(var.separator, [k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k11, k12, k13, k14, k15, k16, k17, k18, k19, k]) => v if can(tobool(v)) || can(tolist(v)) || can(tonumber(v)) || can(tostring(v)) }, [for k20, v20 in v19 : merge(
) if !can(tobool(v20)) && !can(tolist(v20)) && !can(tonumber(v20)) && !can(tostring(v20))]...
) if !can(tobool(v19)) && !can(tolist(v19)) && !can(tonumber(v19)) && !can(tostring(v19))]...
) if !can(tobool(v18)) && !can(tolist(v18)) && !can(tonumber(v18)) && !can(tostring(v18))]...
) if !can(tobool(v17)) && !can(tolist(v17)) && !can(tonumber(v17)) && !can(tostring(v17))]...
) if !can(tobool(v16)) && !can(tolist(v16)) && !can(tonumber(v16)) && !can(tostring(v16))]...
) if !can(tobool(v15)) && !can(tolist(v15)) && !can(tonumber(v15)) && !can(tostring(v15))]...
) if !can(tobool(v14)) && !can(tolist(v14)) && !can(tonumber(v14)) && !can(tostring(v14))]...
) if !can(tobool(v13)) && !can(tolist(v13)) && !can(tonumber(v13)) && !can(tostring(v13))]...
) if !can(tobool(v12)) && !can(tolist(v12)) && !can(tonumber(v12)) && !can(tostring(v12))]...
) if !can(tobool(v11)) && !can(tolist(v11)) && !can(tonumber(v11)) && !can(tostring(v11))]...
) if !can(tobool(v10)) && !can(tolist(v10)) && !can(tonumber(v10)) && !can(tostring(v10))]...
) if !can(tobool(v9)) && !can(tolist(v9)) && !can(tonumber(v9)) && !can(tostring(v9))]...
) if !can(tobool(v8)) && !can(tolist(v8)) && !can(tonumber(v8)) && !can(tostring(v8))]...
) if !can(tobool(v7)) && !can(tolist(v7)) && !can(tonumber(v7)) && !can(tostring(v7))]...
) if !can(tobool(v6)) && !can(tolist(v6)) && !can(tonumber(v6)) && !can(tostring(v6))]...
) if !can(tobool(v5)) && !can(tolist(v5)) && !can(tonumber(v5)) && !can(tostring(v5))]...
) if !can(tobool(v4)) && !can(tolist(v4)) && !can(tonumber(v4)) && !can(tostring(v4))]...
) if !can(tobool(v3)) && !can(tolist(v3)) && !can(tonumber(v3)) && !can(tostring(v3))]...
) if !can(tobool(v2)) && !can(tolist(v2)) && !can(tonumber(v2)) && !can(tostring(v2))]...
) if !can(tobool(v1)) && !can(tolist(v1)) && !can(tonumber(v1)) && !can(tostring(v1))]...
)
}
10 changes: 10 additions & 0 deletions terraform/flatten_map/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
variable "in" {
description = "The map to flatten (will only flatten up to a depth of 20)"
type = any
}

variable "separator" {
description = "Separator to use to separate map path segments"
type = string
default = "."
}
6 changes: 6 additions & 0 deletions terraform/flatten_map/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
terraform {
required_version = ">= 1.0"

required_providers {
}
}

0 comments on commit c074a00

Please sign in to comment.