Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check empty collections for optional attributes #143

Merged
merged 1 commit into from Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions cty/convert/conversion_collection.go
Expand Up @@ -190,7 +190,7 @@ func conversionTupleToSet(tupleType cty.Type, setEty cty.Type, unsafe bool) conv
if len(tupleEtys) == 0 {
// Empty tuple short-circuit
return func(val cty.Value, path cty.Path) (cty.Value, error) {
return cty.SetValEmpty(setEty), nil
return cty.SetValEmpty(setEty.WithoutOptionalAttributesDeep()), nil
}
}

Expand Down Expand Up @@ -280,7 +280,7 @@ func conversionTupleToList(tupleType cty.Type, listEty cty.Type, unsafe bool) co
if len(tupleEtys) == 0 {
// Empty tuple short-circuit
return func(val cty.Value, path cty.Path) (cty.Value, error) {
return cty.ListValEmpty(listEty), nil
return cty.ListValEmpty(listEty.WithoutOptionalAttributesDeep()), nil
}
}

Expand Down Expand Up @@ -372,7 +372,7 @@ func conversionObjectToMap(objectType cty.Type, mapEty cty.Type, unsafe bool) co
if len(objectAtys) == 0 {
// Empty object short-circuit
return func(val cty.Value, path cty.Path) (cty.Value, error) {
return cty.MapValEmpty(mapEty), nil
return cty.MapValEmpty(mapEty.WithoutOptionalAttributesDeep()), nil
}
}

Expand Down
120 changes: 120 additions & 0 deletions cty/convert/public_test.go
Expand Up @@ -1268,6 +1268,126 @@ func TestConvert(t *testing.T) {
})},
),
},
// We should strip optional attributes out of empty sets, maps, lists,
// and tuples.
{
Value: cty.ListValEmpty(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"a": cty.String,
}, []string{"a"})),
Type: cty.Set(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"a": cty.String,
}, []string{"a"})),
Want: cty.SetValEmpty(cty.Object(map[string]cty.Type{
"a": cty.String,
})),
},
{
Value: cty.EmptyTupleVal,
Type: cty.Set(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"a": cty.String,
}, []string{"a"})),
Want: cty.SetValEmpty(cty.Object(map[string]cty.Type{
"a": cty.String,
})),
},
{
Value: cty.SetValEmpty(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"a": cty.String,
}, []string{"a"})),
Type: cty.List(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"a": cty.String,
}, []string{"a"})),
Want: cty.ListValEmpty(cty.Object(map[string]cty.Type{
"a": cty.String,
})),
},
{
Value: cty.EmptyTupleVal,
Type: cty.List(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"a": cty.String,
}, []string{"a"})),
Want: cty.ListValEmpty(cty.Object(map[string]cty.Type{
"a": cty.String,
})),
},
{
Value: cty.EmptyObjectVal,
Type: cty.Map(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"a": cty.String,
}, []string{"a"})),
Want: cty.MapValEmpty(cty.Object(map[string]cty.Type{
"a": cty.String,
})),
},
{
Value: cty.MapValEmpty(cty.String),
Type: cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"a": cty.String,
}, []string{"a"}),
Want: cty.ObjectVal(map[string]cty.Value{
"a": cty.NullVal(cty.String),
}),
},
// We should strip optional attributes out of null sets, maps, lists,
// and tuples.
{
Value: cty.NullVal(cty.List(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"a": cty.String,
}, []string{"a"}))),
Type: cty.Set(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"a": cty.String,
}, []string{"a"})),
Want: cty.NullVal(cty.Set(cty.Object(map[string]cty.Type{
"a": cty.String,
}))),
},
{
Value: cty.NullVal(cty.EmptyTuple),
Type: cty.Set(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"a": cty.String,
}, []string{"a"})),
Want: cty.NullVal(cty.Set(cty.Object(map[string]cty.Type{
"a": cty.String,
}))),
},
{
Value: cty.NullVal(cty.Set(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"a": cty.String,
}, []string{"a"}))),
Type: cty.List(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"a": cty.String,
}, []string{"a"})),
Want: cty.NullVal(cty.List(cty.Object(map[string]cty.Type{
"a": cty.String,
}))),
},
{
Value: cty.NullVal(cty.EmptyTuple),
Type: cty.List(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"a": cty.String,
}, []string{"a"})),
Want: cty.NullVal(cty.List(cty.Object(map[string]cty.Type{
"a": cty.String,
}))),
},
{
Value: cty.NullVal(cty.EmptyObject),
Type: cty.Map(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"a": cty.String,
}, []string{"a"})),
Want: cty.NullVal(cty.Map(cty.Object(map[string]cty.Type{
"a": cty.String,
}))),
},
{
Value: cty.NullVal(cty.Map(cty.String)),
Type: cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"a": cty.String,
}, []string{"a"}),
Want: cty.NullVal(cty.Object(map[string]cty.Type{
"a": cty.String,
})),
},
// We should strip optional attributes out of null values in sets, maps,
// lists and tuples.
{
Expand Down