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

StructScan check interface parameter is a pointer to a struct #487

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
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