terramate-io / terramate

Terramate CLI is an open-source Infrastructure as Code (IaC) Orchestration and Code Generation tool for Terraform, OpenTofu and Terragrunt.
https://terramate.io
Mozilla Public License 2.0
3.12k stars 86 forks source link

feat: for-expr with variables in generate_hcl.content #1714

Closed i4ki closed 1 month ago

i4ki commented 1 month ago

What this PR does / why we need it:

This change makes it possible to use Terramate variables in [for k, v in collection : ... if cond] and {for k, v in collection : ... if cond} when collection and cond does not depend on unknowns. In other words, the cases below are now possible:

globals {
  numbers = [1, 2, 3]
  strings = ["terramate", "is", "fun"]
  partials_config = {
    aws = "aws.vpc.id", 
    azre = "azure.thing"
  }
}

generate_hcl "example.tf" {
  content {
    square_numbers = [for n in global.numbers : n*n]
    upper_strings  = [for s in global.strings : tm_upper(s)]
    config = {for k, v in global.partials_config : k => tm_hcl_expression(v)}
  }
}

which generates the file example.tf below:

cat example.tf
// TERRAMATE: GENERATED AUTOMATICALLY DO NOT EDIT

config = {
  aws  = aws.vpc.id
  azre = azure.thing
}
square_numbers = [
  1,
  4,
  9,
]
upper_strings = [
  "TERRAMATE",
  "IS",
  "FUN",
]

Which issue(s) this PR fixes:

Fixes #1419 Fixes #1689 Fixes #1676

Special notes for your reviewer:

Does this PR introduce a user-facing change?

yes, introduces a feature.
github-actions[bot] commented 1 month ago
metric: time/op
CloudReadLines-4: old 987µs ± 4%: new 1007µs ± 3%: delta: 2.11%
CloudReadLine-4: old 6.99ms ± 1%: new 7.06ms ± 1%: delta: 1.05%
ListFiles-4: old 53.2µs ± 2%: new 53.3µs ± 1%: delta: 0.00%
Generate-4: old 2.53s ± 2%: new 2.53s ± 1%: delta: 0.00%
GenerateRegex-4: old 1.75s ± 1%: new 1.72s ± 1%: delta: -1.54%
TokensForExpressionComplex-4: old 1.26ms ± 0%: new 1.25ms ± 0%: delta: -1.02%
TokensForExpressionPlainStringNoNewline-4: old 893ns ± 1%: new 818ns ± 1%: delta: -8.39%
TokensForExpressionStringWith100Newlines-4: old 21.9µs ± 0%: new 21.8µs ± 0%: delta: -0.34%
TokensForExpressionObjectWith100KeysWithNumberValues-4: old 1.45ms ± 0%: new 1.44ms ± 0%: delta: -0.99%
TokensForExpression-4: old 1.26ms ± 0%: new 1.25ms ± 0%: delta: -1.08%
PartialEvalComplex-4: old 434µs ± 0%: new 578µs ± 1%: delta: 33.08%
PartialEvalSmallString-4: old 3.63µs ± 1%: new 3.61µs ± 0%: delta: -0.45%
PartialEvalHugeString-4: old 1.88ms ± 1%: new 1.88ms ± 0%: delta: 0.00%
PartialEvalHugeInterpolatedString-4: old 4.92ms ± 1%: new 5.01ms ± 2%: delta: 1.80%
PartialEvalObject-4: old 21.6µs ± 0%: new 24.5µs ± 0%: delta: 13.45%
TmAllTrueLiteralList-4: old 6.27ms ± 0%: new 6.27ms ± 0%: delta: 0.00%
TmAllTrueFuncall-4: old 161µs ± 0%: new 161µs ± 0%: delta: 0.00%
TmAnyTrueLiteralList-4: old 147ms ± 0%: new 147ms ± 0%: delta: 0.00%
TmAnyTrueFuncall-4: old 161µs ± 0%: new 161µs ± 0%: delta: 0.00%
TmTernary-4: old 2.83µs ± 1%: new 2.83µs ± 0%: delta: 0.00%
TmTry-4: old 52.2µs ± 1%: new 52.0µs ± 0%: delta: 0.00%
check failed: time/op=+20%
metric: alloc/op
CloudReadLines-4: old 3.12MB ± 0%: new 3.12MB ± 0%: delta: -0.00%
CloudReadLine-4: old 3.37MB ± 0%: new 3.37MB ± 0%: delta: 0.00%
ListFiles-4: old 22.0kB ± 0%: new 22.0kB ± 0%: delta: 0.00%
Generate-4: old 2.32GB ± 0%: new 2.32GB ± 0%: delta: 0.00%
GenerateRegex-4: old 955MB ± 0%: new 956MB ± 0%: delta: 0.02%
TokensForExpressionComplex-4: old 412kB ± 0%: new 404kB ± 0%: delta: -1.87%
TokensForExpressionPlainStringNoNewline-4: old 592B ± 0%: new 512B ± 0%: delta: -13.51%
TokensForExpressionStringWith100Newlines-4: old 12.4kB ± 0%: new 12.4kB ± 0%: delta: -0.64%
TokensForExpressionObjectWith100KeysWithNumberValues-4: old 402kB ± 0%: new 394kB ± 0%: delta: -1.99%
TokensForExpression-4: old 412kB ± 0%: new 404kB ± 0%: delta: -1.87%
PartialEvalComplex-4: old 356kB ± 0%: new 408kB ± 0%: delta: 14.74%
PartialEvalSmallString-4: old 1.74kB ± 0%: new 1.74kB ± 0%: delta: 0.00%
PartialEvalHugeString-4: old 166kB ± 0%: new 166kB ± 0%: delta: 0.00%
PartialEvalHugeInterpolatedString-4: old 4.38MB ± 0%: new 4.38MB ± 0%: delta: 0.00%
PartialEvalObject-4: old 20.4kB ± 0%: new 21.9kB ± 0%: delta: 7.34%
TmAllTrueLiteralList-4: old 1.74MB ± 0%: new 1.74MB ± 0%: delta: 0.00%
TmAllTrueFuncall-4: old 45.5kB ± 0%: new 45.5kB ± 0%: delta: 0.00%
TmAnyTrueLiteralList-4: old 37.9MB ± 0%: new 37.9MB ± 0%: delta: -0.00%
TmAnyTrueFuncall-4: old 45.6kB ± 0%: new 45.6kB ± 0%: delta: 0.00%
TmTernary-4: old 1.20kB ± 0%: new 1.20kB ± 0%: delta: 0.00%
TmTry-4: old 11.2kB ± 0%: new 11.2kB ± 0%: delta: 0.00%
metric: allocs/op
CloudReadLines-4: old 5.54k ± 0%: new 5.54k ± 0%: delta: 0.00%
CloudReadLine-4: old 60.0k ± 0%: new 60.0k ± 0%: delta: 0.00%
ListFiles-4: old 321 ± 0%: new 321 ± 0%: delta: 0.00%
Generate-4: old 25.9M ± 0%: new 25.9M ± 0%: delta: 0.00%
GenerateRegex-4: old 18.6M ± 0%: new 18.6M ± 0%: delta: 0.00%
TokensForExpressionComplex-4: old 4.93k ± 0%: new 4.84k ± 0%: delta: -1.95%
TokensForExpressionPlainStringNoNewline-4: old 21.0 ± 0%: new 20.0 ± 0%: delta: -4.76%
TokensForExpressionStringWith100Newlines-4: old 228 ± 0%: new 227 ± 0%: delta: -0.44%
TokensForExpressionObjectWith100KeysWithNumberValues-4: old 3.29k ± 0%: new 3.19k ± 0%: delta: -3.04%
TokensForExpression-4: old 4.93k ± 0%: new 4.84k ± 0%: delta: -1.95%
PartialEvalComplex-4: old 2.86k ± 0%: new 3.65k ± 0%: delta: 27.67%
PartialEvalSmallString-4: old 23.0 ± 0%: new 23.0 ± 0%: delta: 0.00%
PartialEvalHugeString-4: old 35.0 ± 0%: new 35.0 ± 0%: delta: 0.00%
PartialEvalHugeInterpolatedString-4: old 23.1k ± 0%: new 23.1k ± 0%: delta: 0.00%
PartialEvalObject-4: old 125 ± 0%: new 153 ± 0%: delta: 22.40%
TmAllTrueLiteralList-4: old 13.6k ± 0%: new 13.6k ± 0%: delta: 0.00%
TmAllTrueFuncall-4: old 460 ± 0%: new 460 ± 0%: delta: 0.00%
TmAnyTrueLiteralList-4: old 252k ± 0%: new 252k ± 0%: delta: -0.00%
TmAnyTrueFuncall-4: old 462 ± 0%: new 462 ± 0%: delta: 0.00%
TmTernary-4: old 28.0 ± 0%: new 28.0 ± 0%: delta: 0.00%
TmTry-4: old 147 ± 0%: new 147 ± 0%: delta: 0.00%
check failed: allocs/op=+20%