Skip to content

Commit 2a39ae2

Browse files
committed
fix panics
1 parent eeb4f59 commit 2a39ae2

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

hclext/references.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import (
55
"strings"
66

77
"github.com/hashicorp/hcl/v2"
8+
"github.com/zclconf/go-cty/cty"
89
)
910

1011
func ReferenceNames(exp hcl.Expression) []string {
12+
if exp == nil {
13+
return []string{}
14+
}
1115
allVars := exp.Variables()
1216
vars := make([]string, 0, len(allVars))
1317

@@ -29,7 +33,14 @@ func CreateDotReferenceFromTraversal(traversals ...hcl.Traversal) string {
2933
case hcl.TraverseAttr:
3034
refParts = append(refParts, part.Name)
3135
case hcl.TraverseIndex:
32-
refParts = append(refParts, fmt.Sprintf("[%s]", part.Key.AsString()))
36+
if part.Key.Type().Equals(cty.String) {
37+
refParts = append(refParts, fmt.Sprintf("[%s]", part.Key.AsString()))
38+
} else if part.Key.Type().Equals(cty.Number) {
39+
idx, _ := part.Key.AsBigFloat().Int64()
40+
refParts = append(refParts, fmt.Sprintf("[%d]", idx))
41+
} else {
42+
refParts = append(refParts, fmt.Sprintf("[?? %q]", part.Key.Type().FriendlyName()))
43+
}
3344
}
3445
}
3546
}

0 commit comments

Comments
 (0)