use correct block types in CoerceValue

When creating Null or Unknown values during CoerceValue, the the outer
block type was being used rather than the current block type.
This commit is contained in:
James Bardin 2018-10-18 18:09:43 -04:00
parent c98f2087fb
commit b3a491d035
2 changed files with 87 additions and 9 deletions

View File

@ -98,10 +98,10 @@ func (b *Block) coerceValue(in cty.Value, path cty.Path) (cty.Value, error) {
switch { switch {
case coll.IsNull(): case coll.IsNull():
attrs[typeName] = cty.NullVal(cty.List(b.ImpliedType())) attrs[typeName] = cty.NullVal(cty.List(blockS.ImpliedType()))
continue continue
case !coll.IsKnown(): case !coll.IsKnown():
attrs[typeName] = cty.UnknownVal(cty.List(b.ImpliedType())) attrs[typeName] = cty.UnknownVal(cty.List(blockS.ImpliedType()))
continue continue
} }
@ -116,7 +116,7 @@ func (b *Block) coerceValue(in cty.Value, path cty.Path) (cty.Value, error) {
return cty.UnknownVal(b.ImpliedType()), path.NewErrorf("too many items for attribute %q; must have at least %d", typeName, blockS.MinItems) return cty.UnknownVal(b.ImpliedType()), path.NewErrorf("too many items for attribute %q; must have at least %d", typeName, blockS.MinItems)
} }
if l == 0 { if l == 0 {
attrs[typeName] = cty.ListValEmpty(b.ImpliedType()) attrs[typeName] = cty.ListValEmpty(blockS.ImpliedType())
continue continue
} }
elems := make([]cty.Value, 0, l) elems := make([]cty.Value, 0, l)
@ -146,10 +146,10 @@ func (b *Block) coerceValue(in cty.Value, path cty.Path) (cty.Value, error) {
switch { switch {
case coll.IsNull(): case coll.IsNull():
attrs[typeName] = cty.NullVal(cty.Set(b.ImpliedType())) attrs[typeName] = cty.NullVal(cty.Set(blockS.ImpliedType()))
continue continue
case !coll.IsKnown(): case !coll.IsKnown():
attrs[typeName] = cty.UnknownVal(cty.Set(b.ImpliedType())) attrs[typeName] = cty.UnknownVal(cty.Set(blockS.ImpliedType()))
continue continue
} }
@ -164,7 +164,7 @@ func (b *Block) coerceValue(in cty.Value, path cty.Path) (cty.Value, error) {
return cty.UnknownVal(b.ImpliedType()), path.NewErrorf("too many items for attribute %q; must have at least %d", typeName, blockS.MinItems) return cty.UnknownVal(b.ImpliedType()), path.NewErrorf("too many items for attribute %q; must have at least %d", typeName, blockS.MinItems)
} }
if l == 0 { if l == 0 {
attrs[typeName] = cty.SetValEmpty(b.ImpliedType()) attrs[typeName] = cty.SetValEmpty(blockS.ImpliedType())
continue continue
} }
elems := make([]cty.Value, 0, l) elems := make([]cty.Value, 0, l)
@ -194,10 +194,10 @@ func (b *Block) coerceValue(in cty.Value, path cty.Path) (cty.Value, error) {
switch { switch {
case coll.IsNull(): case coll.IsNull():
attrs[typeName] = cty.NullVal(cty.Map(b.ImpliedType())) attrs[typeName] = cty.NullVal(cty.Map(blockS.ImpliedType()))
continue continue
case !coll.IsKnown(): case !coll.IsKnown():
attrs[typeName] = cty.UnknownVal(cty.Map(b.ImpliedType())) attrs[typeName] = cty.UnknownVal(cty.Map(blockS.ImpliedType()))
continue continue
} }
@ -206,7 +206,7 @@ func (b *Block) coerceValue(in cty.Value, path cty.Path) (cty.Value, error) {
} }
l := coll.LengthInt() l := coll.LengthInt()
if l == 0 { if l == 0 {
attrs[typeName] = cty.MapValEmpty(b.ImpliedType()) attrs[typeName] = cty.MapValEmpty(blockS.ImpliedType())
continue continue
} }
elems := make(map[string]cty.Value) elems := make(map[string]cty.Value)

View File

@ -319,6 +319,84 @@ func TestCoerceValue(t *testing.T) {
cty.DynamicVal, cty.DynamicVal,
`attribute "foo" is required`, `attribute "foo" is required`,
}, },
"unknown nested list": {
&Block{
Attributes: map[string]*Attribute{
"attr": {
Type: cty.String,
Required: true,
},
},
BlockTypes: map[string]*NestedBlock{
"foo": {
Block: Block{},
Nesting: NestingList,
MinItems: 1,
},
},
},
cty.ObjectVal(map[string]cty.Value{
"attr": cty.StringVal("test"),
"foo": cty.UnknownVal(cty.EmptyObject),
}),
cty.ObjectVal(map[string]cty.Value{
"attr": cty.StringVal("test"),
"foo": cty.UnknownVal(cty.List(cty.EmptyObject)),
}),
"",
},
"unknown nested set": {
&Block{
Attributes: map[string]*Attribute{
"attr": {
Type: cty.String,
Required: true,
},
},
BlockTypes: map[string]*NestedBlock{
"foo": {
Block: Block{},
Nesting: NestingSet,
MinItems: 1,
},
},
},
cty.ObjectVal(map[string]cty.Value{
"attr": cty.StringVal("test"),
"foo": cty.UnknownVal(cty.EmptyObject),
}),
cty.ObjectVal(map[string]cty.Value{
"attr": cty.StringVal("test"),
"foo": cty.UnknownVal(cty.Set(cty.EmptyObject)),
}),
"",
},
"unknown nested map": {
&Block{
Attributes: map[string]*Attribute{
"attr": {
Type: cty.String,
Required: true,
},
},
BlockTypes: map[string]*NestedBlock{
"foo": {
Block: Block{},
Nesting: NestingMap,
MinItems: 1,
},
},
},
cty.ObjectVal(map[string]cty.Value{
"attr": cty.StringVal("test"),
"foo": cty.UnknownVal(cty.Map(cty.String)),
}),
cty.ObjectVal(map[string]cty.Value{
"attr": cty.StringVal("test"),
"foo": cty.UnknownVal(cty.Map(cty.EmptyObject)),
}),
"",
},
"extraneous attribute": { "extraneous attribute": {
&Block{}, &Block{},
cty.ObjectVal(map[string]cty.Value{ cty.ObjectVal(map[string]cty.Value{