return a NullVal when presented with a nil flatmap

This allows us to decode the cty.Value back to a nil map, rather than
trying to create a map with nil values.
This commit is contained in:
James Bardin 2018-07-10 15:45:43 -04:00 committed by Martin Atkins
parent 479c6b2466
commit ac8ee20233
2 changed files with 16 additions and 0 deletions

View File

@ -131,6 +131,9 @@ func flatmapValueFromHCL2Seq(m map[string]string, prefix string, val cty.Value)
// The result may contain null values if the given map does not contain keys // The result may contain null values if the given map does not contain keys
// for all of the different key paths implied by the given type. // for all of the different key paths implied by the given type.
func HCL2ValueFromFlatmap(m map[string]string, ty cty.Type) (cty.Value, error) { func HCL2ValueFromFlatmap(m map[string]string, ty cty.Type) (cty.Value, error) {
if m == nil {
return cty.NullVal(ty), nil
}
if !ty.IsObjectType() { if !ty.IsObjectType() {
panic(fmt.Sprintf("HCL2ValueFromFlatmap called on %#v", ty)) panic(fmt.Sprintf("HCL2ValueFromFlatmap called on %#v", ty))
} }

View File

@ -522,6 +522,19 @@ func TestHCL2ValueFromFlatmap(t *testing.T) {
}), }),
WantErr: `invalid count value for "foo." in state: strconv.Atoi: parsing "not-valid": invalid syntax`, WantErr: `invalid count value for "foo." in state: strconv.Atoi: parsing "not-valid": invalid syntax`,
}, },
{
Flatmap: nil,
Type: cty.Object(map[string]cty.Type{
"foo": cty.Set(cty.Object(map[string]cty.Type{
"bar": cty.String,
})),
}),
Want: cty.NullVal(cty.Object(map[string]cty.Type{
"foo": cty.Set(cty.Object(map[string]cty.Type{
"bar": cty.String,
})),
})),
},
} }
for _, test := range tests { for _, test := range tests {