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

Fix eagerAssociations with slice of pointers #391

Merged
merged 1 commit into from
Jun 4, 2019
Merged
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
15 changes: 11 additions & 4 deletions finders.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,25 @@ func (q *Query) eagerAssociations(model interface{}) error {

// eagerAssociations for a slice or array model passed as a param.
v := reflect.ValueOf(model)
if reflect.Indirect(v).Kind() == reflect.Slice ||
reflect.Indirect(v).Kind() == reflect.Array {
kind := reflect.Indirect(v).Kind()
if kind == reflect.Slice || kind == reflect.Array {
v = v.Elem()
for i := 0; i < v.Len(); i++ {
err = q.eagerAssociations(v.Index(i).Addr().Interface())
e := v.Index(i)
if e.Type().Kind() == reflect.Ptr {
// Already a pointer
err = q.eagerAssociations(e.Interface())
} else {
err = q.eagerAssociations(e.Addr().Interface())
}
if err != nil {
return err
}
}
return err
return nil
}

// eagerAssociations for a single element
assos, err := associations.ForStruct(model, q.eagerFields...)
if err != nil {
return errors.Wrap(err, "could not retrieve associations")
Expand Down