From 8f295fcb227fba913a6c85d0661f0d60e1ebfc43 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 20 Jul 2018 12:59:50 -0400 Subject: [PATCH] CoerceValue should insert Null for unset attrs This matches the decoder spec, where a value unset in the configuration is always Null. --- configs/configschema/coerce_value.go | 4 +--- configs/configschema/coerce_value_test.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/configs/configschema/coerce_value.go b/configs/configschema/coerce_value.go index 1f3793ad11..9c54f158aa 100644 --- a/configs/configschema/coerce_value.go +++ b/configs/configschema/coerce_value.go @@ -58,9 +58,7 @@ func (b *Block) coerceValue(in cty.Value, path cty.Path) (cty.Value, error) { switch { case ty.HasAttribute(name): val = in.GetAttr(name) - case attrS.Computed: - val = cty.UnknownVal(attrS.Type) - case attrS.Optional: + case attrS.Computed || attrS.Optional: val = cty.NullVal(attrS.Type) default: return cty.UnknownVal(b.ImpliedType()), path.NewErrorf("attribute %q is required", name) diff --git a/configs/configschema/coerce_value_test.go b/configs/configschema/coerce_value_test.go index d9b7a17f4a..f0e3552d97 100644 --- a/configs/configschema/coerce_value_test.go +++ b/configs/configschema/coerce_value_test.go @@ -342,6 +342,22 @@ func TestCoerceValue(t *testing.T) { cty.DynamicVal, `.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 {