Skip to content

Commit 49f81b8

Browse files
committed
rename dynamic block helper functions
1 parent a177614 commit 49f81b8

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

internal/convert/adv2v2.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func convertRepSpecsWithDynamicBlock(resourceb *hclwrite.Body, diskSizeGB hclwri
108108
if err != nil || !dSpec.IsPresent() {
109109
return dynamicBlock{}, err
110110
}
111-
transformDynamicBlockReferences(dSpec.content.Body(), nRepSpecs, nSpec)
111+
transformReferences(dSpec.content.Body(), nRepSpecs, nSpec)
112112
dConfig, err := convertConfigsWithDynamicBlock(dSpec.content.Body(), diskSizeGB)
113113
if err != nil {
114114
return dynamicBlock{}, err
@@ -123,32 +123,33 @@ func convertConfigsWithDynamicBlock(specbSrc *hclwrite.Body, diskSizeGB hclwrite
123123
if err != nil {
124124
return dynamicBlock{}, err
125125
}
126+
configBody := d.content.Body()
126127
repSpec := hclwrite.NewEmptyFile()
127128
repSpecb := repSpec.Body()
128129
if zoneNameAttr := specbSrc.GetAttribute(nZoneName); zoneNameAttr != nil {
129-
expr := replaceDynamicBlockReferences(hcl.GetAttrExpr(zoneNameAttr), nRepSpecs, nSpec)
130+
expr := transformReference(hcl.GetAttrExpr(zoneNameAttr), nRepSpecs, nSpec)
130131
repSpecb.SetAttributeRaw(nZoneName, hcl.TokensFromExpr(expr))
131132
}
132133
configForEach := fmt.Sprintf("%s.%s", nSpec, nConfig)
133134
configBlockName := getResourceName(d.block)
134-
transformDynamicBlockReferences(d.content.Body(), configBlockName, nRegion)
135-
for _, block := range d.content.Body().Blocks() {
136-
transformDynamicBlockReferences(block.Body(), configBlockName, nRegion)
135+
transformReferences(configBody, configBlockName, nRegion)
136+
for _, block := range configBody.Blocks() {
137+
transformReferences(block.Body(), configBlockName, nRegion)
137138
}
138-
for name, attr := range d.content.Body().Attributes() {
139-
expr := replaceDynamicBlockReferences(hcl.GetAttrExpr(attr), nRepSpecs, nSpec)
140-
d.content.Body().SetAttributeRaw(name, hcl.TokensFromExpr(expr))
139+
for name, attr := range configBody.Attributes() {
140+
expr := transformReference(hcl.GetAttrExpr(attr), nRepSpecs, nSpec)
141+
configBody.SetAttributeRaw(name, hcl.TokensFromExpr(expr))
141142
}
142-
for _, block := range d.content.Body().Blocks() {
143+
for _, block := range configBody.Blocks() {
143144
for name, attr := range block.Body().Attributes() {
144-
expr := replaceDynamicBlockReferences(hcl.GetAttrExpr(attr), nRepSpecs, nSpec)
145+
expr := transformReference(hcl.GetAttrExpr(attr), nRepSpecs, nSpec)
145146
block.Body().SetAttributeRaw(name, hcl.TokensFromExpr(expr))
146147
}
147148
}
148149
regionConfigFile := hclwrite.NewEmptyFile()
149150
regionConfigBody := regionConfigFile.Body()
150-
copyAttributesSorted(regionConfigBody, d.content.Body().Attributes())
151-
for _, block := range d.content.Body().Blocks() {
151+
copyAttributesSorted(regionConfigBody, configBody.Attributes())
152+
for _, block := range configBody.Blocks() {
152153
blockType := block.Type()
153154
blockFile := hclwrite.NewEmptyFile()
154155
blockBody := blockFile.Body()
@@ -165,7 +166,7 @@ func convertConfigsWithDynamicBlock(specbSrc *hclwrite.Body, diskSizeGB hclwrite
165166
repSpecb.SetAttributeRaw(nConfig, regionConfig)
166167
numShardsAttr := specbSrc.GetAttribute(nNumShards)
167168
if numShardsAttr != nil {
168-
numShardsExpr := replaceDynamicBlockReferences(hcl.GetAttrExpr(numShardsAttr), nRepSpecs, nSpec)
169+
numShardsExpr := transformReference(hcl.GetAttrExpr(numShardsAttr), nRepSpecs, nSpec)
169170
tokens := hcl.TokensFromExpr(buildForExpr("i", fmt.Sprintf("range(%s)", numShardsExpr), false))
170171
tokens = append(tokens, hcl.TokensObject(repSpecb)...)
171172
return dynamicBlock{tokens: hcl.EncloseBracketsNewLines(tokens)}, nil
@@ -181,7 +182,7 @@ func convertConfig(repSpecs *hclwrite.Body, diskSizeGB hclwrite.Tokens) error {
181182
if dConfig.IsPresent() {
182183
blockName := getResourceName(dConfig.block)
183184
transform := func(expr string) string {
184-
return replaceDynamicBlockReferences(expr, blockName, nRegion)
185+
return transformReference(expr, blockName, nRegion)
185186
}
186187
copyAttributesSorted(dConfig.content.Body(), dConfig.content.Body().Attributes(), transform)
187188
for _, block := range dConfig.content.Body().Blocks() {

internal/convert/clu2adv.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func fillRepSpecsWithDynamicBlock(resourceb *hclwrite.Body, root attrVals) (dyna
227227
if err != nil || !dSpec.IsPresent() {
228228
return dynamicBlock{}, err
229229
}
230-
transformDynamicBlockReferences(dSpec.content.Body(), nRepSpecs, nSpec)
230+
transformReferences(dSpec.content.Body(), nRepSpecs, nSpec)
231231
dConfig, err := fillConfigsWithDynamicRegion(dSpec.content.Body(), root, true)
232232
if err != nil {
233233
return dynamicBlock{}, err
@@ -252,7 +252,7 @@ func fillConfigsWithDynamicRegion(specbSrc *hclwrite.Body, root attrVals, change
252252
}
253253
forEach := hcl.GetAttrExpr(d.forEach)
254254
if changeReferences {
255-
forEach = replaceDynamicBlockReferences(forEach, nRepSpecs, nSpec)
255+
forEach = transformReference(forEach, nRepSpecs, nSpec)
256256
}
257257
regionFor, err := getDynamicBlockRegionArray(forEach, d.content, root)
258258
if err != nil {
@@ -403,7 +403,7 @@ func replaceDynamicBlockExpr(attr *hclwrite.Attribute, blockName, attrName strin
403403
// getDynamicBlockRegionArray returns the region array for a dynamic block in replication_specs.
404404
// e.g. [ for region in var.replication_specs.regions_config : { ... } if priority == region.priority ]
405405
func getDynamicBlockRegionArray(forEach string, configSrc *hclwrite.Block, root attrVals) (hclwrite.Tokens, error) {
406-
transformDynamicBlockReferences(configSrc.Body(), nConfigSrc, nRegion)
406+
transformReferences(configSrc.Body(), nConfigSrc, nRegion)
407407
priorityStr := hcl.GetAttrExpr(configSrc.Body().GetAttribute(nPriority))
408408
if priorityStr == "" {
409409
return nil, fmt.Errorf("%s: %s not found", errRepSpecs, nPriority)

internal/convert/shared.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,19 @@ func getResourceName(resource *hclwrite.Block) string {
9393
return labels[0]
9494
}
9595

96-
// replaceDynamicBlockReferences changes value references,
96+
// transformReference changes value references,
9797
// e.g. regions_config.value.electable_nodes to region.electable_nodes
98-
func replaceDynamicBlockReferences(expr, blockName, varName string) string {
98+
func transformReference(expr, blockName, varName string) string {
9999
return strings.ReplaceAll(expr,
100100
fmt.Sprintf("%s.%s.", blockName, nValue),
101101
fmt.Sprintf("%s.", varName))
102102
}
103103

104-
// transformDynamicBlockReferences transforms all attribute references in a body from dynamic block format
105-
func transformDynamicBlockReferences(configSrcb *hclwrite.Body, blockName, varName string) {
106-
for name, attr := range configSrcb.Attributes() {
107-
expr := replaceDynamicBlockReferences(hcl.GetAttrExpr(attr), blockName, varName)
108-
configSrcb.SetAttributeRaw(name, hcl.TokensFromExpr(expr))
104+
// transformReferences transforms all attribute references in a body from dynamic block format
105+
func transformReferences(body *hclwrite.Body, blockName, varName string) {
106+
for name, attr := range body.Attributes() {
107+
expr := transformReference(hcl.GetAttrExpr(attr), blockName, varName)
108+
body.SetAttributeRaw(name, hcl.TokensFromExpr(expr))
109109
}
110110
}
111111

0 commit comments

Comments
 (0)