handle non-null, but empty NestingMap in a set

This commit is contained in:
James Bardin 2020-10-15 21:21:14 -04:00
parent b59c64245b
commit 77af322c1c
2 changed files with 64 additions and 7 deletions

View File

@ -333,8 +333,9 @@ func setElementCompareValue(schema *configschema.Block, v cty.Value, isConfig bo
} }
for name, blockType := range schema.BlockTypes { for name, blockType := range schema.BlockTypes {
switch blockType.Nesting { elementType := blockType.Block.ImpliedType()
switch blockType.Nesting {
case configschema.NestingSingle, configschema.NestingGroup: case configschema.NestingSingle, configschema.NestingGroup:
attrs[name] = setElementCompareValue(&blockType.Block, v.GetAttr(name), isConfig) attrs[name] = setElementCompareValue(&blockType.Block, v.GetAttr(name), isConfig)
@ -361,7 +362,7 @@ func setElementCompareValue(schema *configschema.Block, v cty.Value, isConfig bo
attrs[name] = cty.SetVal(elems) attrs[name] = cty.SetVal(elems)
// NestingList cases // NestingList cases
case blockType.Block.ImpliedType().HasDynamicTypes(): case elementType.HasDynamicTypes():
attrs[name] = cty.TupleVal(elems) attrs[name] = cty.TupleVal(elems)
default: default:
attrs[name] = cty.ListVal(elems) attrs[name] = cty.ListVal(elems)
@ -369,19 +370,19 @@ func setElementCompareValue(schema *configschema.Block, v cty.Value, isConfig bo
} else { } else {
switch { switch {
case blockType.Nesting == configschema.NestingSet: case blockType.Nesting == configschema.NestingSet:
attrs[name] = cty.SetValEmpty(blockType.Block.ImpliedType()) attrs[name] = cty.SetValEmpty(elementType)
// NestingList cases // NestingList cases
case blockType.Block.ImpliedType().HasDynamicTypes(): case elementType.HasDynamicTypes():
attrs[name] = cty.EmptyTupleVal attrs[name] = cty.EmptyTupleVal
default: default:
attrs[name] = cty.ListValEmpty(blockType.Block.ImpliedType()) attrs[name] = cty.ListValEmpty(elementType)
} }
} }
case configschema.NestingMap: case configschema.NestingMap:
cv := v.GetAttr(name) cv := v.GetAttr(name)
if cv.IsNull() || !cv.IsKnown() { if cv.IsNull() || !cv.IsKnown() || cv.LengthInt() == 0 {
attrs[name] = cv attrs[name] = cv
continue continue
} }
@ -390,8 +391,9 @@ func setElementCompareValue(schema *configschema.Block, v cty.Value, isConfig bo
kv, ev := it.Element() kv, ev := it.Element()
elems[kv.AsString()] = setElementCompareValue(&blockType.Block, ev, isConfig) elems[kv.AsString()] = setElementCompareValue(&blockType.Block, ev, isConfig)
} }
switch { switch {
case blockType.Block.ImpliedType().HasDynamicTypes(): case elementType.HasDynamicTypes():
attrs[name] = cty.ObjectVal(elems) attrs[name] = cty.ObjectVal(elems)
default: default:
attrs[name] = cty.MapVal(elems) attrs[name] = cty.MapVal(elems)

View File

@ -799,6 +799,61 @@ func TestProposedNewObject(t *testing.T) {
}), }),
}), }),
}, },
"empty nested map in set": {
&configschema.Block{
BlockTypes: map[string]*configschema.NestedBlock{
"foo": {
Nesting: configschema.NestingSet,
Block: configschema.Block{
BlockTypes: map[string]*configschema.NestedBlock{
"bar": {
Nesting: configschema.NestingMap,
Block: configschema.Block{
Attributes: map[string]*configschema.Attribute{
"baz": {
Type: cty.String,
Optional: true,
},
},
},
},
},
},
},
},
},
cty.ObjectVal(map[string]cty.Value{
"foo": cty.SetVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"bar": cty.MapValEmpty(cty.Object(map[string]cty.Type{
"baz": cty.String,
})),
}),
}),
}),
cty.ObjectVal(map[string]cty.Value{
"foo": cty.SetVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"bar": cty.MapVal(map[string]cty.Value{
"bing": cty.ObjectVal(map[string]cty.Value{
"baz": cty.StringVal("true"),
}),
}),
}),
}),
}),
cty.ObjectVal(map[string]cty.Value{
"foo": cty.SetVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"bar": cty.MapVal(map[string]cty.Value{
"bing": cty.ObjectVal(map[string]cty.Value{
"baz": cty.StringVal("true"),
}),
}),
}),
}),
}),
},
} }
for name, test := range tests { for name, test := range tests {