Skip to content

Commit

Permalink
Fix eagerAssociations with slice of pointers (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislas-m authored Jun 4, 2019
1 parent 96a54f4 commit 8f6a6c0
Showing 1 changed file with 11 additions and 4 deletions.
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

0 comments on commit 8f6a6c0

Please sign in to comment.