@@ -98,14 +98,40 @@ func (app *App) processFile(path string, state *tfstate.TFState) error {
98
98
func (app * App ) getValueFromAttribute (attr * hclsyntax.Attribute ) (string , error ) {
99
99
switch attr .Expr .(type ) {
100
100
case * hclsyntax.TemplateExpr :
101
+ result := []string {}
101
102
for _ , part := range attr .Expr .(* hclsyntax.TemplateExpr ).Parts {
102
103
switch part .(type ) {
103
104
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 , "" ))
105
129
default :
106
130
return "" , fmt .Errorf ("unexpected type: %T" , part )
107
131
}
108
132
}
133
+ result = append (result , "\" " )
134
+ return strings .Join (result , "" ), nil
109
135
case * hclsyntax.ScopeTraversalExpr :
110
136
valueSlice := []string {}
111
137
for _ , traversals := range attr .Expr .(* hclsyntax.ScopeTraversalExpr ).Variables () {
@@ -128,9 +154,19 @@ func (app *App) getValueFromAttribute(attr *hclsyntax.Attribute) (string, error)
128
154
}
129
155
case hcl.TraverseIndex :
130
156
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 ())
134
170
}
135
171
}
136
172
}
0 commit comments