Skip to content

Commit

Permalink
schemahcl: handle null values as omission
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Oct 5, 2024
1 parent 0d53941 commit a34f15b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions schemahcl/schemahcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,10 @@ func (s *State) toAttrs(ctx *hcl.EvalContext, vr SchemaValidator, hclAttrs hclsy
if diag.HasErrors() {
return nil, s.typeError(diag, scope)
}
// Setting an attribute as null means omission.
if value.IsNull() {
continue
}
if err := vr.ValidateAttribute(ctx, hclAttr, value); err != nil {
return nil, err
}
Expand Down
12 changes: 12 additions & 0 deletions schemahcl/schemahcl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ vars = {
marshal, err := Marshal(&test)
require.NoError(t, err)
require.EqualValues(t, f, string(marshal))

var v struct {
NullV string `spec:"null_v"`
NullP *string `spec:"null_p"`
}
err = New().EvalBytes([]byte(`
null_v = null
null_p = null
`), &v, nil)
require.NoError(t, err)
require.Empty(t, v.NullV)
require.Nil(t, v.NullP)
}

func TestResource(t *testing.T) {
Expand Down

0 comments on commit a34f15b

Please sign in to comment.