From 97bde5467c8ff8f791e0556fe809b2d8bf11f511 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 2 Apr 2019 14:27:31 -0400 Subject: [PATCH] filter nulls when shimming a config Nulls can't exist in HCL, and the legacy sdk will panic when encountering them. The map shim already did this, just copy the same pattern in the list case. --- config/hcl2shim/values.go | 5 ++++- config/hcl2shim/values_test.go | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/config/hcl2shim/values.go b/config/hcl2shim/values.go index 2c5b2907e..b5868f6ed 100644 --- a/config/hcl2shim/values.go +++ b/config/hcl2shim/values.go @@ -167,7 +167,10 @@ func ConfigValueFromHCL2(v cty.Value) interface{} { it := v.ElementIterator() for it.Next() { _, ev := it.Element() - l = append(l, ConfigValueFromHCL2(ev)) + cv := ConfigValueFromHCL2(ev) + if cv != nil { + l = append(l, cv) + } } return l } diff --git a/config/hcl2shim/values_test.go b/config/hcl2shim/values_test.go index 7c3011da0..ac70c339e 100644 --- a/config/hcl2shim/values_test.go +++ b/config/hcl2shim/values_test.go @@ -232,6 +232,26 @@ func TestConfigValueFromHCL2Block(t *testing.T) { &configschema.Block{}, nil, }, + // nulls should be filtered out of the config, since they couldn't exist + // in hcl. + { + cty.ObjectVal(map[string]cty.Value{ + "list": cty.ListVal([]cty.Value{ + cty.StringVal("ok"), + cty.NullVal(cty.String)}), + }), + &configschema.Block{ + Attributes: map[string]*configschema.Attribute{ + "list": { + Type: cty.List(cty.String), + Optional: true, + }, + }, + }, + map[string]interface{}{ + "list": []interface{}{"ok"}, + }, + }, } for _, test := range tests {