Skip to content

Commit f35f01a

Browse files
authored
Merge pull request #19 from takaishi/fix/import
Fix/import
2 parents 7833541 + 7bd9988 commit f35f01a

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

app.go

Lines changed: 40 additions & 4 deletions
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() {
@@ -128,9 +154,19 @@ func (app *App) getValueFromAttribute(attr *hclsyntax.Attribute) (string, error)
128154
}
129155
case hcl.TraverseIndex:
130156
valueSlice = valueSlice[:len(valueSlice)-1]
131-
valueSlice = append(valueSlice, fmt.Sprintf("[\"%s\"]", traversal.(hcl.TraverseIndex).Key.AsString()))
132-
if i == tl-1 {
133-
return strings.Join(valueSlice, ""), nil
157+
switch traversal.(hcl.TraverseIndex).Key.Type().FriendlyName() {
158+
case "string":
159+
valueSlice = append(valueSlice, fmt.Sprintf("[\"%s\"]", traversal.(hcl.TraverseIndex).Key.AsString()))
160+
if i == tl-1 {
161+
return strings.Join(valueSlice, ""), nil
162+
}
163+
case "number":
164+
valueSlice = append(valueSlice, fmt.Sprintf("[%s]", traversal.(hcl.TraverseIndex).Key.AsBigFloat().String()))
165+
if i == tl-1 {
166+
return strings.Join(valueSlice, ""), nil
167+
}
168+
default:
169+
return "", fmt.Errorf("unexpected type: %T", traversal.(hcl.TraverseIndex).Key.Type().FriendlyName())
134170
}
135171
}
136172
}

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)