Skip to content

Commit 63af914

Browse files
committed
small refactors
1 parent d013a5f commit 63af914

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

internal/convert/convert.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,14 @@ func extractTagsLabelsDynamicBlock(resourceb *hclwrite.Body, name string) (hclwr
261261
}
262262
collectionExpr := strings.TrimSpace(string(d.forEach.Expr().BuildTokens(nil).Bytes()))
263263
keyExpr := strings.TrimSpace(strings.ReplaceAll(string(key.Expr().BuildTokens(nil).Bytes()),
264-
fmt.Sprintf("%s.%s", name, nKey), nKey))
264+
fmt.Sprintf("%s.%s", name, nKey), nKey)) // e.g. occurrences of tags.key are changed to key to be valid in the for expression
265265
valueExpr := strings.TrimSpace(strings.ReplaceAll(string(value.Expr().BuildTokens(nil).Bytes()),
266266
fmt.Sprintf("%s.%s", name, nValue), nValue))
267267
forExpr := strings.TrimSpace(fmt.Sprintf("for key, value in %s : %s => %s",
268268
collectionExpr, keyExpr, valueExpr))
269269
tokenDynamic := hcl.TokensObjectFromExpr(forExpr)
270270
if keyExpr == nKey && valueExpr == nValue { // expression can be simplified and use for_each expression
271-
tokenDynamic = hcl.TokensFromString(collectionExpr)
271+
tokenDynamic = hcl.TokensFromExpr(collectionExpr)
272272
}
273273
resourceb.RemoveBlock(d.block)
274274
return tokenDynamic, nil
@@ -474,11 +474,7 @@ func getDynamicBlock(body *hclwrite.Body, name string) (dynamicBlock, error) {
474474
if content == nil {
475475
return dynamicBlock{}, fmt.Errorf("dynamic block %s: block %s not found", name, nContent)
476476
}
477-
return dynamicBlock{
478-
forEach: forEach,
479-
block: block,
480-
content: content,
481-
}, nil
477+
return dynamicBlock{forEach: forEach, block: block, content: content}, nil
482478
}
483479
return dynamicBlock{}, nil
484480
}

internal/hcl/hcl.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ func TokensObject(body *hclwrite.Body) hclwrite.Tokens {
101101
return encloseBraces(tokens)
102102
}
103103

104-
// TokensFromString creates the tokens for an expression provided as a string.
105-
func TokensFromString(expr string) hclwrite.Tokens {
104+
// TokensFromExpr creates the tokens for an expression provided as a string.
105+
func TokensFromExpr(expr string) hclwrite.Tokens {
106106
return hclwrite.Tokens{{Type: hclsyntax.TokenIdent, Bytes: []byte(expr)}}
107107
}
108108

109109
// TokensObjectFromExpr creates an object with an expression.
110110
func TokensObjectFromExpr(expr string) hclwrite.Tokens {
111111
tokens := hclwrite.Tokens{tokenNewLine}
112-
tokens = append(tokens, TokensFromString(expr)...)
112+
tokens = append(tokens, TokensFromExpr(expr)...)
113113
tokens = append(tokens, tokenNewLine)
114114
return encloseBraces(tokens)
115115
}

0 commit comments

Comments
 (0)