Skip to content

Commit

Permalink
schemahcl: support variables in dynamic blocks (#2932)
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m authored Jul 8, 2024
1 parent f299d2f commit 2f7e534
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 6 additions & 6 deletions schemahcl/schemahcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func (s *State) Eval(parsed *hclparse.Parser, v any, input map[string]cty.Value)
blocks := make([]*hclsyntax.Block, 0, len(metaBlocks))
for name, bs := range metaBlocks {
for _, b := range bs {
nb, err := forEachBlocks(ctx, b)
nb, err := s.forEachBlocks(ctx, b)
if err != nil {
return err
}
Expand Down Expand Up @@ -1020,7 +1020,7 @@ func hclList(items []hclwrite.Tokens) hclwrite.Tokens {
return t
}

func forEachBlocks(ctx *hcl.EvalContext, b *hclsyntax.Block) ([]*hclsyntax.Block, error) {
func (s *State) forEachBlocks(ctx *hcl.EvalContext, b *hclsyntax.Block) ([]*hclsyntax.Block, error) {
forEach, diags := b.Body.Attributes[forEachAttr].Expr.Value(ctx)
if diags.HasErrors() {
return nil, diags
Expand All @@ -1039,7 +1039,7 @@ func forEachBlocks(ctx *hcl.EvalContext, b *hclsyntax.Block) ([]*hclsyntax.Block
"value": v,
}),
}
nb, err := copyBlock(nctx, b)
nb, err := s.copyBlock(nctx, b, []string{b.Type})
if err != nil {
return nil, fmt.Errorf("schemahcl: evaluate block for value %q: %w", v, err)
}
Expand All @@ -1048,7 +1048,7 @@ func forEachBlocks(ctx *hcl.EvalContext, b *hclsyntax.Block) ([]*hclsyntax.Block
return blocks, nil
}

func copyBlock(ctx *hcl.EvalContext, b *hclsyntax.Block) (*hclsyntax.Block, error) {
func (s *State) copyBlock(ctx *hcl.EvalContext, b *hclsyntax.Block, scope []string) (*hclsyntax.Block, error) {
nb := &hclsyntax.Block{
Type: b.Type,
Labels: b.Labels,
Expand All @@ -1059,7 +1059,7 @@ func copyBlock(ctx *hcl.EvalContext, b *hclsyntax.Block) (*hclsyntax.Block, erro
},
}
for k, v := range b.Body.Attributes {
x, diags := v.Expr.Value(ctx)
x, diags := v.Expr.Value(s.mayScopeContext(ctx, append(scope, k)))
if diags.HasErrors() {
return nil, diags
}
Expand All @@ -1068,7 +1068,7 @@ func copyBlock(ctx *hcl.EvalContext, b *hclsyntax.Block) (*hclsyntax.Block, erro
nb.Body.Attributes[k] = &nv
}
for _, v := range b.Body.Blocks {
nv, err := copyBlock(ctx, v)
nv, err := s.copyBlock(ctx, v, append(scope, v.Type))
if err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions schemahcl/schemahcl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ env "prod" {
env "staging" {
for_each = toset(var.domains)
url = "${each.value.name}:${each.value.port}"
driver = MYSQL
}
env "dev" {
Expand All @@ -445,6 +446,7 @@ env "dev" {
`)
)
require.NoError(t, New(
WithScopedEnums("env.driver", "MYSQL", "POSTGRES"),
WithDataSource("sql", func(_ context.Context, ectx *hcl.EvalContext, b *hclsyntax.Block) (cty.Value, error) {
attrs, diags := b.Body.JustAttributes()
if diags.HasErrors() {
Expand Down

0 comments on commit 2f7e534

Please sign in to comment.