From f489e22ab470eff93fb86e384156c64e69cff588 Mon Sep 17 00:00:00 2001 From: Aaron L Date: Fri, 26 Aug 2016 21:40:11 -0700 Subject: [PATCH] Rename Loaded -> R --- README.md | 2 +- boil/reflect.go | 6 +++--- boil/reflect_test.go | 8 ++++---- templates/00_struct.tpl | 6 +++--- templates/06_relationship_to_one_eager.tpl | 14 +++++++------- templates/07_relationship_to_many_eager.tpl | 20 ++++++++++---------- templates_test/relationship_to_many.tpl | 10 +++++----- templates_test/relationship_to_one.tpl | 10 +++++----- 8 files changed, 38 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 90261e3..be4eb68 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/boil/reflect.go b/boil/reflect.go index dbfb970..bfa5454 100644 --- a/boil/reflect.go +++ b/boil/reflect.go @@ -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 { diff --git a/boil/reflect_test.go b/boil/reflect_test.go index 57e2de2..66495b8 100644 --- a/boil/reflect_test.go +++ b/boil/reflect_test.go @@ -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} diff --git a/templates/00_struct.tpl b/templates/00_struct.tpl index 50eabbf..b271e0e 100644 --- a/templates/00_struct.tpl +++ b/templates/00_struct.tpl @@ -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}} diff --git a/templates/06_relationship_to_one_eager.tpl b/templates/06_relationship_to_one_eager.tpl index baeecbc..7083366 100644 --- a/templates/06_relationship_to_one_eager.tpl +++ b/templates/06_relationship_to_one_eager.tpl @@ -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 } } diff --git a/templates/07_relationship_to_many_eager.tpl b/templates/07_relationship_to_many_eager.tpl index 986d995..0350990 100644 --- a/templates/07_relationship_to_many_eager.tpl +++ b/templates/07_relationship_to_many_eager.tpl @@ -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 } } diff --git a/templates_test/relationship_to_many.tpl b/templates_test/relationship_to_many.tpl index 8466fc7..be1148c 100644 --- a/templates_test/relationship_to_many.tpl +++ b/templates_test/relationship_to_many.tpl @@ -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) } diff --git a/templates_test/relationship_to_one.tpl b/templates_test/relationship_to_one.tpl index b33890d..8eb73dd 100644 --- a/templates_test/relationship_to_one.tpl +++ b/templates_test/relationship_to_one.tpl @@ -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") } }