Skip to content

Commit 7b6afc3

Browse files
committed
refactor new block creation
1 parent f7921d5 commit 7b6afc3

File tree

3 files changed

+21
-32
lines changed

3 files changed

+21
-32
lines changed

internal/convert/adv2v2.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,22 +149,19 @@ func convertConfigsWithDynamicBlock(specbSrc *hclwrite.Body, diskSizeGB hclwrite
149149
return dynamicBlock{}, err
150150
}
151151
configBody := d.content.Body()
152-
repSpec := hclwrite.NewEmptyFile()
153-
repSpecb := repSpec.Body()
152+
repSpecb := hclwrite.NewEmptyFile().Body()
154153
if zoneNameAttr := specbSrc.GetAttribute(nZoneName); zoneNameAttr != nil {
155154
expr := transformReference(hcl.GetAttrExpr(zoneNameAttr), nRepSpecs, nSpec)
156155
repSpecb.SetAttributeRaw(nZoneName, hcl.TokensFromExpr(expr))
157156
}
158157
configForEach := fmt.Sprintf("%s.%s", nSpec, nConfig)
159158
configBlockName := getResourceName(d.block)
160159
transformReferences(configBody, configBlockName, nRegion)
161-
regionConfigFile := hclwrite.NewEmptyFile()
162-
regionConfigBody := regionConfigFile.Body()
160+
regionConfigBody := hclwrite.NewEmptyFile().Body()
163161
copyAttributesSorted(regionConfigBody, configBody.Attributes())
164162
for _, block := range configBody.Blocks() {
165163
blockType := block.Type()
166-
blockFile := hclwrite.NewEmptyFile()
167-
blockBody := blockFile.Body()
164+
blockBody := hclwrite.NewEmptyFile().Body()
168165
copyAttributesSorted(blockBody, block.Body().Attributes())
169166
if diskSizeGB != nil && (blockType == nElectableSpecs ||
170167
blockType == nReadOnlySpecs || blockType == nAnalyticsSpecs) {
@@ -176,8 +173,7 @@ func convertConfigsWithDynamicBlock(specbSrc *hclwrite.Body, diskSizeGB hclwrite
176173
regionTokens = append(regionTokens, hcl.TokensObject(regionConfigBody)...)
177174
regionConfig := hcl.EncloseBracketsNewLines(regionTokens)
178175
repSpecb.SetAttributeRaw(nConfig, regionConfig)
179-
numShardsAttr := specbSrc.GetAttribute(nNumShards)
180-
if numShardsAttr != nil {
176+
if numShardsAttr := specbSrc.GetAttribute(nNumShards); numShardsAttr != nil {
181177
numShardsExpr := transformReference(hcl.GetAttrExpr(numShardsAttr), nRepSpecs, nSpec)
182178
tokens := hcl.TokensFromExpr(buildForExpr("i", fmt.Sprintf("range(%s)", numShardsExpr), false))
183179
tokens = append(tokens, hcl.TokensObject(repSpecb)...)

internal/convert/clu2adv.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ func fillMovedBlocks(body *hclwrite.Body, moveLabels []string) {
116116
// fillFreeTierCluster is the entry point to convert clusters in free tier
117117
func fillFreeTierCluster(resourceb *hclwrite.Body) error {
118118
resourceb.SetAttributeValue(nClusterType, cty.StringVal(valClusterType))
119-
config := hclwrite.NewEmptyFile()
120-
configb := config.Body()
119+
configb := hclwrite.NewEmptyFile().Body()
121120
hcl.SetAttrInt(configb, nPriority, valMaxPriority)
122121
if err := hcl.MoveAttr(resourceb, configb, nRegionNameSrc, nRegionName, errFreeCluster); err != nil {
123122
return err
@@ -128,14 +127,14 @@ func fillFreeTierCluster(resourceb *hclwrite.Body) error {
128127
if err := hcl.MoveAttr(resourceb, configb, nBackingProviderName, nBackingProviderName, errFreeCluster); err != nil {
129128
return err
130129
}
131-
electableSpec := hclwrite.NewEmptyFile()
132-
if err := hcl.MoveAttr(resourceb, electableSpec.Body(), nInstanceSizeSrc, nInstanceSize, errFreeCluster); err != nil {
130+
electableSpecb := hclwrite.NewEmptyFile().Body()
131+
if err := hcl.MoveAttr(resourceb, electableSpecb, nInstanceSizeSrc, nInstanceSize, errFreeCluster); err != nil {
133132
return err
134133
}
135-
configb.SetAttributeRaw(nElectableSpecs, hcl.TokensObject(electableSpec.Body()))
136-
repSpecs := hclwrite.NewEmptyFile()
137-
repSpecs.Body().SetAttributeRaw(nConfig, hcl.TokensArraySingle(configb))
138-
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensArraySingle(repSpecs.Body()))
134+
configb.SetAttributeRaw(nElectableSpecs, hcl.TokensObject(electableSpecb))
135+
repSpecsb := hclwrite.NewEmptyFile().Body()
136+
repSpecsb.SetAttributeRaw(nConfig, hcl.TokensArraySingle(configb))
137+
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensArraySingle(repSpecsb))
139138
return nil
140139
}
141140

@@ -190,8 +189,7 @@ func fillRepSpecs(resourceb *hclwrite.Body, root attrVals) error {
190189
var resultTokens []hclwrite.Tokens
191190
var resultBodies []*hclwrite.Body
192191
for _, block := range repSpecBlocks {
193-
spec := hclwrite.NewEmptyFile()
194-
specb := spec.Body()
192+
specb := hclwrite.NewEmptyFile().Body()
195193
specbSrc := block.Body()
196194
_ = hcl.MoveAttr(specbSrc, specb, nZoneName, nZoneName, errRepSpecs)
197195
shardsAttr := specbSrc.GetAttribute(nNumShards)
@@ -245,8 +243,7 @@ func fillConfigsWithDynamicRegion(specbSrc *hclwrite.Body, root attrVals, change
245243
if err != nil || !d.IsPresent() {
246244
return dynamicBlock{}, err
247245
}
248-
repSpec := hclwrite.NewEmptyFile()
249-
repSpecb := repSpec.Body()
246+
repSpecb := hclwrite.NewEmptyFile().Body()
250247
if zoneName := hcl.GetAttrExpr(specbSrc.GetAttribute(nZoneName)); zoneName != "" {
251248
repSpecb.SetAttributeRaw(nZoneName, hcl.TokensFromExpr(zoneName))
252249
}
@@ -268,7 +265,7 @@ func fillConfigsWithDynamicRegion(specbSrc *hclwrite.Body, root attrVals, change
268265
return dynamicBlock{}, fmt.Errorf("%s: %s not found", errRepSpecs, nNumShards)
269266
}
270267
tokens := hcl.TokensFromExpr(buildForExpr("i", fmt.Sprintf("range(%s)", hcl.GetAttrExpr(shards)), false))
271-
tokens = append(tokens, hcl.EncloseBraces(repSpec.BuildTokens(nil), true)...)
268+
tokens = append(tokens, hcl.EncloseBraces(repSpecb.BuildTokens(nil), true)...)
272269
d.tokens = hcl.EncloseBracketsNewLines(tokens)
273270
return d, nil
274271
}
@@ -284,7 +281,7 @@ func fillRegionConfigs(specb, specbSrc *hclwrite.Body, root attrVals) error {
284281
if err != nil {
285282
return err
286283
}
287-
configs = append(configs, config.Body())
284+
configs = append(configs, config)
288285
specbSrc.RemoveBlock(configSrc)
289286
}
290287
if len(configs) == 0 {
@@ -295,9 +292,8 @@ func fillRegionConfigs(specb, specbSrc *hclwrite.Body, root attrVals) error {
295292
return nil
296293
}
297294

298-
func getRegionConfig(configSrc *hclwrite.Block, root attrVals, isDynamicBlock bool) (*hclwrite.File, error) {
299-
file := hclwrite.NewEmptyFile()
300-
fileb := file.Body()
295+
func getRegionConfig(configSrc *hclwrite.Block, root attrVals, isDynamicBlock bool) (*hclwrite.Body, error) {
296+
fileb := hclwrite.NewEmptyFile().Body()
301297
fileb.SetAttributeRaw(nProviderName, root.req[nProviderName])
302298
if err := hcl.MoveAttr(configSrc.Body(), fileb, nRegionName, nRegionName, errRepSpecs); err != nil {
303299
return nil, err
@@ -317,13 +313,12 @@ func getRegionConfig(configSrc *hclwrite.Block, root attrVals, isDynamicBlock bo
317313
if autoScaling := getAutoScalingOpt(root.opt); autoScaling != nil {
318314
fileb.SetAttributeRaw(nAutoScaling, autoScaling)
319315
}
320-
return file, nil
316+
return fileb, nil
321317
}
322318

323319
func getSpec(configSrc *hclwrite.Block, countName string, root attrVals, isDynamicBlock bool) (hclwrite.Tokens, error) {
324320
var (
325-
file = hclwrite.NewEmptyFile()
326-
fileb = file.Body()
321+
fileb = hclwrite.NewEmptyFile().Body()
327322
count = configSrc.Body().GetAttribute(countName)
328323
)
329324
if count == nil {
@@ -359,8 +354,7 @@ func getAutoScalingOpt(opt map[string]hclwrite.Tokens) hclwrite.Tokens {
359354
{nComputeMaxInstanceSizeSrc, nComputeMaxInstanceSize},
360355
{nComputeScaleDownEnabledSrc, nComputeScaleDownEnabled},
361356
}
362-
file = hclwrite.NewEmptyFile()
363-
fileb = file.Body()
357+
fileb = hclwrite.NewEmptyFile().Body()
364358
found = false
365359
)
366360
for _, tuple := range names {

internal/convert/shared.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,7 @@ func extractTagsLabelsDynamicBlock(resourceb *hclwrite.Body, name string) (hclwr
206206

207207
func extractTagsLabelsIndividual(resourceb *hclwrite.Body, name string) (hclwrite.Tokens, error) {
208208
var (
209-
file = hclwrite.NewEmptyFile()
210-
fileb = file.Body()
209+
fileb = hclwrite.NewEmptyFile().Body()
211210
)
212211
blocks := collectBlocks(resourceb, name)
213212
if len(blocks) == 0 {

0 commit comments

Comments
 (0)