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

Strip optional attributes from null objects during conversion #140

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cty/convert/conversion_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,19 @@ func conversionObjectToObject(in, out cty.Type, unsafe bool) conversion {
}
}

if val.IsNull() {
// Strip optional attributes out of the embedded type for null
// values.
val = cty.NullVal(val.Type().WithoutOptionalAttributesDeep())
}

attrVals[name] = val
}

for name := range outOptionals {
if _, exists := attrVals[name]; !exists {
wantTy := outAtys[name]
attrVals[name] = cty.NullVal(wantTy)
attrVals[name] = cty.NullVal(wantTy.WithoutOptionalAttributesDeep())
}
}

Expand Down
97 changes: 97 additions & 0 deletions cty/convert/public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,103 @@ func TestConvert(t *testing.T) {
}, []string{"b", "c"}),
WantError: `map element type is incompatible with attribute "c": object required`,
},
{
Value: cty.TupleVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"d": cty.NumberVal(big.NewFloat(10)),
"c": cty.ObjectVal(map[string]cty.Value{
"a": cty.StringVal("foo"),
"b": cty.BoolVal(true),
}),
}),
cty.ObjectVal(map[string]cty.Value{
"d": cty.NumberVal(big.NewFloat(5)),
"c": cty.NullVal(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"a": cty.String,
"b": cty.Bool,
}, []string{"b"})),
}),
}),
Type: cty.Set(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"c": cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"a": cty.String,
"b": cty.Bool,
}, []string{"b"}),
"d": cty.Number,
}, []string{"c"})),
Want: cty.SetVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"d": cty.NumberVal(big.NewFloat(10)),
"c": cty.ObjectVal(map[string]cty.Value{
"a": cty.StringVal("foo"),
"b": cty.BoolVal(true),
}),
}),
cty.ObjectVal(map[string]cty.Value{
"d": cty.NumberVal(big.NewFloat(5)),
"c": cty.NullVal(cty.Object(map[string]cty.Type{
"a": cty.String,
"b": cty.Bool,
})),
}),
}),
},
{
Value: cty.TupleVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"d": cty.NumberVal(big.NewFloat(10)),
"c": cty.ObjectVal(map[string]cty.Value{
"a": cty.StringVal("foo"),
"b": cty.BoolVal(true),
}),
}),
cty.ObjectVal(map[string]cty.Value{
"d": cty.NumberVal(big.NewFloat(5)),
}),
}),
Type: cty.Set(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"c": cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"a": cty.String,
"b": cty.Bool,
}, []string{"b"}),
"d": cty.Number,
}, []string{"c"})),
Want: cty.SetVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"d": cty.NumberVal(big.NewFloat(10)),
"c": cty.ObjectVal(map[string]cty.Value{
"a": cty.StringVal("foo"),
"b": cty.BoolVal(true),
}),
}),
cty.ObjectVal(map[string]cty.Value{
"d": cty.NumberVal(big.NewFloat(5)),
"c": cty.NullVal(cty.Object(map[string]cty.Type{
"a": cty.String,
"b": cty.Bool,
})),
}),
}),
},
{
Value: cty.MapVal(map[string]cty.Value{
"a": cty.StringVal("boop"),
}),
Type: cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"a": cty.String,
"b": cty.String,
"c": cty.Object(map[string]cty.Type{
"d": cty.String,
}),
}, []string{"b", "c"}),
Want: cty.ObjectVal(map[string]cty.Value{
"a": cty.StringVal("boop"),
"b": cty.NullVal(cty.String),
"c": cty.NullVal(cty.Object(map[string]cty.Type{
"d": cty.String,
})),
}),
},
{
Value: cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
Expand Down