Skip to content

Commit

Permalink
fix: variables evaluated correctly when module expands based on anoth…
Browse files Browse the repository at this point in the history
…er module
  • Loading branch information
aliscott committed Aug 28, 2023
1 parent b71509c commit 5573c4d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
10 changes: 5 additions & 5 deletions internal/hcl/block.go
Expand Up @@ -160,6 +160,10 @@ func (blocks Blocks) Matching(pattern BlockMatcher) *Block {
search = blocks.OfType(pattern.Type)
}

if pattern.Label == "" && len(search) > 0 {
return search[0]
}

for _, block := range search {
label := block.Label()
if pattern.StripCount {
Expand All @@ -171,10 +175,6 @@ func (blocks Blocks) Matching(pattern BlockMatcher) *Block {
}
}

if len(search) > 0 {
return search[0]
}

return nil
}

Expand Down Expand Up @@ -571,7 +571,7 @@ func (b *Block) IsForEachReferencedExpanded(moduleBlocks Blocks) bool {

label := r.String()
if blockType == "module" {
label = r.typeLabel
label = r.nameLabel
}

referenced := moduleBlocks.Matching(BlockMatcher{
Expand Down
17 changes: 15 additions & 2 deletions internal/hcl/evaluator.go
Expand Up @@ -352,7 +352,21 @@ func (e *Evaluator) evaluateModules() {
if v := moduleCall.Module.Key(); v != nil {
e.ctx.Set(outputs, "module", stripCount(moduleCall.Name), *v)
} else if v := moduleCall.Module.Index(); v != nil {
e.ctx.Set(outputs, "module", stripCount(moduleCall.Name), fmt.Sprintf("%d", *v))
existing := e.ctx.Get("module", stripCount(moduleCall.Name))

var list []cty.Value
if isList(existing) {
list = existing.AsValueSlice()
}

// Pad the list if the index is greater than the current length.
for int64(len(list)) <= *v {
list = append(list, cty.DynamicVal)
}

list[*v] = outputs

e.ctx.Set(cty.TupleVal(list), "module", stripCount(moduleCall.Name))
} else {
e.ctx.Set(outputs, "module", moduleCall.Name)
}
Expand Down Expand Up @@ -1048,5 +1062,4 @@ func ExpFunctions(baseDir string, logger *logrus.Entry) map[string]function.Func
"yamlencode": yaml.YAMLEncodeFunc,
"zipmap": stdlib.ZipmapFunc,
}

}

0 comments on commit 5573c4d

Please sign in to comment.