Skip to content

Commit

Permalink
Add support of preload gorm option (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilev-ms authored and Daniil Kukharau committed Sep 20, 2018
1 parent ce4cb89 commit d3c2230
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
3 changes: 3 additions & 0 deletions gorm/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ fields:
}
}
if isModel(fType) {
if ok, flag := gormTag(&sf, "preload"); ok && flag == "false" {
continue
}
subPreload, err := preloadEverything(fType, append(path, objType))
if err != nil {
return nil, err
Expand Down
17 changes: 12 additions & 5 deletions gorm/fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
)

type Model struct {
Property string
SubModel SubModel
SubModels []SubModel
CycleModel *CycleModel
Property string
SubModel SubModel
SubModels []SubModel
CycleModel *CycleModel
NotPreloadObj SubModel `gorm:"preload:false"`
PreloadObj SubModel `gorm:"preload:true"`
}

type CycleModel struct {
Expand Down Expand Up @@ -69,9 +71,14 @@ func TestGormFieldSelection(t *testing.T) {
nil,
false,
},
{
"not_preload_obj,preload_obj",
[]string{"NotPreloadObj", "PreloadObj"},
false,
},
{
"",
[]string{"SubModel.SubSubModel", "SubModel", "SubModels.SubSubModel", "SubModels", "CycleModel"},
[]string{"SubModel.SubSubModel", "SubModel", "SubModels.SubSubModel", "SubModels", "CycleModel", "PreloadObj.SubSubModel", "PreloadObj"},
false,
},
}
Expand Down

0 comments on commit d3c2230

Please sign in to comment.