Skip to content

Commit

Permalink
fix incorrect reference to variable block
Browse files Browse the repository at this point in the history
  • Loading branch information
lonegunmanb committed May 17, 2024
1 parent 182f297 commit 28b2115
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions base_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func (c *BaseConfig) EvalContext() *hcl.EvalContext {
}
for bt, bs := range c.blocksByTypes() {
sample := bs[0]
if s, ok := sample.(BlockCustomizedRefType); ok {
bt = s.CustomizedRefType()
}
if _, ok := sample.(SingleValueBlock); ok {
ctx.Variables[bt] = SingleValues(castBlock[SingleValueBlock](bs))
continue
Expand Down
4 changes: 4 additions & 0 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
"strings"
)

type BlockCustomizedRefType interface {
CustomizedRefType() string
}

type BlockType interface {
BlockType() string
}
Expand Down
31 changes: 31 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,34 @@ func (s *configSuite) TestForEach_blocksWithIndexShouldHasNewBlockId() {
d1 := ruleBlocks[1].(Block)
s.NotEqual(d0.Id(), d1.Id())
}

func (s *configSuite) TestParseConfigWithVariable() {
content := `
variable "test" {
default = "hello"
}
data "dummy" sample {
data = {
key = var.test
}
}
`

s.dummyFsWithFiles(map[string]string{
"test.hcl": content,
})
t := s.T()

config, err := BuildDummyConfig("", "", nil, nil)
require.NoError(t, err)
_, err = RunDummyPlan(config)
require.NoError(t, err)
dataBlocks := Blocks[TestData](config)
assert.Len(t, dataBlocks, 1)
dummyData, ok := dataBlocks[0].(*DummyData)
require.True(t, ok)
assert.Equal(t, map[string]string{
"key": "hello",
}, dummyData.Tags)
}
5 changes: 5 additions & 0 deletions variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var _ Variable = &VariableBlock{}
var _ PrePlanBlock = &VariableBlock{}
var _ PlanBlock = &VariableBlock{}
var _ CustomDecode = &VariableBlock{}
var _ BlockCustomizedRefType = &VariableBlock{}

type Variable interface {
SingleValueBlock
Expand Down Expand Up @@ -52,6 +53,10 @@ func (v *VariableBlock) BlockType() string {
return "variable"
}

func (v *VariableBlock) CustomizedRefType() string {
return "var"
}

func (v *VariableBlock) AddressLength() int {
return 2
}
Expand Down

0 comments on commit 28b2115

Please sign in to comment.