diff --git a/config/hcl2shim/values.go b/config/hcl2shim/values.go index 000ad7ba88..2c5b2907e6 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 805155f0be..7c3011da05 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)