lang: EvalBlock should use ReferencesInBlock

Previously it was calling directly to hcldec.Variables, and thus missing
the special fixups we do inside ReferencesInBlock to deal with
Terraform-specific concerns such as our attribute-as-blocks preprocessing.
This commit is contained in:
Martin Atkins 2019-04-03 15:25:58 -07:00
parent 90b70bba08
commit 4d52999538
2 changed files with 62 additions and 17 deletions

View File

@ -47,8 +47,7 @@ func (s *Scope) ExpandBlock(body hcl.Body, schema *configschema.Block) (hcl.Body
func (s *Scope) EvalBlock(body hcl.Body, schema *configschema.Block) (cty.Value, tfdiags.Diagnostics) {
spec := schema.DecoderSpec()
traversals := hcldec.Variables(body, spec)
refs, diags := References(traversals)
refs, diags := ReferencesInBlock(body, schema)
ctx, ctxDiags := s.EvalContext(refs)
diags = diags.Append(ctxDiags)

View File

@ -286,20 +286,20 @@ func TestScopeEvalContext(t *testing.T) {
}
func TestScopeExpandEvalBlock(t *testing.T) {
nestedObjTy := cty.Object(map[string]cty.Type{
"boop": cty.String,
})
schema := &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"foo": {
Type: cty.String,
},
"foo": {Type: cty.String, Optional: true},
"list_of_obj": {Type: cty.List(nestedObjTy), Optional: true},
},
BlockTypes: map[string]*configschema.NestedBlock{
"bar": {
Nesting: configschema.NestingMap,
Block: configschema.Block{
Attributes: map[string]*configschema.Attribute{
"baz": {
Type: cty.String,
},
"baz": {Type: cty.String, Optional: true},
},
},
},
@ -328,6 +328,7 @@ func TestScopeExpandEvalBlock(t *testing.T) {
`,
cty.ObjectVal(map[string]cty.Value{
"foo": cty.NullVal(cty.String),
"list_of_obj": cty.NullVal(cty.List(nestedObjTy)),
"bar": cty.MapValEmpty(cty.Object(map[string]cty.Type{
"baz": cty.String,
})),
@ -339,6 +340,7 @@ func TestScopeExpandEvalBlock(t *testing.T) {
`,
cty.ObjectVal(map[string]cty.Value{
"foo": cty.StringVal("hello"),
"list_of_obj": cty.NullVal(cty.List(nestedObjTy)),
"bar": cty.MapValEmpty(cty.Object(map[string]cty.Type{
"baz": cty.String,
})),
@ -350,6 +352,7 @@ func TestScopeExpandEvalBlock(t *testing.T) {
`,
cty.ObjectVal(map[string]cty.Value{
"foo": cty.StringVal("howdy"),
"list_of_obj": cty.NullVal(cty.List(nestedObjTy)),
"bar": cty.MapValEmpty(cty.Object(map[string]cty.Type{
"baz": cty.String,
})),
@ -361,6 +364,7 @@ func TestScopeExpandEvalBlock(t *testing.T) {
`,
cty.ObjectVal(map[string]cty.Value{
"foo": cty.NullVal(cty.String),
"list_of_obj": cty.NullVal(cty.List(nestedObjTy)),
"bar": cty.MapVal(map[string]cty.Value{
"static": cty.ObjectVal(map[string]cty.Value{
"baz": cty.NullVal(cty.String),
@ -379,6 +383,7 @@ func TestScopeExpandEvalBlock(t *testing.T) {
`,
cty.ObjectVal(map[string]cty.Value{
"foo": cty.NullVal(cty.String),
"list_of_obj": cty.NullVal(cty.List(nestedObjTy)),
"bar": cty.MapVal(map[string]cty.Value{
"static0": cty.ObjectVal(map[string]cty.Value{
"baz": cty.StringVal("0"),
@ -401,6 +406,7 @@ func TestScopeExpandEvalBlock(t *testing.T) {
`,
cty.ObjectVal(map[string]cty.Value{
"foo": cty.NullVal(cty.String),
"list_of_obj": cty.NullVal(cty.List(nestedObjTy)),
"bar": cty.MapVal(map[string]cty.Value{
"elem0": cty.ObjectVal(map[string]cty.Value{
"baz": cty.StringVal("0"),
@ -423,6 +429,7 @@ func TestScopeExpandEvalBlock(t *testing.T) {
`,
cty.ObjectVal(map[string]cty.Value{
"foo": cty.NullVal(cty.String),
"list_of_obj": cty.NullVal(cty.List(nestedObjTy)),
"bar": cty.MapVal(map[string]cty.Value{
"key1": cty.ObjectVal(map[string]cty.Value{
"baz": cty.StringVal("val1"),
@ -433,7 +440,45 @@ func TestScopeExpandEvalBlock(t *testing.T) {
}),
}),
},
"everything at once": {
"list-of-object attribute": {
`
list_of_obj = [
{
boop = local.greeting
},
]
`,
cty.ObjectVal(map[string]cty.Value{
"foo": cty.NullVal(cty.String),
"list_of_obj": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"boop": cty.StringVal("howdy"),
}),
}),
"bar": cty.MapValEmpty(cty.Object(map[string]cty.Type{
"baz": cty.String,
})),
}),
},
"list-of-object attribute as blocks": {
`
list_of_obj {
boop = local.greeting
}
`,
cty.ObjectVal(map[string]cty.Value{
"foo": cty.NullVal(cty.String),
"list_of_obj": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"boop": cty.StringVal("howdy"),
}),
}),
"bar": cty.MapValEmpty(cty.Object(map[string]cty.Type{
"baz": cty.String,
})),
}),
},
"lots of things at once": {
`
foo = "whoop"
bar "static0" {
@ -462,6 +507,7 @@ func TestScopeExpandEvalBlock(t *testing.T) {
`,
cty.ObjectVal(map[string]cty.Value{
"foo": cty.StringVal("whoop"),
"list_of_obj": cty.NullVal(cty.List(nestedObjTy)),
"bar": cty.MapVal(map[string]cty.Value{
"key1": cty.ObjectVal(map[string]cty.Value{
"baz": cty.StringVal("val1"),