Skip to content

Commit

Permalink
convert: Don't produce list and set type constraints with nested opti…
Browse files Browse the repository at this point in the history
…onal attributes

Optional attributes are only for type conversion targets and are not valid in general type constraints.
  • Loading branch information
liamcervante authored Oct 10, 2022
1 parent be97162 commit 213b8de
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 1.11.1 (Unreleased)

* `convert`: Fix for error when converting empty sets and lists with nested optional attributes by explicitly removing optional attribute information from collections.

# 1.11.0 (August 22, 2022)

Expand Down
4 changes: 2 additions & 2 deletions cty/convert/conversion_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func conversionCollectionToList(ety cty.Type, conv conversion) conversion {
if ety == cty.DynamicPseudoType {
return cty.ListValEmpty(val.Type().ElementType()), nil
}
return cty.ListValEmpty(ety), nil
return cty.ListValEmpty(ety.WithoutOptionalAttributesDeep()), nil
}

if !cty.CanListVal(elems) {
Expand Down Expand Up @@ -99,7 +99,7 @@ func conversionCollectionToSet(ety cty.Type, conv conversion) conversion {
if ety == cty.DynamicPseudoType {
return cty.SetValEmpty(val.Type().ElementType()), nil
}
return cty.SetValEmpty(ety), nil
return cty.SetValEmpty(ety.WithoutOptionalAttributesDeep()), nil
}

if !cty.CanSetVal(elems) {
Expand Down
109 changes: 109 additions & 0 deletions cty/convert/public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package convert

import (
"fmt"
"math/big"
"testing"

"github.com/zclconf/go-cty/cty"
Expand Down Expand Up @@ -990,6 +991,114 @@ func TestConvert(t *testing.T) {
}),
WantError: false,
},
{
Value: cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"xs": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"x": cty.NumberVal(big.NewFloat(1234)),
}),
}),
}),
cty.ObjectVal(map[string]cty.Value{
"xs": cty.ListValEmpty(cty.Object(map[string]cty.Type{
"x": cty.Number,
})),
})},
),
Type: cty.List(cty.Object(map[string]cty.Type{
"xs": cty.List(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"x": cty.Number,
}, []string{"x"})),
})),
Want: cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"xs": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"x": cty.NumberVal(big.NewFloat(1234)),
}),
}),
}),
cty.ObjectVal(map[string]cty.Value{
"xs": cty.ListValEmpty(cty.Object(map[string]cty.Type{
"x": cty.Number,
})),
})},
),
WantError: false,
},
{
Value: cty.SetVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"xs": cty.SetVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"x": cty.NumberVal(big.NewFloat(1234)),
}),
}),
}),
cty.ObjectVal(map[string]cty.Value{
"xs": cty.SetValEmpty(cty.Object(map[string]cty.Type{
"x": cty.Number,
})),
})},
),
Type: cty.Set(cty.Object(map[string]cty.Type{
"xs": cty.Set(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"x": cty.Number,
}, []string{"x"})),
})),
Want: cty.SetVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"xs": cty.SetVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"x": cty.NumberVal(big.NewFloat(1234)),
}),
}),
}),
cty.ObjectVal(map[string]cty.Value{
"xs": cty.SetValEmpty(cty.Object(map[string]cty.Type{
"x": cty.Number,
})),
})},
),
WantError: false,
},
{
Value: cty.MapVal(map[string]cty.Value{
"foo": cty.ObjectVal(map[string]cty.Value{
"xs": cty.MapVal(map[string]cty.Value{
"nested_foo": cty.ObjectVal(map[string]cty.Value{
"x": cty.NumberVal(big.NewFloat(1234)),
}),
}),
}),
"bar": cty.ObjectVal(map[string]cty.Value{
"xs": cty.MapValEmpty(cty.Object(map[string]cty.Type{
"x": cty.Number,
})),
})},
),
Type: cty.Map(cty.Object(map[string]cty.Type{
"xs": cty.Map(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"x": cty.Number,
}, []string{"x"})),
})),
Want: cty.MapVal(map[string]cty.Value{
"foo": cty.ObjectVal(map[string]cty.Value{
"xs": cty.MapVal(map[string]cty.Value{
"nested_foo": cty.ObjectVal(map[string]cty.Value{
"x": cty.NumberVal(big.NewFloat(1234)),
}),
}),
}),
"bar": cty.ObjectVal(map[string]cty.Value{
"xs": cty.MapValEmpty(cty.Object(map[string]cty.Type{
"x": cty.Number,
})),
})},
),
WantError: false,
},
}

for _, test := range tests {
Expand Down

0 comments on commit 213b8de

Please sign in to comment.