Rename Loaded -> R
This commit is contained in:
parent
dc49fa4d59
commit
f489e22ab4
8 changed files with 38 additions and 38 deletions
|
@ -95,7 +95,7 @@ users, err := models.Users(db, Load("FavoriteMovies"))
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(len(users.Loaded.FavoriteMovies))
|
||||
fmt.Println(len(users.R.FavoriteMovies))
|
||||
```
|
||||
|
||||
## How to boil your database
|
||||
|
|
|
@ -110,7 +110,7 @@ func (q *Query) BindFast(obj interface{}, titleCases map[string]string) error {
|
|||
// loadRelationships dynamically calls the template generated eager load
|
||||
// functions of the form:
|
||||
//
|
||||
// func (t *TableLoaded) LoadRelationshipName(exec Executor, singular bool, obj interface{})
|
||||
// func (t *TableR) LoadRelationshipName(exec Executor, singular bool, obj interface{})
|
||||
//
|
||||
// The arguments to this function are:
|
||||
// - t is not considered here, and is always passed nil. The function exists on a loaded
|
||||
|
@ -125,11 +125,11 @@ func (q *Query) loadRelationships(obj interface{}, singular bool) error {
|
|||
typ = typ.Elem().Elem()
|
||||
}
|
||||
|
||||
rel, found := typ.FieldByName("Loaded")
|
||||
rel, found := typ.FieldByName("R")
|
||||
// If the users object has no loaded struct, it must be
|
||||
// a custom object and we should not attempt to load any relationships.
|
||||
if !found {
|
||||
return errors.New("load query mod was used but bound struct contained no Loaded field")
|
||||
return errors.New("load query mod was used but bound struct contained no R field")
|
||||
}
|
||||
|
||||
for _, relationship := range q.load {
|
||||
|
|
|
@ -166,9 +166,9 @@ func TestBindSingular(t *testing.T) {
|
|||
|
||||
var loadFunctionCalled bool
|
||||
|
||||
type testLoadedStruct struct{}
|
||||
type testRStruct struct{}
|
||||
|
||||
func (r *testLoadedStruct) LoadTestOne(exec Executor, singular bool, obj interface{}) error {
|
||||
func (r *testRStruct) LoadTestOne(exec Executor, singular bool, obj interface{}) error {
|
||||
loadFunctionCalled = true
|
||||
return nil
|
||||
}
|
||||
|
@ -179,7 +179,7 @@ func TestLoadRelationshipsSlice(t *testing.T) {
|
|||
|
||||
testSlice := []*struct {
|
||||
ID int
|
||||
Loaded *testLoadedStruct
|
||||
R *testRStruct
|
||||
}{}
|
||||
|
||||
q := Query{load: []string{"TestOne"}, executor: nil}
|
||||
|
@ -198,7 +198,7 @@ func TestLoadRelationshipsSingular(t *testing.T) {
|
|||
|
||||
testSingular := struct {
|
||||
ID int
|
||||
Loaded *testLoadedStruct
|
||||
R *testRStruct
|
||||
}{}
|
||||
|
||||
q := Query{load: []string{"TestOne"}, executor: nil}
|
||||
|
|
|
@ -11,15 +11,15 @@ type {{$modelName}} struct {
|
|||
{{end -}}
|
||||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else}}
|
||||
Loaded *{{$modelName}}Loaded `boil:"-" json:"-" toml:"-" yaml:"-"`
|
||||
R *{{$modelName}}R `boil:"-" json:"-" toml:"-" yaml:"-"`
|
||||
{{end -}}
|
||||
}
|
||||
|
||||
{{- $dot := . -}}
|
||||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else}}
|
||||
// {{$modelName}}Loaded are where relationships are eagerly loaded.
|
||||
type {{$modelName}}Loaded struct {
|
||||
// {{$modelName}}R is where relationships are stored.
|
||||
type {{$modelName}}R struct {
|
||||
{{range .Table.FKeys -}}
|
||||
{{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
|
||||
{{- template "relationship_to_one_struct_helper" $rel}}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
{{- $slice := printf "%sSlice" .LocalTable.NameGo -}}
|
||||
// Load{{.Function.Name}} allows an eager lookup of values, cached into the
|
||||
// loaded structs of the objects.
|
||||
func (r *{{.LocalTable.NameGo}}Loaded) Load{{.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error {
|
||||
func (r *{{.LocalTable.NameGo}}R) Load{{.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error {
|
||||
var slice []*{{.LocalTable.NameGo}}
|
||||
var object *{{.LocalTable.NameGo}}
|
||||
|
||||
|
@ -45,20 +45,20 @@ func (r *{{.LocalTable.NameGo}}Loaded) Load{{.Function.Name}}(e boil.Executor, s
|
|||
}
|
||||
|
||||
if singular && len(resultSlice) != 0 {
|
||||
if object.Loaded == nil {
|
||||
object.Loaded = &{{.LocalTable.NameGo}}Loaded{}
|
||||
if object.R == nil {
|
||||
object.R = &{{.LocalTable.NameGo}}R{}
|
||||
}
|
||||
object.Loaded.{{.Function.Name}} = resultSlice[0]
|
||||
object.R.{{.Function.Name}} = resultSlice[0]
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, foreign := range resultSlice {
|
||||
for _, local := range slice {
|
||||
if local.{{.Function.LocalAssignment}} == foreign.{{.Function.ForeignAssignment}} {
|
||||
if local.Loaded == nil {
|
||||
local.Loaded = &{{.LocalTable.NameGo}}Loaded{}
|
||||
if local.R == nil {
|
||||
local.R = &{{.LocalTable.NameGo}}R{}
|
||||
}
|
||||
local.Loaded.{{.Function.Name}} = foreign
|
||||
local.R.{{.Function.Name}} = foreign
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
{{- $slice := printf "%sSlice" $rel.LocalTable.NameGo -}}
|
||||
// Load{{$rel.Function.Name}} allows an eager lookup of values, cached into the
|
||||
// loaded structs of the objects.
|
||||
func (r *{{$rel.LocalTable.NameGo}}Loaded) Load{{$rel.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error {
|
||||
func (r *{{$rel.LocalTable.NameGo}}R) Load{{$rel.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error {
|
||||
var slice []*{{$rel.LocalTable.NameGo}}
|
||||
var object *{{$rel.LocalTable.NameGo}}
|
||||
|
||||
|
@ -82,10 +82,10 @@ func (r *{{$rel.LocalTable.NameGo}}Loaded) Load{{$rel.Function.Name}}(e boil.Exe
|
|||
{{end}}
|
||||
|
||||
if singular {
|
||||
if object.Loaded == nil {
|
||||
object.Loaded = &{{$rel.LocalTable.NameGo}}Loaded{}
|
||||
if object.R == nil {
|
||||
object.R = &{{$rel.LocalTable.NameGo}}R{}
|
||||
}
|
||||
object.Loaded.{{$rel.Function.Name}} = resultSlice
|
||||
object.R.{{$rel.Function.Name}} = resultSlice
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -94,10 +94,10 @@ func (r *{{$rel.LocalTable.NameGo}}Loaded) Load{{$rel.Function.Name}}(e boil.Exe
|
|||
localJoinCol := localJoinCols[i]
|
||||
for _, local := range slice {
|
||||
if local.{{$rel.Function.LocalAssignment}} == localJoinCol {
|
||||
if local.Loaded == nil {
|
||||
local.Loaded = &{{$rel.LocalTable.NameGo}}Loaded{}
|
||||
if local.R == nil {
|
||||
local.R = &{{$rel.LocalTable.NameGo}}R{}
|
||||
}
|
||||
local.Loaded.{{$rel.Function.Name}} = append(local.Loaded.{{$rel.Function.Name}}, foreign)
|
||||
local.R.{{$rel.Function.Name}} = append(local.R.{{$rel.Function.Name}}, foreign)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -106,10 +106,10 @@ func (r *{{$rel.LocalTable.NameGo}}Loaded) Load{{$rel.Function.Name}}(e boil.Exe
|
|||
for _, foreign := range resultSlice {
|
||||
for _, local := range slice {
|
||||
if local.{{$rel.Function.LocalAssignment}} == foreign.{{$rel.Function.ForeignAssignment}} {
|
||||
if local.Loaded == nil {
|
||||
local.Loaded = &{{$rel.LocalTable.NameGo}}Loaded{}
|
||||
if local.R == nil {
|
||||
local.R = &{{$rel.LocalTable.NameGo}}R{}
|
||||
}
|
||||
local.Loaded.{{$rel.Function.Name}} = append(local.Loaded.{{$rel.Function.Name}}, foreign)
|
||||
local.R.{{$rel.Function.Name}} = append(local.R.{{$rel.Function.Name}}, foreign)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
|
@ -75,18 +75,18 @@ func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
|
|||
}
|
||||
|
||||
slice := {{$rel.LocalTable.NameGo}}Slice{&a}
|
||||
if err = a.Loaded.Load{{$rel.Function.Name}}(tx, false, &slice); err != nil {
|
||||
if err = a.R.Load{{$rel.Function.Name}}(tx, false, &slice); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := len(a.Loaded.{{$rel.Function.Name}}); got != 2 {
|
||||
if got := len(a.R.{{$rel.Function.Name}}); got != 2 {
|
||||
t.Error("number of eager loaded records wrong, got:", got)
|
||||
}
|
||||
|
||||
a.Loaded.{{$rel.Function.Name}} = nil
|
||||
if err = a.Loaded.Load{{$rel.Function.Name}}(tx, true, &a); err != nil {
|
||||
a.R.{{$rel.Function.Name}} = nil
|
||||
if err = a.R.Load{{$rel.Function.Name}}(tx, true, &a); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := len(a.Loaded.{{$rel.Function.Name}}); got != 2 {
|
||||
if got := len(a.R.{{$rel.Function.Name}}); got != 2 {
|
||||
t.Error("number of eager loaded records wrong, got:", got)
|
||||
}
|
||||
|
||||
|
|
|
@ -42,18 +42,18 @@ func test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(
|
|||
}
|
||||
|
||||
slice := {{.LocalTable.NameGo}}Slice{&local}
|
||||
if err = local.Loaded.Load{{.Function.Name}}(tx, false, &slice); err != nil {
|
||||
if err = local.R.Load{{.Function.Name}}(tx, false, &slice); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if local.Loaded.{{.Function.Name}} == nil {
|
||||
if local.R.{{.Function.Name}} == nil {
|
||||
t.Error("struct should have been eager loaded")
|
||||
}
|
||||
|
||||
local.Loaded.{{.Function.Name}} = nil
|
||||
if err = local.Loaded.Load{{.Function.Name}}(tx, true, &local); err != nil {
|
||||
local.R.{{.Function.Name}} = nil
|
||||
if err = local.R.Load{{.Function.Name}}(tx, true, &local); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if local.Loaded.{{.Function.Name}} == nil {
|
||||
if local.R.{{.Function.Name}} == nil {
|
||||
t.Error("struct should have been eager loaded")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue