Skip to content

Commit

Permalink
document: Added check that an interface parameter is a pointer to a s…
Browse files Browse the repository at this point in the history
…tructure in StructScan (#487)
  • Loading branch information
abramlab committed Oct 29, 2023
1 parent d5bccbd commit cd14cd2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions document/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func StructScan(d types.Document, t interface{}) error {
return errors.New("target must be pointer to a valid Go type")
}

if ref.Elem().Kind() != reflect.Struct {
return errors.New("target must be pointer to a struct")
}

if ref.IsNil() {
ref.Set(reflect.New(ref.Type().Elem()))
}
Expand Down
7 changes: 7 additions & 0 deletions document/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,13 @@ func TestScan(t *testing.T) {
assert.NoError(t, err)
require.Equal(t, &foo{A: &bar{B: 10}}, &f)
})

t.Run("Pointer not to struct", func(t *testing.T) {
var b int
d := document.NewFieldBuffer().Add("a", types.NewIntegerValue(10))
err := document.StructScan(d, &b)
assert.Error(t, err)
})
}

type documentScanner struct {
Expand Down

0 comments on commit cd14cd2

Please sign in to comment.