Skip to content

Commit 7bd9988

Browse files
committed
fix: can't parse id with embedding variable
1 parent 3332f1a commit 7bd9988

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

app.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,40 @@ func (app *App) processFile(path string, state *tfstate.TFState) error {
9898
func (app *App) getValueFromAttribute(attr *hclsyntax.Attribute) (string, error) {
9999
switch attr.Expr.(type) {
100100
case *hclsyntax.TemplateExpr:
101+
result := []string{}
101102
for _, part := range attr.Expr.(*hclsyntax.TemplateExpr).Parts {
102103
switch part.(type) {
103104
case *hclsyntax.LiteralValueExpr:
104-
return part.(*hclsyntax.LiteralValueExpr).Val.AsString(), nil
105+
result = append(result, part.(*hclsyntax.LiteralValueExpr).Val.AsString())
106+
case *hclsyntax.ScopeTraversalExpr:
107+
valueSlice := []string{"\"", "${"}
108+
for _, traversals := range part.(*hclsyntax.ScopeTraversalExpr).Variables() {
109+
tl := len(traversals)
110+
for i, traversal := range traversals {
111+
switch traversal.(type) {
112+
case hcl.TraverseRoot:
113+
valueSlice = append(valueSlice, traversal.(hcl.TraverseRoot).Name)
114+
valueSlice = append(valueSlice, ".")
115+
if i == tl-1 {
116+
valueSlice = valueSlice[:len(valueSlice)-1]
117+
}
118+
case hcl.TraverseAttr:
119+
valueSlice = append(valueSlice, traversal.(hcl.TraverseAttr).Name)
120+
valueSlice = append(valueSlice, ".")
121+
if i == tl-1 {
122+
valueSlice = valueSlice[:len(valueSlice)-1]
123+
}
124+
}
125+
}
126+
}
127+
valueSlice = append(valueSlice, "}")
128+
result = append(result, strings.Join(valueSlice, ""))
105129
default:
106130
return "", fmt.Errorf("unexpected type: %T", part)
107131
}
108132
}
133+
result = append(result, "\"")
134+
return strings.Join(result, ""), nil
109135
case *hclsyntax.ScopeTraversalExpr:
110136
valueSlice := []string{}
111137
for _, traversals := range attr.Expr.(*hclsyntax.ScopeTraversalExpr).Variables() {

import.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (app *App) cutImportBlock(data []byte, to string, id string) ([]byte, error
5858
s.Init(bytes.NewReader(data))
5959
s.Mode = scanner.ScanIdents | scanner.ScanFloats
6060
s.IsIdentRune = func(ch rune, i int) bool {
61-
return ch == '-' || ch == '_' || ch == '.' || ch == '[' || ch == ']' || ch == ':' || ch == '"' || unicode.IsLetter(ch) || unicode.IsDigit(ch) && i > 0
61+
return ch == '-' || ch == '_' || ch == '.' || ch == '[' || ch == ']' || ch == ':' || ch == '"' || ch == '$' || ch == '{' || ch == '}' || unicode.IsLetter(ch) || unicode.IsDigit(ch) && i > 0
6262
}
6363

6464
var lastPos int

import_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,29 @@ import {
126126
to = module.foo["hoge"]
127127
}
128128
bbb
129+
`),
130+
wantErr: false,
131+
},
132+
{
133+
name: "",
134+
fields: fields{},
135+
args: args{
136+
data: []byte(`
137+
# import
138+
aaa
139+
import {
140+
id = "${local.a}-1"
141+
to = module.foo[0]
142+
}
143+
bbb
144+
`),
145+
to: "module.foo[0]",
146+
id: "${local.a}-1",
147+
},
148+
want: []byte(`
149+
# import
150+
aaa
151+
bbb
129152
`),
130153
wantErr: false,
131154
},

0 commit comments

Comments
 (0)