insert empty objects into config from empty blocks

When creating a legacy config from a cty.Value, empty nested blocks
should corespond to empty objects in the config.
This commit is contained in:
James Bardin 2019-01-05 15:10:30 -05:00
parent 6f54bfaa7c
commit 3677522a28
2 changed files with 12 additions and 16 deletions

View File

@ -80,11 +80,6 @@ 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()
@ -97,11 +92,6 @@ 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()

View File

@ -151,7 +151,7 @@ func TestConfigValueFromHCL2Block(t *testing.T) {
},
{
cty.ObjectVal(map[string]cty.Value{
"address": cty.ListValEmpty(cty.EmptyObject), // should be omitted altogether in result
"address": cty.ListValEmpty(cty.EmptyObject),
}),
&configschema.Block{
BlockTypes: map[string]*configschema.NestedBlock{
@ -161,7 +161,9 @@ func TestConfigValueFromHCL2Block(t *testing.T) {
},
},
},
map[string]interface{}{},
map[string]interface{}{
"address": []interface{}{},
},
},
{
cty.ObjectVal(map[string]cty.Value{
@ -193,7 +195,9 @@ func TestConfigValueFromHCL2Block(t *testing.T) {
},
},
},
map[string]interface{}{},
map[string]interface{}{
"address": []interface{}{},
},
},
{
cty.ObjectVal(map[string]cty.Value{
@ -225,7 +229,9 @@ func TestConfigValueFromHCL2Block(t *testing.T) {
},
},
},
map[string]interface{}{},
map[string]interface{}{
"address": map[string]interface{}{},
},
},
{
cty.NullVal(cty.EmptyObject),
@ -234,8 +240,8 @@ func TestConfigValueFromHCL2Block(t *testing.T) {
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%#v", test.Input), func(t *testing.T) {
for i, test := range tests {
t.Run(fmt.Sprintf("%d-%#v", i, 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)