Skip to content

Commit 4acd041

Browse files
committed
move tags and labels funcs to shared.go
1 parent 26fe17d commit 4acd041

File tree

2 files changed

+76
-88
lines changed

2 files changed

+76
-88
lines changed

internal/convert/clu2adv.go

Lines changed: 6 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,13 @@ func fillReplicationSpecs(resourceb *hclwrite.Body, root attrVals) error {
179179
return fmt.Errorf("%s: no replication_specs found", errRepSpecs)
180180
}
181181
if hasVariableNumShards(repSpecBlocks) {
182-
tokens, err := processVariableReplicationSpecs(repSpecBlocks, root)
182+
tokens, err := processVariableRepSpecs(repSpecBlocks, root)
183183
if err != nil {
184184
return err
185185
}
186186
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensFuncConcat(tokens...))
187187
} else {
188-
tokens, err := processStaticReplicationSpecs(resourceb, repSpecBlocks, root)
188+
tokens, err := processStaticRepSpecs(repSpecBlocks, root)
189189
if err != nil {
190190
return err
191191
}
@@ -195,76 +195,6 @@ func fillReplicationSpecs(resourceb *hclwrite.Body, root attrVals) error {
195195
return nil
196196
}
197197

198-
func fillTagsLabelsOpt(resourceb *hclwrite.Body, name string) error {
199-
tokensDynamic, err := extractTagsLabelsDynamicBlock(resourceb, name)
200-
if err != nil {
201-
return err
202-
}
203-
tokensIndividual, err := extractTagsLabelsIndividual(resourceb, name)
204-
if err != nil {
205-
return err
206-
}
207-
if tokensDynamic != nil && tokensIndividual != nil {
208-
resourceb.SetAttributeRaw(name, hcl.TokensFuncMerge(tokensDynamic, tokensIndividual))
209-
return nil
210-
}
211-
if tokensDynamic != nil {
212-
resourceb.SetAttributeRaw(name, tokensDynamic)
213-
}
214-
if tokensIndividual != nil {
215-
resourceb.SetAttributeRaw(name, tokensIndividual)
216-
}
217-
return nil
218-
}
219-
220-
func extractTagsLabelsDynamicBlock(resourceb *hclwrite.Body, name string) (hclwrite.Tokens, error) {
221-
d, err := getDynamicBlock(resourceb, name)
222-
if err != nil || !d.IsPresent() {
223-
return nil, err
224-
}
225-
key := d.content.Body().GetAttribute(nKey)
226-
value := d.content.Body().GetAttribute(nValue)
227-
if key == nil || value == nil {
228-
return nil, fmt.Errorf("dynamic block %s: %s or %s not found", name, nKey, nValue)
229-
}
230-
keyExpr := replaceDynamicBlockExpr(key, name, nKey)
231-
valueExpr := replaceDynamicBlockExpr(value, name, nValue)
232-
collectionExpr := hcl.GetAttrExpr(d.forEach)
233-
forExpr := fmt.Sprintf("for key, value in %s : %s => %s", collectionExpr, keyExpr, valueExpr)
234-
tokens := hcl.EncloseBraces(hcl.EncloseNewLines(hcl.TokensFromExpr(forExpr)), false)
235-
if keyExpr == nKey && valueExpr == nValue { // expression can be simplified and use for_each expression
236-
tokens = hcl.TokensFromExpr(collectionExpr)
237-
}
238-
resourceb.RemoveBlock(d.block)
239-
return tokens, nil
240-
}
241-
242-
func extractTagsLabelsIndividual(resourceb *hclwrite.Body, name string) (hclwrite.Tokens, error) {
243-
var (
244-
file = hclwrite.NewEmptyFile()
245-
fileb = file.Body()
246-
found = false
247-
)
248-
for {
249-
block := resourceb.FirstMatchingBlock(name, nil)
250-
if block == nil {
251-
break
252-
}
253-
key := block.Body().GetAttribute(nKey)
254-
value := block.Body().GetAttribute(nValue)
255-
if key == nil || value == nil {
256-
return nil, fmt.Errorf("%s: %s or %s not found", name, nKey, nValue)
257-
}
258-
setKeyValue(fileb, key, value)
259-
resourceb.RemoveBlock(block)
260-
found = true
261-
}
262-
if !found {
263-
return nil, nil
264-
}
265-
return hcl.TokensObject(fileb), nil
266-
}
267-
268198
// fillReplicationSpecsWithDynamicBlock used for dynamic blocks in replication_specs
269199
func fillReplicationSpecsWithDynamicBlock(resourceb *hclwrite.Body, root attrVals) (dynamicBlock, error) {
270200
dSpec, err := getDynamicBlock(resourceb, nRepSpecs)
@@ -307,7 +237,6 @@ func fillWithDynamicRegionConfigs(specbSrc *hclwrite.Body, root attrVals, change
307237
priorityFor = append(priorityFor, hcl.TokensFromExpr(priorityForStr)...)
308238
priorityFor = append(priorityFor, regionFor...)
309239
repSpecb.SetAttributeRaw(nConfig, hcl.TokensFuncFlatten(priorityFor))
310-
311240
shards := specbSrc.GetAttribute(nNumShards)
312241
if shards == nil {
313242
return dynamicBlock{}, fmt.Errorf("%s: %s not found", errRepSpecs, nNumShards)
@@ -463,10 +392,10 @@ func getDynamicBlockRegionArray(forEach string, configSrc *hclwrite.Block, root
463392
return hcl.EncloseBracketsNewLines(tokens), nil
464393
}
465394

466-
func processVariableReplicationSpecs(repSpecBlocks []*hclwrite.Block, root attrVals) ([]hclwrite.Tokens, error) {
395+
func processVariableRepSpecs(repSpecBlocks []*hclwrite.Block, root attrVals) ([]hclwrite.Tokens, error) {
467396
var concatParts []hclwrite.Tokens
468397
for _, block := range repSpecBlocks {
469-
tokens, err := processReplicationSpecBlock(block, root, true)
398+
tokens, err := processReplicationSpecBlock(block, root)
470399
if err != nil {
471400
return nil, err
472401
}
@@ -475,14 +404,12 @@ func processVariableReplicationSpecs(repSpecBlocks []*hclwrite.Block, root attrV
475404
return concatParts, nil
476405
}
477406

478-
func processStaticReplicationSpecs(resourceb *hclwrite.Body, repSpecBlocks []*hclwrite.Block,
479-
root attrVals) (hclwrite.Tokens, error) {
407+
func processStaticRepSpecs(repSpecBlocks []*hclwrite.Block, root attrVals) (hclwrite.Tokens, error) {
480408
var specbs []*hclwrite.Body
481409
for _, block := range repSpecBlocks {
482410
spec := hclwrite.NewEmptyFile()
483411
specb := spec.Body()
484412
specbSrc := block.Body()
485-
486413
d, err := fillWithDynamicRegionConfigs(specbSrc, root, false)
487414
if err != nil {
488415
return nil, err
@@ -492,50 +419,41 @@ func processStaticReplicationSpecs(resourceb *hclwrite.Body, repSpecBlocks []*hc
492419
// This is complex, return the dynamic block as is
493420
return d.tokens, nil
494421
}
495-
496422
_ = hcl.MoveAttr(specbSrc, specb, nZoneName, nZoneName, errRepSpecs)
497-
498423
shardsAttr := specbSrc.GetAttribute(nNumShards)
499424
if shardsAttr == nil {
500425
return nil, fmt.Errorf("%s: %s not found", errRepSpecs, nNumShards)
501426
}
502-
503427
shardsVal, _ := hcl.GetAttrInt(shardsAttr, errNumShards)
504-
505428
if errConfig := fillRegionConfigs(specb, specbSrc, root); errConfig != nil {
506429
return nil, errConfig
507430
}
508-
509431
for range shardsVal {
510432
specbs = append(specbs, specb)
511433
}
512434
}
513435
return hcl.TokensArray(specbs), nil
514436
}
515437

516-
func processReplicationSpecBlock(block *hclwrite.Block, root attrVals, isVariable bool) (hclwrite.Tokens, error) {
438+
func processReplicationSpecBlock(block *hclwrite.Block, root attrVals) (hclwrite.Tokens, error) {
517439
spec := hclwrite.NewEmptyFile()
518440
specb := spec.Body()
519441
specbSrc := block.Body()
520-
521442
d, err := fillWithDynamicRegionConfigs(specbSrc, root, false)
522443
if err != nil {
523444
return nil, err
524445
}
525446
if d.IsPresent() {
526447
return d.tokens, nil
527448
}
528-
529449
_ = hcl.MoveAttr(specbSrc, specb, nZoneName, nZoneName, errRepSpecs)
530450
shardsAttr := specbSrc.GetAttribute(nNumShards)
531451
if shardsAttr == nil {
532452
return nil, fmt.Errorf("%s: %s not found", errRepSpecs, nNumShards)
533453
}
534-
535454
if errConfig := fillRegionConfigs(specb, specbSrc, root); errConfig != nil {
536455
return nil, errConfig
537456
}
538-
539457
return processNumShards(shardsAttr, specb)
540458
}
541459

internal/convert/shared.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,73 @@ func buildForExpr(varName, collection string, trailingSpace bool) string {
223223
}
224224
return expr
225225
}
226+
227+
func fillTagsLabelsOpt(resourceb *hclwrite.Body, name string) error {
228+
tokensDynamic, err := extractTagsLabelsDynamicBlock(resourceb, name)
229+
if err != nil {
230+
return err
231+
}
232+
tokensIndividual, err := extractTagsLabelsIndividual(resourceb, name)
233+
if err != nil {
234+
return err
235+
}
236+
if tokensDynamic != nil && tokensIndividual != nil {
237+
resourceb.SetAttributeRaw(name, hcl.TokensFuncMerge(tokensDynamic, tokensIndividual))
238+
return nil
239+
}
240+
if tokensDynamic != nil {
241+
resourceb.SetAttributeRaw(name, tokensDynamic)
242+
}
243+
if tokensIndividual != nil {
244+
resourceb.SetAttributeRaw(name, tokensIndividual)
245+
}
246+
return nil
247+
}
248+
249+
func extractTagsLabelsDynamicBlock(resourceb *hclwrite.Body, name string) (hclwrite.Tokens, error) {
250+
d, err := getDynamicBlock(resourceb, name)
251+
if err != nil || !d.IsPresent() {
252+
return nil, err
253+
}
254+
key := d.content.Body().GetAttribute(nKey)
255+
value := d.content.Body().GetAttribute(nValue)
256+
if key == nil || value == nil {
257+
return nil, fmt.Errorf("dynamic block %s: %s or %s not found", name, nKey, nValue)
258+
}
259+
keyExpr := replaceDynamicBlockExpr(key, name, nKey)
260+
valueExpr := replaceDynamicBlockExpr(value, name, nValue)
261+
collectionExpr := hcl.GetAttrExpr(d.forEach)
262+
forExpr := fmt.Sprintf("for key, value in %s : %s => %s", collectionExpr, keyExpr, valueExpr)
263+
tokens := hcl.EncloseBraces(hcl.EncloseNewLines(hcl.TokensFromExpr(forExpr)), false)
264+
if keyExpr == nKey && valueExpr == nValue { // expression can be simplified and use for_each expression
265+
tokens = hcl.TokensFromExpr(collectionExpr)
266+
}
267+
resourceb.RemoveBlock(d.block)
268+
return tokens, nil
269+
}
270+
271+
func extractTagsLabelsIndividual(resourceb *hclwrite.Body, name string) (hclwrite.Tokens, error) {
272+
var (
273+
file = hclwrite.NewEmptyFile()
274+
fileb = file.Body()
275+
found = false
276+
)
277+
for {
278+
block := resourceb.FirstMatchingBlock(name, nil)
279+
if block == nil {
280+
break
281+
}
282+
key := block.Body().GetAttribute(nKey)
283+
value := block.Body().GetAttribute(nValue)
284+
if key == nil || value == nil {
285+
return nil, fmt.Errorf("%s: %s or %s not found", name, nKey, nValue)
286+
}
287+
setKeyValue(fileb, key, value)
288+
resourceb.RemoveBlock(block)
289+
found = true
290+
}
291+
if !found {
292+
return nil, nil
293+
}
294+
return hcl.TokensObject(fileb), nil
295+
}

0 commit comments

Comments
 (0)