CoerceValue should insert Null for unset attrs

This matches the decoder spec, where a value unset in the configuration
is always Null.
This commit is contained in:
James Bardin 2018-07-20 12:59:50 -04:00 committed by Martin Atkins
parent 50e099ad10
commit 8f295fcb22
2 changed files with 17 additions and 3 deletions

View File

@ -58,9 +58,7 @@ func (b *Block) coerceValue(in cty.Value, path cty.Path) (cty.Value, error) {
switch { switch {
case ty.HasAttribute(name): case ty.HasAttribute(name):
val = in.GetAttr(name) val = in.GetAttr(name)
case attrS.Computed: case attrS.Computed || attrS.Optional:
val = cty.UnknownVal(attrS.Type)
case attrS.Optional:
val = cty.NullVal(attrS.Type) val = cty.NullVal(attrS.Type)
default: default:
return cty.UnknownVal(b.ImpliedType()), path.NewErrorf("attribute %q is required", name) return cty.UnknownVal(b.ImpliedType()), path.NewErrorf("attribute %q is required", name)

View File

@ -342,6 +342,22 @@ func TestCoerceValue(t *testing.T) {
cty.DynamicVal, cty.DynamicVal,
`.foo: number required`, `.foo: number required`,
}, },
"unset computed value": {
&Block{
Attributes: map[string]*Attribute{
"foo": {
Type: cty.String,
Optional: true,
Computed: true,
},
},
},
cty.ObjectVal(map[string]cty.Value{}),
cty.ObjectVal(map[string]cty.Value{
"foo": cty.NullVal(cty.String),
}),
``,
},
} }
for name, test := range tests { for name, test := range tests {