Skip to content

Commit 893b027

Browse files
yhakbardiofeher
andcommitted
refactor: Address PR #128 nitpick feedback
- Remove dead len(labels) > 0 guards in include/dependency/unit/stack label getters (matchers already guarantee a label) - Extract firstLabelFromContainingBlock helper in ast/stack to DRY GetUnitLabel and GetStackLabel - Use cty.String comparison instead of FriendlyName() == "string" in extractStringValue for idiomatic HCL type checking - Replace tautological predicate in FindFirstParentMatch test with one that actually walks the tree but doesn't match - Remove meaningless assert.NotNil checks on method values in config and stack tests Co-authored-by: Diogenes Fernandes <diofeher@gmail.com>
1 parent 58308d4 commit 893b027

5 files changed

Lines changed: 20 additions & 60 deletions

File tree

internal/ast/ast_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,11 @@ func TestFindFirstParentMatch(t *testing.T) {
283283
content: `locals {
284284
foo = "bar"
285285
}`,
286-
pos: hcl.Pos{Line: 2, Column: 2},
287-
matcher: func(*ast.IndexedNode) bool { return false },
286+
pos: hcl.Pos{Line: 2, Column: 2},
287+
matcher: func(n *ast.IndexedNode) bool {
288+
block, ok := n.Node.(*hclsyntax.Block)
289+
return ok && block.Type == "inputs"
290+
},
288291
expected: false,
289292
},
290293
}

internal/ast/config/config.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,7 @@ func (c *configAST) GetIncludeLabel(node *ast.IndexedNode) (string, bool) {
8181
return "", false
8282
}
8383

84-
name := ""
85-
if labels := includeBlock.Node.(*hclsyntax.Block).Labels; len(labels) > 0 {
86-
name = labels[0]
87-
}
88-
89-
return name, true
84+
return includeBlock.Node.(*hclsyntax.Block).Labels[0], true
9085
}
9186

9287
// GetDependencyLabel returns the label of the given node, if it is a dependency block
@@ -105,12 +100,7 @@ func (c *configAST) GetDependencyLabel(node *ast.IndexedNode) (string, bool) {
105100
return "", false
106101
}
107102

108-
name := ""
109-
if labels := depBlock.Node.(*hclsyntax.Block).Labels; len(labels) > 0 {
110-
name = labels[0]
111-
}
112-
113-
return name, true
103+
return depBlock.Node.(*hclsyntax.Block).Labels[0], true
114104
}
115105

116106
// GetLocals returns the locals scope

internal/ast/config/config_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@ locals {
3737
configAST := config.NewConfigAST(indexedAST)
3838
require.NotNil(t, configAST)
3939

40-
// Test interface methods exist and work
41-
assert.NotNil(t, configAST.FindNodeAt)
42-
assert.NotNil(t, configAST.GetIncludeLabel)
43-
assert.NotNil(t, configAST.GetDependencyLabel)
44-
assert.NotNil(t, configAST.GetLocals)
45-
assert.NotNil(t, configAST.GetIncludes)
46-
4740
// Test that locals and includes are captured
4841
locals := configAST.GetLocals()
4942
assert.NotNil(t, locals)
@@ -86,8 +79,6 @@ dependency "vpc" {
8679
`,
8780
testFunc: func(t *testing.T, configAST config.ConfigAST) {
8881
t.Helper()
89-
// Test that it implements the interface
90-
var _ = configAST
9182
assert.NotNil(t, configAST)
9283
},
9384
},

internal/ast/stack/stack.go

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/hashicorp/hcl/v2"
88
"github.com/hashicorp/hcl/v2/hclsyntax"
9+
"github.com/zclconf/go-cty/cty"
910
)
1011

1112
// StackAST provides methods for working with terragrunt.stack.hcl files.
@@ -39,42 +40,28 @@ func (s *stackAST) FindNodeAt(pos hcl.Pos) *ast.IndexedNode {
3940

4041
// GetUnitLabel returns the label of the given node, if it is a unit block
4142
func (s *stackAST) GetUnitLabel(node *ast.IndexedNode) (string, bool) {
42-
attr := ast.FindFirstParentMatch(node, ast.IsAttribute)
43-
if attr == nil {
44-
return "", false
45-
}
46-
47-
unitBlock := ast.FindFirstParentMatch(attr, isUnitBlock)
48-
if unitBlock == nil {
49-
return "", false
50-
}
51-
52-
name := ""
53-
if labels := unitBlock.Node.(*hclsyntax.Block).Labels; len(labels) > 0 {
54-
name = labels[0]
55-
}
56-
57-
return name, true
43+
return firstLabelFromContainingBlock(node, isUnitBlock)
5844
}
5945

6046
// GetStackLabel returns the label of the given node, if it is a stack block
6147
func (s *stackAST) GetStackLabel(node *ast.IndexedNode) (string, bool) {
48+
return firstLabelFromContainingBlock(node, isStackBlock)
49+
}
50+
51+
// firstLabelFromContainingBlock walks up to the containing attribute and then the
52+
// nearest block matching blockMatcher, returning that block's first label.
53+
func firstLabelFromContainingBlock(node *ast.IndexedNode, blockMatcher func(*ast.IndexedNode) bool) (string, bool) {
6254
attr := ast.FindFirstParentMatch(node, ast.IsAttribute)
6355
if attr == nil {
6456
return "", false
6557
}
6658

67-
stackBlock := ast.FindFirstParentMatch(attr, isStackBlock)
68-
if stackBlock == nil {
59+
block := ast.FindFirstParentMatch(attr, blockMatcher)
60+
if block == nil {
6961
return "", false
7062
}
7163

72-
name := ""
73-
if labels := stackBlock.Node.(*hclsyntax.Block).Labels; len(labels) > 0 {
74-
name = labels[0]
75-
}
76-
77-
return name, true
64+
return block.Node.(*hclsyntax.Block).Labels[0], true
7865
}
7966

8067
// GetUnitSource returns the source attribute value from a unit block
@@ -162,14 +149,14 @@ func (s *stackAST) getBlockAttribute(node *ast.IndexedNode, blockMatcher func(*a
162149
func (s *stackAST) extractStringValue(expr hclsyntax.Expression) (string, bool) {
163150
switch e := expr.(type) {
164151
case *hclsyntax.LiteralValueExpr:
165-
if e.Val.Type().FriendlyName() == "string" {
152+
if e.Val.Type() == cty.String {
166153
return e.Val.AsString(), true
167154
}
168155
case *hclsyntax.TemplateExpr:
169156
// Handle quoted strings which are parsed as TemplateExpr
170157
if len(e.Parts) == 1 {
171158
if literal, ok := e.Parts[0].(*hclsyntax.LiteralValueExpr); ok {
172-
if literal.Val.Type().FriendlyName() == "string" {
159+
if literal.Val.Type() == cty.String {
173160
return literal.Val.AsString(), true
174161
}
175162
}

internal/ast/stack/stack_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@ stack "nested" {
3939
// Create StackAST
4040
stackAST := stack.NewStackAST(indexedAST)
4141
require.NotNil(t, stackAST)
42-
43-
// Test interface methods exist and work
44-
assert.NotNil(t, stackAST.FindNodeAt)
45-
assert.NotNil(t, stackAST.GetUnitLabel)
46-
assert.NotNil(t, stackAST.GetStackLabel)
47-
assert.NotNil(t, stackAST.GetUnitSource)
48-
assert.NotNil(t, stackAST.GetUnitPath)
49-
assert.NotNil(t, stackAST.FindUnitAt)
50-
assert.NotNil(t, stackAST.FindStackAt)
5142
}
5243

5344
func TestStackAST_Methods(t *testing.T) {
@@ -82,8 +73,6 @@ stack "nested" {
8273
`,
8374
testFunc: func(t *testing.T, stackAST stack.StackAST) {
8475
t.Helper()
85-
// Test that it implements the interface
86-
var _ = stackAST
8776
assert.NotNil(t, stackAST)
8877
},
8978
},

0 commit comments

Comments
 (0)