Skip to content

Commit 0ef8321

Browse files
committed
reduce empty lines
1 parent be5d01a commit 0ef8321

File tree

3 files changed

+9
-36
lines changed

3 files changed

+9
-36
lines changed

internal/convert/adv2v2.go

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -64,68 +64,57 @@ func convertRepSpecs(resourceb *hclwrite.Body, diskSizeGB hclwrite.Tokens) error
6464
if dSpec.IsPresent() {
6565
return convertDynamicRepSpecs(resourceb, dSpec, diskSizeGB)
6666
}
67-
6867
repSpecBlocks := collectBlocks(resourceb, nRepSpecs)
6968
if len(repSpecBlocks) == 0 {
7069
return fmt.Errorf("must have at least one replication_specs")
7170
}
72-
71+
var tokens hclwrite.Tokens
7372
if hasVariableNumShards(repSpecBlocks) {
74-
tokens, err := processVariableNumShards(repSpecBlocks, diskSizeGB)
75-
if err != nil {
76-
return err
77-
}
78-
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensFuncConcat(tokens...))
73+
tokens, err = processVariableNumShards(repSpecBlocks, diskSizeGB)
7974
} else {
80-
tokens, err := processStaticNumShards(repSpecBlocks, diskSizeGB)
81-
if err != nil {
82-
return err
83-
}
84-
resourceb.SetAttributeRaw(nRepSpecs, tokens)
75+
tokens, err = processStaticNumShards(repSpecBlocks, diskSizeGB)
8576
}
86-
77+
if err != nil {
78+
return err
79+
}
80+
resourceb.SetAttributeRaw(nRepSpecs, tokens)
8781
return nil
8882
}
8983

9084
func convertDynamicRepSpecs(resourceb *hclwrite.Body, dSpec dynamicBlock, diskSizeGB hclwrite.Tokens) error {
9185
transformDynamicBlockReferences(dSpec.content.Body(), nRepSpecs, nSpec)
92-
9386
dConfig, err := findDynamicConfigBlock(dSpec.content.Body())
9487
if err != nil {
9588
return err
9689
}
9790
if dConfig.IsPresent() {
9891
return convertDynamicRepSpecsWithDynamicConfig(resourceb, dSpec, dConfig, diskSizeGB)
9992
}
100-
10193
tokens, err := processDynamicRepSpecsWithoutConfig(dSpec, diskSizeGB)
10294
if err != nil {
10395
return err
10496
}
105-
10697
resourceb.RemoveBlock(dSpec.block)
10798
resourceb.SetAttributeRaw(nRepSpecs, tokens)
10899
return nil
109100
}
110101

111-
func processVariableNumShards(repSpecBlocks []*hclwrite.Block, diskSizeGB hclwrite.Tokens) ([]hclwrite.Tokens, error) {
102+
func processVariableNumShards(repSpecBlocks []*hclwrite.Block, diskSizeGB hclwrite.Tokens) (hclwrite.Tokens, error) {
112103
var concatParts []hclwrite.Tokens
113104
for _, block := range repSpecBlocks {
114105
blockb := block.Body()
115106
numShardsAttr := blockb.GetAttribute(nNumShards)
116107
blockb.RemoveAttribute(nNumShards)
117-
118108
if err := convertConfig(blockb, diskSizeGB); err != nil {
119109
return nil, err
120110
}
121-
122111
tokens, err := processNumShards(numShardsAttr, blockb)
123112
if err != nil {
124113
return nil, err
125114
}
126115
concatParts = append(concatParts, tokens)
127116
}
128-
return concatParts, nil
117+
return hcl.TokensFuncConcat(concatParts...), nil
129118
}
130119

131120
func processStaticNumShards(repSpecBlocks []*hclwrite.Block, diskSizeGB hclwrite.Tokens) (hclwrite.Tokens, error) {
@@ -134,11 +123,9 @@ func processStaticNumShards(repSpecBlocks []*hclwrite.Block, diskSizeGB hclwrite
134123
blockb := block.Body()
135124
numShardsAttr := blockb.GetAttribute(nNumShards)
136125
blockb.RemoveAttribute(nNumShards)
137-
138126
if err := convertConfig(blockb, diskSizeGB); err != nil {
139127
return nil, err
140128
}
141-
142129
if numShardsAttr != nil {
143130
numShardsVal, _ := hcl.GetAttrInt(numShardsAttr, errNumShards)
144131
for range numShardsVal {
@@ -168,15 +155,12 @@ func findDynamicConfigBlock(body *hclwrite.Body) (dynamicBlock, error) {
168155
func processDynamicRepSpecsWithoutConfig(dSpec dynamicBlock, diskSizeGB hclwrite.Tokens) (hclwrite.Tokens, error) {
169156
numShardsAttr := dSpec.content.Body().GetAttribute(nNumShards)
170157
dSpec.content.Body().RemoveAttribute(nNumShards)
171-
172158
if err := convertConfig(dSpec.content.Body(), diskSizeGB); err != nil {
173159
return nil, err
174160
}
175-
176161
if numShardsAttr != nil {
177162
return buildDynamicRepSpecsWithShards(dSpec, numShardsAttr)
178163
}
179-
180164
return buildSimpleDynamicRepSpecs(dSpec)
181165
}
182166

@@ -202,22 +186,18 @@ func buildDynamicRepSpecsWithNumShards(dSpec, dConfig dynamicBlock, diskSizeGB h
202186
configBlockName string, numShardsAttr *hclwrite.Attribute) (hclwrite.Tokens, error) {
203187
numShardsExpr := replaceDynamicBlockReferences(hcl.GetAttrExpr(numShardsAttr), nRepSpecs, nSpec)
204188
transformDynamicBlockReferencesRecursive(dConfig.content.Body(), configBlockName, nRegion)
205-
206189
transform := func(expr string) string {
207190
return replaceDynamicBlockReferences(expr, nRepSpecs, nSpec)
208191
}
209192
transformAttributesSorted(dConfig.content.Body(), dConfig.content.Body().Attributes(), transform)
210193
for _, block := range dConfig.content.Body().Blocks() {
211194
transformAttributesSorted(block.Body(), block.Body().Attributes(), transform)
212195
}
213-
214196
regionConfigBody := buildRegionConfigBody(dConfig, diskSizeGB)
215197
regionTokens := buildRegionForExpr(nSpec, regionConfigBody)
216-
217198
repSpecBody := buildRepSpecBody(dSpec, regionTokens)
218199
innerTokens := buildInnerForExpr(numShardsExpr, repSpecBody)
219200
outerTokens := buildOuterForExpr(dSpec, innerTokens)
220-
221201
return hcl.TokensFuncFlatten(outerTokens), nil
222202
}
223203

internal/convert/clu2adv.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ func convertResource(block *hclwrite.Block) (bool, error) {
6767
if errDyn := checkDynamicBlock(blockb); errDyn != nil {
6868
return false, errDyn
6969
}
70-
7170
var err error
7271
if isFreeTierCluster(blockb) {
7372
err = fillFreeTierCluster(blockb)
@@ -139,7 +138,6 @@ func fillFreeTierCluster(resourceb *hclwrite.Body) error {
139138
return err
140139
}
141140
configb.SetAttributeRaw(nElectableSpecs, hcl.TokensObject(electableSpec.Body()))
142-
143141
repSpecs := hclwrite.NewEmptyFile()
144142
repSpecs.Body().SetAttributeRaw(nConfig, hcl.TokensArraySingle(configb))
145143
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensArraySingle(repSpecs.Body()))
@@ -181,12 +179,10 @@ func fillReplicationSpecs(resourceb *hclwrite.Body, root attrVals) error {
181179
resourceb.SetAttributeRaw(nRepSpecs, d.tokens)
182180
return nil
183181
}
184-
185182
repSpecBlocks := collectBlocks(resourceb, nRepSpecs)
186183
if len(repSpecBlocks) == 0 {
187184
return fmt.Errorf("%s: no replication_specs found", errRepSpecs)
188185
}
189-
190186
if hasVariableNumShards(repSpecBlocks) {
191187
tokens, err := processVariableReplicationSpecs(repSpecBlocks, root)
192188
if err != nil {

internal/convert/shared.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,10 @@ func transformDynamicBlockReferences(configSrcb *hclwrite.Body, blockName, varNa
100100
// transformDynamicBlockReferencesRecursive transforms attributes and nested blocks recursively
101101
// replacing references from dynamic block format, e.g. regions_config.value.* to region.*
102102
func transformDynamicBlockReferencesRecursive(body *hclwrite.Body, blockName, varName string) {
103-
// Transform attributes in deterministic order
104103
transform := func(expr string) string {
105104
return replaceDynamicBlockReferences(expr, blockName, varName)
106105
}
107106
transformAttributesSorted(body, body.Attributes(), transform)
108-
109-
// Transform nested blocks
110107
for _, block := range body.Blocks() {
111108
transformDynamicBlockReferencesRecursive(block.Body(), blockName, varName)
112109
}

0 commit comments

Comments
 (0)