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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Println(len(users.Loaded.FavoriteMovies))
|
fmt.Println(len(users.R.FavoriteMovies))
|
||||||
```
|
```
|
||||||
|
|
||||||
## How to boil your database
|
## 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
|
// loadRelationships dynamically calls the template generated eager load
|
||||||
// functions of the form:
|
// 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:
|
// The arguments to this function are:
|
||||||
// - t is not considered here, and is always passed nil. The function exists on a loaded
|
// - 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()
|
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
|
// If the users object has no loaded struct, it must be
|
||||||
// a custom object and we should not attempt to load any relationships.
|
// a custom object and we should not attempt to load any relationships.
|
||||||
if !found {
|
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 {
|
for _, relationship := range q.load {
|
||||||
|
|
|
@ -166,9 +166,9 @@ func TestBindSingular(t *testing.T) {
|
||||||
|
|
||||||
var loadFunctionCalled bool
|
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
|
loadFunctionCalled = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -179,7 +179,7 @@ func TestLoadRelationshipsSlice(t *testing.T) {
|
||||||
|
|
||||||
testSlice := []*struct {
|
testSlice := []*struct {
|
||||||
ID int
|
ID int
|
||||||
Loaded *testLoadedStruct
|
R *testRStruct
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
q := Query{load: []string{"TestOne"}, executor: nil}
|
q := Query{load: []string{"TestOne"}, executor: nil}
|
||||||
|
@ -198,7 +198,7 @@ func TestLoadRelationshipsSingular(t *testing.T) {
|
||||||
|
|
||||||
testSingular := struct {
|
testSingular := struct {
|
||||||
ID int
|
ID int
|
||||||
Loaded *testLoadedStruct
|
R *testRStruct
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
q := Query{load: []string{"TestOne"}, executor: nil}
|
q := Query{load: []string{"TestOne"}, executor: nil}
|
||||||
|
|
|
@ -11,15 +11,15 @@ type {{$modelName}} struct {
|
||||||
{{end -}}
|
{{end -}}
|
||||||
{{- if .Table.IsJoinTable -}}
|
{{- if .Table.IsJoinTable -}}
|
||||||
{{- else}}
|
{{- else}}
|
||||||
Loaded *{{$modelName}}Loaded `boil:"-" json:"-" toml:"-" yaml:"-"`
|
R *{{$modelName}}R `boil:"-" json:"-" toml:"-" yaml:"-"`
|
||||||
{{end -}}
|
{{end -}}
|
||||||
}
|
}
|
||||||
|
|
||||||
{{- $dot := . -}}
|
{{- $dot := . -}}
|
||||||
{{- if .Table.IsJoinTable -}}
|
{{- if .Table.IsJoinTable -}}
|
||||||
{{- else}}
|
{{- else}}
|
||||||
// {{$modelName}}Loaded are where relationships are eagerly loaded.
|
// {{$modelName}}R is where relationships are stored.
|
||||||
type {{$modelName}}Loaded struct {
|
type {{$modelName}}R struct {
|
||||||
{{range .Table.FKeys -}}
|
{{range .Table.FKeys -}}
|
||||||
{{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
|
{{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
|
||||||
{{- template "relationship_to_one_struct_helper" $rel}}
|
{{- template "relationship_to_one_struct_helper" $rel}}
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
{{- $slice := printf "%sSlice" .LocalTable.NameGo -}}
|
{{- $slice := printf "%sSlice" .LocalTable.NameGo -}}
|
||||||
// Load{{.Function.Name}} allows an eager lookup of values, cached into the
|
// Load{{.Function.Name}} allows an eager lookup of values, cached into the
|
||||||
// loaded structs of the objects.
|
// 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 slice []*{{.LocalTable.NameGo}}
|
||||||
var object *{{.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 singular && len(resultSlice) != 0 {
|
||||||
if object.Loaded == nil {
|
if object.R == nil {
|
||||||
object.Loaded = &{{.LocalTable.NameGo}}Loaded{}
|
object.R = &{{.LocalTable.NameGo}}R{}
|
||||||
}
|
}
|
||||||
object.Loaded.{{.Function.Name}} = resultSlice[0]
|
object.R.{{.Function.Name}} = resultSlice[0]
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, foreign := range resultSlice {
|
for _, foreign := range resultSlice {
|
||||||
for _, local := range slice {
|
for _, local := range slice {
|
||||||
if local.{{.Function.LocalAssignment}} == foreign.{{.Function.ForeignAssignment}} {
|
if local.{{.Function.LocalAssignment}} == foreign.{{.Function.ForeignAssignment}} {
|
||||||
if local.Loaded == nil {
|
if local.R == nil {
|
||||||
local.Loaded = &{{.LocalTable.NameGo}}Loaded{}
|
local.R = &{{.LocalTable.NameGo}}R{}
|
||||||
}
|
}
|
||||||
local.Loaded.{{.Function.Name}} = foreign
|
local.R.{{.Function.Name}} = foreign
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
{{- $slice := printf "%sSlice" $rel.LocalTable.NameGo -}}
|
{{- $slice := printf "%sSlice" $rel.LocalTable.NameGo -}}
|
||||||
// Load{{$rel.Function.Name}} allows an eager lookup of values, cached into the
|
// Load{{$rel.Function.Name}} allows an eager lookup of values, cached into the
|
||||||
// loaded structs of the objects.
|
// 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 slice []*{{$rel.LocalTable.NameGo}}
|
||||||
var object *{{$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}}
|
{{end}}
|
||||||
|
|
||||||
if singular {
|
if singular {
|
||||||
if object.Loaded == nil {
|
if object.R == nil {
|
||||||
object.Loaded = &{{$rel.LocalTable.NameGo}}Loaded{}
|
object.R = &{{$rel.LocalTable.NameGo}}R{}
|
||||||
}
|
}
|
||||||
object.Loaded.{{$rel.Function.Name}} = resultSlice
|
object.R.{{$rel.Function.Name}} = resultSlice
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,10 +94,10 @@ func (r *{{$rel.LocalTable.NameGo}}Loaded) Load{{$rel.Function.Name}}(e boil.Exe
|
||||||
localJoinCol := localJoinCols[i]
|
localJoinCol := localJoinCols[i]
|
||||||
for _, local := range slice {
|
for _, local := range slice {
|
||||||
if local.{{$rel.Function.LocalAssignment}} == localJoinCol {
|
if local.{{$rel.Function.LocalAssignment}} == localJoinCol {
|
||||||
if local.Loaded == nil {
|
if local.R == nil {
|
||||||
local.Loaded = &{{$rel.LocalTable.NameGo}}Loaded{}
|
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
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,10 +106,10 @@ func (r *{{$rel.LocalTable.NameGo}}Loaded) Load{{$rel.Function.Name}}(e boil.Exe
|
||||||
for _, foreign := range resultSlice {
|
for _, foreign := range resultSlice {
|
||||||
for _, local := range slice {
|
for _, local := range slice {
|
||||||
if local.{{$rel.Function.LocalAssignment}} == foreign.{{$rel.Function.ForeignAssignment}} {
|
if local.{{$rel.Function.LocalAssignment}} == foreign.{{$rel.Function.ForeignAssignment}} {
|
||||||
if local.Loaded == nil {
|
if local.R == nil {
|
||||||
local.Loaded = &{{$rel.LocalTable.NameGo}}Loaded{}
|
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
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,18 +75,18 @@ func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
slice := {{$rel.LocalTable.NameGo}}Slice{&a}
|
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)
|
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)
|
t.Error("number of eager loaded records wrong, got:", got)
|
||||||
}
|
}
|
||||||
|
|
||||||
a.Loaded.{{$rel.Function.Name}} = nil
|
a.R.{{$rel.Function.Name}} = nil
|
||||||
if err = a.Loaded.Load{{$rel.Function.Name}}(tx, true, &a); err != nil {
|
if err = a.R.Load{{$rel.Function.Name}}(tx, true, &a); err != nil {
|
||||||
t.Fatal(err)
|
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)
|
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}
|
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)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if local.Loaded.{{.Function.Name}} == nil {
|
if local.R.{{.Function.Name}} == nil {
|
||||||
t.Error("struct should have been eager loaded")
|
t.Error("struct should have been eager loaded")
|
||||||
}
|
}
|
||||||
|
|
||||||
local.Loaded.{{.Function.Name}} = nil
|
local.R.{{.Function.Name}} = nil
|
||||||
if err = local.Loaded.Load{{.Function.Name}}(tx, true, &local); err != nil {
|
if err = local.R.Load{{.Function.Name}}(tx, true, &local); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if local.Loaded.{{.Function.Name}} == nil {
|
if local.R.{{.Function.Name}} == nil {
|
||||||
t.Error("struct should have been eager loaded")
|
t.Error("struct should have been eager loaded")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue