Skip to content

Commit 472bac3

Browse files
committed
refactor buildForExpr
1 parent 1bdacc5 commit 472bac3

File tree

3 files changed

+20
-25
lines changed

3 files changed

+20
-25
lines changed

internal/convert/adv2v2.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ func convertDynamicRepSpecs(resourceb *hclwrite.Body, dSpec dynamicBlock, diskSi
171171
}
172172

173173
// Create the for expression for the flattened replication_specs
174-
outerFor := buildForExpression(nSpec, hcl.GetAttrExpr(dSpec.forEach))
175-
innerFor := buildForExpressionWithIndex("i", fmt.Sprintf("range(%s)", numShardsExpr))
176-
forExpr := fmt.Sprintf("%s [\n %s", outerFor, innerFor)
174+
outerFor := buildForExpr(nSpec, hcl.GetAttrExpr(dSpec.forEach))
175+
innerFor := buildForExpr("i", fmt.Sprintf("range(%s)", numShardsExpr))
176+
forExpr := fmt.Sprintf("%s [\n %s ", outerFor, innerFor)
177177
tokens := hcl.TokensFromExpr(forExpr)
178178
tokens = append(tokens, hcl.TokensObject(dSpec.content.Body())...)
179179
tokens = append(tokens, hcl.TokensFromExpr("\n ]\n]")...)
@@ -191,7 +191,7 @@ func convertDynamicRepSpecs(resourceb *hclwrite.Body, dSpec dynamicBlock, diskSi
191191
}
192192

193193
// Create the for expression without num_shards
194-
forExpr := buildForExpression(nSpec, hcl.GetAttrExpr(dSpec.forEach))
194+
forExpr := buildForExpr(nSpec, hcl.GetAttrExpr(dSpec.forEach))
195195
tokens := hcl.TokensFromExpr(forExpr)
196196
tokens = append(tokens, hcl.TokensObject(dSpec.content.Body())...)
197197
tokens = hcl.EncloseBracketsNewLines(tokens)
@@ -257,7 +257,7 @@ func convertDynamicRepSpecsWithDynamicConfig(resourceb *hclwrite.Body, dSpec, dC
257257
processRegionConfigBlocks(regionConfigBody, dConfig.content.Body().Blocks(), diskSizeGB)
258258

259259
// Build the region_configs for expression
260-
regionForExpr := buildForExpression(nRegion, configForEach)
260+
regionForExpr := buildForExpr(nRegion, configForEach)
261261
regionTokens := hcl.TokensFromExpr(regionForExpr)
262262
regionTokens = append(regionTokens, hcl.TokensObject(regionConfigBody)...)
263263

@@ -273,13 +273,13 @@ func convertDynamicRepSpecsWithDynamicConfig(resourceb *hclwrite.Body, dSpec, dC
273273
repSpecBody.SetAttributeRaw(nConfig, hcl.EncloseBracketsNewLines(regionTokens))
274274

275275
// Build the inner for expression with range
276-
innerForExpr := buildForExpressionWithIndex("i", fmt.Sprintf("range(%s)", numShardsExpr))
276+
innerForExpr := buildForExpr("i", fmt.Sprintf("range(%s)", numShardsExpr))
277277
innerTokens := hcl.TokensFromExpr(innerForExpr)
278278
innerTokens = append(innerTokens, hcl.TokensObject(repSpecBody)...)
279279

280280
// Build the outer for expression
281-
outerForExpr := buildForExpression(nSpec, hcl.GetAttrExpr(dSpec.forEach))
282-
outerTokens := hcl.TokensFromExpr(outerForExpr)
281+
outerForExpr := buildForExpr(nSpec, hcl.GetAttrExpr(dSpec.forEach))
282+
outerTokens := hcl.TokensFromExpr(fmt.Sprintf("%s ", outerForExpr))
283283
outerTokens = append(outerTokens, hcl.EncloseBracketsNewLines(innerTokens)...)
284284

285285
// Apply flatten to the entire expression
@@ -339,16 +339,16 @@ func convertDynamicRepSpecsWithoutNumShards(resourceb *hclwrite.Body, dSpec, dCo
339339
configForEach := fmt.Sprintf("%s.%s", nSpec, nConfig)
340340

341341
// Build the region_configs for expression
342-
regionForExpr := buildForExpression(nRegion, configForEach)
342+
regionForExpr := buildForExpr(nRegion, configForEach)
343343
regionTokens := hcl.TokensFromExpr(regionForExpr)
344344
regionTokens = append(regionTokens, hcl.TokensObject(configb)...)
345345

346346
repSpecb.SetAttributeRaw(nConfig, hcl.EncloseBracketsNewLines(regionTokens))
347347

348348
// Build the for expression as an array wrapped in flatten
349349
// Format: flatten([for spec in ... : [ { ... } ] ])
350-
forExpr := buildForExpression(nSpec, hcl.GetAttrExpr(dSpec.forEach))
351-
innerTokens := hcl.TokensFromExpr(forExpr)
350+
forExpr := buildForExpr(nSpec, hcl.GetAttrExpr(dSpec.forEach))
351+
innerTokens := hcl.TokensFromExpr(fmt.Sprintf("%s ", forExpr))
352352
innerTokens = append(innerTokens, hcl.TokensArraySingle(repSpecb)...)
353353

354354
// Apply flatten to the entire expression
@@ -406,7 +406,7 @@ func convertDynamicConfig(repSpecs *hclwrite.Body, dConfig dynamicBlock, diskSiz
406406
processAllSpecs(dConfig.content.Body(), diskSizeGB)
407407

408408
// Build the for expression
409-
forExpr := buildForExpression(nRegion, hcl.GetAttrExpr(dConfig.forEach))
409+
forExpr := buildForExpr(nRegion, hcl.GetAttrExpr(dConfig.forEach))
410410
tokens := hcl.TokensFromExpr(forExpr)
411411
tokens = append(tokens, hcl.TokensObject(dConfig.content.Body())...)
412412
tokens = hcl.EncloseBracketsNewLines(tokens)

internal/convert/clu2adv.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ func fillReplicationSpecsWithDynamicBlock(resourceb *hclwrite.Body, root attrVal
371371
if err != nil {
372372
return dynamicBlock{}, err
373373
}
374-
forSpec := hcl.TokensFromExpr(buildForExpression(nSpec, hcl.GetAttrExpr(dSpec.forEach)))
374+
forSpec := hcl.TokensFromExpr(fmt.Sprintf("%s ", buildForExpr(nSpec, hcl.GetAttrExpr(dSpec.forEach))))
375375
forSpec = append(forSpec, dConfig.tokens...)
376376
tokens := hcl.TokensFuncFlatten(forSpec)
377377
dSpec.tokens = tokens
@@ -397,17 +397,17 @@ func fillWithDynamicRegionConfigs(specbSrc *hclwrite.Body, root attrVals, change
397397
if err != nil {
398398
return dynamicBlock{}, err
399399
}
400-
priorityForStr := buildForExpression(nPriority, fmt.Sprintf("range(%d, %d, -1)", valMaxPriority, valMinPriority))
400+
priorityForStr := buildForExpr(nPriority, fmt.Sprintf("range(%d, %d, -1)", valMaxPriority, valMinPriority))
401401
priorityFor := hcl.TokensComment(commentPriorityFor)
402-
priorityFor = append(priorityFor, hcl.TokensFromExpr(priorityForStr)...)
402+
priorityFor = append(priorityFor, hcl.TokensFromExpr(fmt.Sprintf("%s ", priorityForStr))...)
403403
priorityFor = append(priorityFor, regionFor...)
404404
repSpecb.SetAttributeRaw(nConfig, hcl.TokensFuncFlatten(priorityFor))
405405

406406
shards := specbSrc.GetAttribute(nNumShards)
407407
if shards == nil {
408408
return dynamicBlock{}, fmt.Errorf("%s: %s not found", errRepSpecs, nNumShards)
409409
}
410-
tokens := hcl.TokensFromExpr(buildForExpressionWithIndex("i", fmt.Sprintf("range(%s)", hcl.GetAttrExpr(shards))))
410+
tokens := hcl.TokensFromExpr(buildForExpr("i", fmt.Sprintf("range(%s)", hcl.GetAttrExpr(shards))))
411411
tokens = append(tokens, hcl.EncloseBraces(repSpec.BuildTokens(nil), true)...)
412412
d.tokens = hcl.EncloseBracketsNewLines(tokens)
413413
return d, nil
@@ -563,7 +563,7 @@ func getDynamicBlockRegionArray(forEach string, configSrc *hclwrite.Block, root
563563
if err != nil {
564564
return nil, err
565565
}
566-
tokens := hcl.TokensFromExpr(buildForExpression(nRegion, forEach))
566+
tokens := hcl.TokensFromExpr(buildForExpr(nRegion, forEach))
567567
tokens = append(tokens, hcl.EncloseBraces(region.BuildTokens(nil), true)...)
568568
tokens = append(tokens, hcl.TokensFromExpr(fmt.Sprintf("if %s == %s", nPriority, priorityStr))...)
569569
return hcl.EncloseBracketsNewLines(tokens), nil

internal/convert/shared.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,7 @@ func fillSpecOpt(resourceb *hclwrite.Body, name string, diskSizeGBTokens hclwrit
186186
fillBlockOpt(resourceb, name)
187187
}
188188

189-
// buildForExpression builds a for expression with the given variable and collection
190-
func buildForExpression(varName, collection string) string {
191-
return fmt.Sprintf("for %s in %s : ", varName, collection)
192-
}
193-
194-
// buildForExpressionWithIndex builds a for expression with an index variable
195-
func buildForExpressionWithIndex(indexVar, collection string) string {
196-
return fmt.Sprintf("for %s in %s : ", indexVar, collection)
189+
// buildForExpr builds a for expression with the given variable and collection
190+
func buildForExpr(varName, collection string) string {
191+
return fmt.Sprintf("for %s in %s :", varName, collection)
197192
}

0 commit comments

Comments
 (0)