Skip to content

Commit 55b93fd

Browse files
committed
refactor isDynamicBlock
1 parent 969da96 commit 55b93fd

File tree

1 file changed

+26
-34
lines changed

1 file changed

+26
-34
lines changed

internal/convert/convert.go

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -399,31 +399,13 @@ func getRegionConfig(configSrc *hclwrite.Block, root attrVals, isDynamicBlock bo
399399
if err := hcl.MoveAttr(configSrc.Body(), fileb, nPriority, nPriority, errRepSpecs); err != nil {
400400
return nil, err
401401
}
402-
if electable, _ := getSpecs(configSrc, nElectableNodes, root); electable != nil {
403-
if isDynamicBlock {
404-
tokens := hcl.TokensFromExpr(fmt.Sprintf("region.%s > 0 ?", nElectableNodes))
405-
tokens = append(tokens, electable...)
406-
tokens = append(tokens, hcl.TokensFromExpr(": null")...)
407-
electable = tokens
408-
}
402+
if electable, _ := getSpecs(configSrc, nElectableNodes, root, isDynamicBlock); electable != nil {
409403
fileb.SetAttributeRaw(nElectableSpecs, electable)
410404
}
411-
if readOnly, _ := getSpecs(configSrc, nReadOnlyNodes, root); readOnly != nil {
412-
if isDynamicBlock {
413-
tokens := hcl.TokensFromExpr(fmt.Sprintf("region.%s > 0 ?", nReadOnlyNodes))
414-
tokens = append(tokens, readOnly...)
415-
tokens = append(tokens, hcl.TokensFromExpr(": null")...)
416-
readOnly = tokens
417-
}
405+
if readOnly, _ := getSpecs(configSrc, nReadOnlyNodes, root, isDynamicBlock); readOnly != nil {
418406
fileb.SetAttributeRaw(nReadOnlySpecs, readOnly)
419407
}
420-
if analytics, _ := getSpecs(configSrc, nAnalyticsNodes, root); analytics != nil {
421-
if isDynamicBlock {
422-
tokens := hcl.TokensFromExpr(fmt.Sprintf("region.%s > 0 ?", nAnalyticsNodes))
423-
tokens = append(tokens, analytics...)
424-
tokens = append(tokens, hcl.TokensFromExpr(": null")...)
425-
analytics = tokens
426-
}
408+
if analytics, _ := getSpecs(configSrc, nAnalyticsNodes, root, isDynamicBlock); analytics != nil {
427409
fileb.SetAttributeRaw(nAnalyticsSpecs, analytics)
428410
}
429411
if autoScaling := getAutoScalingOpt(root.opt); autoScaling != nil {
@@ -432,7 +414,7 @@ func getRegionConfig(configSrc *hclwrite.Block, root attrVals, isDynamicBlock bo
432414
return file, nil
433415
}
434416

435-
func getSpecs(configSrc *hclwrite.Block, countName string, root attrVals) (hclwrite.Tokens, error) {
417+
func getSpecs(configSrc *hclwrite.Block, countName string, root attrVals, isDynamicBlock bool) (hclwrite.Tokens, error) {
436418
var (
437419
file = hclwrite.NewEmptyFile()
438420
fileb = file.Body()
@@ -455,7 +437,11 @@ func getSpecs(configSrc *hclwrite.Block, countName string, root attrVals) (hclwr
455437
if root.opt[nDiskIOPSSrc] != nil {
456438
fileb.SetAttributeRaw(nDiskIOPS, root.opt[nDiskIOPSSrc])
457439
}
458-
return hcl.TokensObject(fileb), nil
440+
tokens := hcl.TokensObject(fileb)
441+
if isDynamicBlock {
442+
tokens = encloseDynamicBlockRegionSpec(tokens, countName)
443+
}
444+
return tokens, nil
459445
}
460446

461447
func getAutoScalingOpt(opt map[string]hclwrite.Tokens) hclwrite.Tokens {
@@ -513,17 +499,6 @@ func getResourceLabel(resource *hclwrite.Block) string {
513499
return labels[1]
514500
}
515501

516-
func checkDynamicBlock(body *hclwrite.Body) error {
517-
for _, block := range body.Blocks() {
518-
name := getResourceName(block)
519-
if block.Type() != nDynamic || slices.Contains(dynamicBlockAllowList, name) {
520-
continue
521-
}
522-
return fmt.Errorf("dynamic blocks are not supported for %s", name)
523-
}
524-
return nil
525-
}
526-
527502
type dynamicBlock struct {
528503
block *hclwrite.Block
529504
forEach *hclwrite.Attribute
@@ -535,6 +510,17 @@ func (d dynamicBlock) IsPresent() bool {
535510
return d.block != nil
536511
}
537512

513+
func checkDynamicBlock(body *hclwrite.Body) error {
514+
for _, block := range body.Blocks() {
515+
name := getResourceName(block)
516+
if block.Type() != nDynamic || slices.Contains(dynamicBlockAllowList, name) {
517+
continue
518+
}
519+
return fmt.Errorf("dynamic blocks are not supported for %s", name)
520+
}
521+
return nil
522+
}
523+
538524
func getDynamicBlock(body *hclwrite.Body, name string) (dynamicBlock, error) {
539525
for _, block := range body.Blocks() {
540526
if block.Type() != nDynamic || name != getResourceName(block) {
@@ -559,6 +545,12 @@ func replaceDynamicBlockExpr(attr *hclwrite.Attribute, blockName, attrName strin
559545
return strings.ReplaceAll(expr, fmt.Sprintf("%s.%s", blockName, attrName), attrName)
560546
}
561547

548+
func encloseDynamicBlockRegionSpec(specTokens hclwrite.Tokens, countName string) hclwrite.Tokens {
549+
tokens := hcl.TokensFromExpr(fmt.Sprintf("%s.%s > 0 ?", nRegion, countName))
550+
tokens = append(tokens, specTokens...)
551+
return append(tokens, hcl.TokensFromExpr(": null")...)
552+
}
553+
562554
func sortConfigsByPriority(configs []*hclwrite.Body) []*hclwrite.Body {
563555
for _, config := range configs {
564556
if _, err := hcl.GetAttrInt(config.GetAttribute(nPriority), errPriority); err != nil {

0 commit comments

Comments
 (0)