Skip to content

Commit

Permalink
Merge pull request #555 from hashicorp/pre-convert-optional-defaults
Browse files Browse the repository at this point in the history
ext/typeexpr: Convert defaults for optional attrs
  • Loading branch information
alisdair committed Sep 23, 2022
2 parents 85e45c5 + b857322 commit 68124d5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion ext/typeexpr/get_type.go
Expand Up @@ -249,7 +249,7 @@ func getType(expr hcl.Expression, constraint, withDefaults bool) (cty.Type, *Def
// If a default is set for an optional attribute, verify that it is
// convertible to the attribute type.
if defaultVal, ok := defaultValues[attrName]; ok {
_, err := convert.Convert(defaultVal, aty)
convertedDefaultVal, err := convert.Convert(defaultVal, aty)
if err != nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Expand All @@ -258,6 +258,8 @@ func getType(expr hcl.Expression, constraint, withDefaults bool) (cty.Type, *Def
Subject: defaultExpr.Range().Ptr(),
})
delete(defaultValues, attrName)
} else {
defaultValues[attrName] = convertedDefaultVal
}
}

Expand Down
4 changes: 3 additions & 1 deletion ext/typeexpr/get_type_test.go
Expand Up @@ -504,7 +504,9 @@ func TestGetTypeDefaults(t *testing.T) {
}, []string{"b"}),
}, []string{"a"}),
DefaultValues: map[string]cty.Value{
"a": cty.EmptyObjectVal,
"a": cty.ObjectVal(map[string]cty.Value{
"b": cty.NullVal(cty.Number),
}),
},
Children: map[string]*Defaults{
"a": {
Expand Down

0 comments on commit 68124d5

Please sign in to comment.