From c7269bec02a930682eddab2a162ba0ca50d6de63 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Sat, 12 Jan 2019 09:17:58 -0500 Subject: [PATCH] Revert "insert empty objects into config from empty blocks" This reverts commit 3677522a283f662ac76568f179f29b5e999d4326. Later changes negate the need for this, and removing these again prevents us from having to strip them back out when helper/schema doesn't want them. --- config/hcl2shim/values.go | 10 ++++++++++ config/hcl2shim/values_test.go | 18 ++++++------------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/config/hcl2shim/values.go b/config/hcl2shim/values.go index 000ad7ba8..2c5b2907e 100644 --- a/config/hcl2shim/values.go +++ b/config/hcl2shim/values.go @@ -80,6 +80,11 @@ func ConfigValueFromHCL2Block(v cty.Value, schema *configschema.Block) map[strin case configschema.NestingList, configschema.NestingSet: l := bv.LengthInt() + if l == 0 { + // skip empty collections to better mimic how HCL1 would behave + continue + } + elems := make([]interface{}, 0, l) for it := bv.ElementIterator(); it.Next(); { _, ev := it.Element() @@ -92,6 +97,11 @@ func ConfigValueFromHCL2Block(v cty.Value, schema *configschema.Block) map[strin ret[name] = elems case configschema.NestingMap: + if bv.LengthInt() == 0 { + // skip empty collections to better mimic how HCL1 would behave + continue + } + elems := make(map[string]interface{}) for it := bv.ElementIterator(); it.Next(); { ek, ev := it.Element() diff --git a/config/hcl2shim/values_test.go b/config/hcl2shim/values_test.go index 805155f0b..7c3011da0 100644 --- a/config/hcl2shim/values_test.go +++ b/config/hcl2shim/values_test.go @@ -151,7 +151,7 @@ func TestConfigValueFromHCL2Block(t *testing.T) { }, { cty.ObjectVal(map[string]cty.Value{ - "address": cty.ListValEmpty(cty.EmptyObject), + "address": cty.ListValEmpty(cty.EmptyObject), // should be omitted altogether in result }), &configschema.Block{ BlockTypes: map[string]*configschema.NestedBlock{ @@ -161,9 +161,7 @@ func TestConfigValueFromHCL2Block(t *testing.T) { }, }, }, - map[string]interface{}{ - "address": []interface{}{}, - }, + map[string]interface{}{}, }, { cty.ObjectVal(map[string]cty.Value{ @@ -195,9 +193,7 @@ func TestConfigValueFromHCL2Block(t *testing.T) { }, }, }, - map[string]interface{}{ - "address": []interface{}{}, - }, + map[string]interface{}{}, }, { cty.ObjectVal(map[string]cty.Value{ @@ -229,9 +225,7 @@ func TestConfigValueFromHCL2Block(t *testing.T) { }, }, }, - map[string]interface{}{ - "address": map[string]interface{}{}, - }, + map[string]interface{}{}, }, { cty.NullVal(cty.EmptyObject), @@ -240,8 +234,8 @@ func TestConfigValueFromHCL2Block(t *testing.T) { }, } - for i, test := range tests { - t.Run(fmt.Sprintf("%d-%#v", i, test.Input), func(t *testing.T) { + for _, test := range tests { + t.Run(fmt.Sprintf("%#v", test.Input), func(t *testing.T) { got := ConfigValueFromHCL2Block(test.Input, test.Schema) if !reflect.DeepEqual(got, test.Want) { t.Errorf("wrong result\ninput: %#v\ngot: %#v\nwant: %#v", test.Input, got, test.Want)