From f05a6b37949f3a28f07debeaa25c14792323c7a8 Mon Sep 17 00:00:00 2001 From: crystalin Date: Fri, 5 Jun 2015 12:54:52 +0200 Subject: [PATCH] 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 { ... } --- preload.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/preload.go b/preload.go index add077ab..f1c0fae5 100644 --- a/preload.go +++ b/preload.go @@ -216,13 +216,16 @@ func (scope *Scope) getColumnsAsScope(column string) *Scope { } fieldStruct, _ := modelType.FieldByName(column) 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() } else { columns = reflect.New(reflect.SliceOf(reflect.PtrTo(fieldStruct.Type))).Elem() } for i := 0; i < values.Len(); i++ { column := reflect.Indirect(values.Index(i)).FieldByName(column) + if column.Kind() == reflect.Ptr { + column = column.Elem() + } if column.Kind() == reflect.Slice { for i := 0; i < column.Len(); i++ { columns = reflect.Append(columns, column.Index(i).Addr())