diff --git a/config/hcl2shim/flatmap.go b/config/hcl2shim/flatmap.go index 5a40b9ecc..6b9a52dbc 100644 --- a/config/hcl2shim/flatmap.go +++ b/config/hcl2shim/flatmap.go @@ -68,6 +68,10 @@ func flatmapValueFromHCL2Primitive(m map[string]string, key string, val cty.Valu } func flatmapValueFromHCL2Map(m map[string]string, prefix string, val cty.Value) { + if val.IsNull() { + // Omit entirely + return + } if !val.IsKnown() { switch { case val.Type().IsObjectType(): @@ -95,6 +99,10 @@ func flatmapValueFromHCL2Map(m map[string]string, prefix string, val cty.Value) } func flatmapValueFromHCL2Seq(m map[string]string, prefix string, val cty.Value) { + if val.IsNull() { + // Omit entirely + return + } if !val.IsKnown() { m[prefix+"#"] = UnknownVariableValue return diff --git a/config/hcl2shim/flatmap_test.go b/config/hcl2shim/flatmap_test.go index abad07e6a..affd8dbf5 100644 --- a/config/hcl2shim/flatmap_test.go +++ b/config/hcl2shim/flatmap_test.go @@ -249,6 +249,73 @@ func TestFlatmapValueFromHCL2(t *testing.T) { } } +func TestFlatmapValueFromHCL2FromFlatmap(t *testing.T) { + tests := []struct { + Name string + Map map[string]string + Type cty.Type + }{ + { + "empty flatmap with collections", + map[string]string{}, + cty.Object(map[string]cty.Type{ + "foo": cty.Map(cty.String), + "bar": cty.Set(cty.String), + }), + }, + { + "nil flatmap with collections", + nil, + cty.Object(map[string]cty.Type{ + "foo": cty.Map(cty.String), + "bar": cty.Set(cty.String), + }), + }, + { + "empty flatmap with nested collections", + map[string]string{}, + cty.Object(map[string]cty.Type{ + "foo": cty.Object( + map[string]cty.Type{ + "baz": cty.Map(cty.String), + }, + ), + "bar": cty.Set(cty.String), + }), + }, + { + "partial flatmap with nested collections", + map[string]string{ + "foo.baz.%": "1", + "foo.baz.key": "val", + }, + cty.Object(map[string]cty.Type{ + "foo": cty.Object( + map[string]cty.Type{ + "baz": cty.Map(cty.String), + "biz": cty.Map(cty.String), + }, + ), + "bar": cty.Set(cty.String), + }), + }, + } + + for _, test := range tests { + t.Run(test.Name, func(t *testing.T) { + val, err := HCL2ValueFromFlatmap(test.Map, test.Type) + if err != nil { + t.Fatal(err) + } + + got := FlatmapValueFromHCL2(val) + + for _, problem := range deep.Equal(got, test.Map) { + t.Error(problem) + } + }) + } +} func TestHCL2ValueFromFlatmap(t *testing.T) { tests := []struct { Flatmap map[string]string