From 13d446d29409f07526faf2bc43b22fd0a1fc162f Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Mon, 22 Jan 2024 20:54:10 +0200 Subject: [PATCH] schemahcl: allow set for_each on tuple any --- doc/md/atlas-schema/hcl.mdx | 2 +- schemahcl/schemahcl.go | 2 +- schemahcl/schemahcl_test.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/doc/md/atlas-schema/hcl.mdx b/doc/md/atlas-schema/hcl.mdx index aa928fc7d71..583c4a80090 100644 --- a/doc/md/atlas-schema/hcl.mdx +++ b/doc/md/atlas-schema/hcl.mdx @@ -805,7 +805,7 @@ or a `set` of references. ```hcl title="schema.pg.hcl" {2-3} trigger "audit_log_trigger" { - for_each = toset([table.users, table.orders, table.payments]) + for_each = [table.users, table.orders, table.payments] on = each.value after { insert = true diff --git a/schemahcl/schemahcl.go b/schemahcl/schemahcl.go index b6cd4446c26..8635be04e97 100644 --- a/schemahcl/schemahcl.go +++ b/schemahcl/schemahcl.go @@ -942,7 +942,7 @@ func forEachBlocks(ctx *hcl.EvalContext, b *hclsyntax.Block) ([]*hclsyntax.Block if diags.HasErrors() { return nil, diags } - if t := forEach.Type(); !t.IsSetType() && !t.IsObjectType() { + if t := forEach.Type(); !t.IsSetType() && !t.IsObjectType() && !t.IsTupleType() { return nil, fmt.Errorf("schemahcl: for_each does not support %s type", t.FriendlyName()) } delete(b.Body.Attributes, forEachAttr) diff --git a/schemahcl/schemahcl_test.go b/schemahcl/schemahcl_test.go index e3ad75acc08..b06eadb1495 100644 --- a/schemahcl/schemahcl_test.go +++ b/schemahcl/schemahcl_test.go @@ -501,6 +501,37 @@ table "t1" { table "t2" { schema = schema.s2 } +`, string(buf)) + + // Tuple of type any. + b1 = []byte(` +schema "s1" { + comment = "schema comment" +} +schema "s2" { + # object without comment. +} + +table { + for_each = [schema.s1, schema.s2] + name = each.value.name + schema = each.value +} +`) + err = New().EvalBytes(b1, &doc1, nil) + require.NoError(t, err) + buf, err = Marshal.MarshalSpec(&doc1) + require.NoError(t, err) + require.Equal(t, `schema "s1" { +} +schema "s2" { +} +table "s1" { + schema = schema.s1 +} +table "s2" { + schema = schema.s2 +} `, string(buf)) }