Support for preload of Struct Ptr

This fixes the issue when preloading .Preload("Project.Repositories").Find(&[]User{})
with 
type User struct {
  Project *Project
}

type Project struct {
  Repositories []Repository
}

type Repository struct {
...
}
This commit is contained in:
crystalin 2015-06-05 12:54:52 +02:00
parent 0b8c9f29a9
commit f05a6b3794
1 changed files with 4 additions and 1 deletions

View File

@ -216,13 +216,16 @@ func (scope *Scope) getColumnsAsScope(column string) *Scope {
} }
fieldStruct, _ := modelType.FieldByName(column) fieldStruct, _ := modelType.FieldByName(column)
var columns reflect.Value var columns reflect.Value
if fieldStruct.Type.Kind() == reflect.Slice { if fieldStruct.Type.Kind() == reflect.Slice || fieldStruct.Type.Kind() == reflect.Ptr {
columns = reflect.New(reflect.SliceOf(reflect.PtrTo(fieldStruct.Type.Elem()))).Elem() columns = reflect.New(reflect.SliceOf(reflect.PtrTo(fieldStruct.Type.Elem()))).Elem()
} else { } else {
columns = reflect.New(reflect.SliceOf(reflect.PtrTo(fieldStruct.Type))).Elem() columns = reflect.New(reflect.SliceOf(reflect.PtrTo(fieldStruct.Type))).Elem()
} }
for i := 0; i < values.Len(); i++ { for i := 0; i < values.Len(); i++ {
column := reflect.Indirect(values.Index(i)).FieldByName(column) column := reflect.Indirect(values.Index(i)).FieldByName(column)
if column.Kind() == reflect.Ptr {
column = column.Elem()
}
if column.Kind() == reflect.Slice { if column.Kind() == reflect.Slice {
for i := 0; i < column.Len(); i++ { for i := 0; i < column.Len(); i++ {
columns = reflect.Append(columns, column.Index(i).Addr()) columns = reflect.Append(columns, column.Index(i).Addr())