Skip to content

Commit 4f0cca7

Browse files
committed
reduce use of transformReference
1 parent 49f81b8 commit 4f0cca7

File tree

2 files changed

+8
-20
lines changed

2 files changed

+8
-20
lines changed

internal/convert/adv2v2.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,6 @@ func convertConfigsWithDynamicBlock(specbSrc *hclwrite.Body, diskSizeGB hclwrite
133133
configForEach := fmt.Sprintf("%s.%s", nSpec, nConfig)
134134
configBlockName := getResourceName(d.block)
135135
transformReferences(configBody, configBlockName, nRegion)
136-
for _, block := range configBody.Blocks() {
137-
transformReferences(block.Body(), configBlockName, nRegion)
138-
}
139-
for name, attr := range configBody.Attributes() {
140-
expr := transformReference(hcl.GetAttrExpr(attr), nRepSpecs, nSpec)
141-
configBody.SetAttributeRaw(name, hcl.TokensFromExpr(expr))
142-
}
143-
for _, block := range configBody.Blocks() {
144-
for name, attr := range block.Body().Attributes() {
145-
expr := transformReference(hcl.GetAttrExpr(attr), nRepSpecs, nSpec)
146-
block.Body().SetAttributeRaw(name, hcl.TokensFromExpr(expr))
147-
}
148-
}
149136
regionConfigFile := hclwrite.NewEmptyFile()
150137
regionConfigBody := regionConfigFile.Body()
151138
copyAttributesSorted(regionConfigBody, configBody.Attributes())
@@ -181,13 +168,8 @@ func convertConfig(repSpecs *hclwrite.Body, diskSizeGB hclwrite.Tokens) error {
181168
}
182169
if dConfig.IsPresent() {
183170
blockName := getResourceName(dConfig.block)
184-
transform := func(expr string) string {
185-
return transformReference(expr, blockName, nRegion)
186-
}
187-
copyAttributesSorted(dConfig.content.Body(), dConfig.content.Body().Attributes(), transform)
188-
for _, block := range dConfig.content.Body().Blocks() {
189-
copyAttributesSorted(block.Body(), block.Body().Attributes(), transform)
190-
}
171+
transformReferences(dConfig.content.Body(), blockName, nRegion)
172+
copyAttributesSorted(dConfig.content.Body(), dConfig.content.Body().Attributes())
191173
processAllSpecs(dConfig.content.Body(), diskSizeGB)
192174
tokens := hcl.TokensFromExpr(buildForExpr(nRegion, hcl.GetAttrExpr(dConfig.forEach), false))
193175
tokens = append(tokens, hcl.TokensObject(dConfig.content.Body())...)
@@ -235,6 +217,9 @@ func copyAttributesSorted(targetBody *hclwrite.Body, sourceAttrs map[string]*hcl
235217
}
236218
targetBody.SetAttributeRaw(name, hcl.TokensFromExpr(expr))
237219
}
220+
for _, block := range targetBody.Blocks() {
221+
copyAttributesSorted(block.Body(), block.Body().Attributes(), transforms...)
222+
}
238223
}
239224

240225
func processAllSpecs(body *hclwrite.Body, diskSizeGB hclwrite.Tokens) {

internal/convert/shared.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ func transformReferences(body *hclwrite.Body, blockName, varName string) {
107107
expr := transformReference(hcl.GetAttrExpr(attr), blockName, varName)
108108
body.SetAttributeRaw(name, hcl.TokensFromExpr(expr))
109109
}
110+
for _, block := range body.Blocks() {
111+
transformReferences(block.Body(), blockName, varName)
112+
}
110113
}
111114

112115
// collectBlocks removes and returns all blocks of the given name from body in order of appearance.

0 commit comments

Comments
 (0)