Skip to content

Commit

Permalink
convert: strip optional attributes out of empty maps
Browse files Browse the repository at this point in the history
  • Loading branch information
liamcervante authored and apparentlymart committed Jan 2, 2025
1 parent a3f6631 commit 86acbd7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cty/convert/conversion_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func conversionCollectionToMap(ety cty.Type, conv conversion) conversion {
if ety == cty.DynamicPseudoType {
return cty.MapValEmpty(val.Type().ElementType()), nil
}
return cty.MapValEmpty(ety), nil
return cty.MapValEmpty(ety.WithoutOptionalAttributesDeep()), nil
}

if ety.IsCollectionType() || ety.IsObjectType() {
Expand Down
30 changes: 30 additions & 0 deletions cty/convert/public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1794,6 +1794,36 @@ func TestConvert(t *testing.T) {
})),
}),
},

{
Value: cty.TupleVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"optional_map": cty.EmptyObjectVal,
}),
cty.ObjectVal(map[string]cty.Value{
"optional_map": cty.MapValEmpty(cty.Object(map[string]cty.Type{
"asdf": cty.String,
})),
}),
}),
Type: cty.Set(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"optional_map": cty.Map(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"asdf": cty.String,
}, []string{"asdf"})),
}, []string{"optional_map"})),
Want: cty.SetVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"optional_map": cty.MapValEmpty(cty.Object(map[string]cty.Type{
"asdf": cty.String,
})),
}),
cty.ObjectVal(map[string]cty.Value{
"optional_map": cty.MapValEmpty(cty.Object(map[string]cty.Type{
"asdf": cty.String,
})),
}),
}),
},
}

for _, test := range tests {
Expand Down

0 comments on commit 86acbd7

Please sign in to comment.