Rename Relationships struct to Loaded

This commit is contained in:
Aaron L 2016-08-22 21:53:37 -07:00
parent 48dc259841
commit 509fe44f4e
7 changed files with 43 additions and 44 deletions

View file

@ -100,10 +100,10 @@ func (q *Query) Bind(obj interface{}) 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 *TableRelationships) LoadRelationshipName(exec Executor, singular bool, obj interface{}) // func (t *TableLoaded) 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 relationships // - t is not considered here, and is always passed nil. The function exists on a loaded
// struct to avoid a circular dependency with boil, and the receiver is ignored. // struct to avoid a circular dependency with boil, and the receiver is ignored.
// - exec is used to perform additional queries that might be required for loading the relationships. // - exec is used to perform additional queries that might be required for loading the relationships.
// - singular is passed in to identify whether or not this was a single object // - singular is passed in to identify whether or not this was a single object
@ -115,11 +115,11 @@ func (q *Query) loadRelationships(obj interface{}, singular bool) error {
typ = typ.Elem() typ = typ.Elem()
} }
rel, found := typ.FieldByName("Relationships") rel, found := typ.FieldByName("Loaded")
// If the users object has no Relationships 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 relationship field") return errors.New("load query mod was used but bound struct contained no Loaded field")
} }
for _, relationship := range q.load { for _, relationship := range q.load {

View file

@ -105,9 +105,9 @@ func TestBindSingular(t *testing.T) {
var loadFunctionCalled bool var loadFunctionCalled bool
type testRelationshipsStruct struct{} type testLoadedStruct struct{}
func (r *testRelationshipsStruct) LoadTestOne(exec Executor, singular bool, obj interface{}) error { func (r *testLoadedStruct) LoadTestOne(exec Executor, singular bool, obj interface{}) error {
loadFunctionCalled = true loadFunctionCalled = true
return nil return nil
} }
@ -118,7 +118,7 @@ func TestLoadRelationshipsSlice(t *testing.T) {
testSlice := []*struct { testSlice := []*struct {
ID int ID int
Relationships *testRelationshipsStruct Loaded *testLoadedStruct
}{} }{}
q := Query{load: []string{"TestOne"}, executor: nil} q := Query{load: []string{"TestOne"}, executor: nil}
@ -137,7 +137,7 @@ func TestLoadRelationshipsSingular(t *testing.T) {
testSingular := &struct { testSingular := &struct {
ID int ID int
Relationships *testRelationshipsStruct Loaded *testLoadedStruct
}{} }{}
q := Query{load: []string{"TestOne"}, executor: nil} q := Query{load: []string{"TestOne"}, executor: nil}

View file

@ -11,16 +11,15 @@ type {{$modelName}} struct {
{{end -}} {{end -}}
{{- if .Table.IsJoinTable -}} {{- if .Table.IsJoinTable -}}
{{- else}} {{- else}}
Relationships *{{$modelName}}Relationships `boil:"-" json:"-" toml:"-" yaml:"-"` Loaded *{{$modelName}}Loaded `boil:"-" json:"-" toml:"-" yaml:"-"`
{{end -}} {{end -}}
} }
{{- $dot := . -}} {{- $dot := . -}}
{{- if .Table.IsJoinTable -}} {{- if .Table.IsJoinTable -}}
{{- else}} {{- else}}
// {{$modelName}}Relationships are where relationships are both cached // {{$modelName}}Loaded are where relationships are eagerly loaded.
// and eagerly loaded. type {{$modelName}}Loaded struct {
type {{$modelName}}Relationships 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}}

View file

@ -2,8 +2,8 @@
{{- $arg := printf "maybe%s" .LocalTable.NameGo -}} {{- $arg := printf "maybe%s" .LocalTable.NameGo -}}
{{- $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
// relationships structs of the objects. // loaded structs of the objects.
func (r *{{.LocalTable.NameGo}}Relationships) Load{{.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error { func (r *{{.LocalTable.NameGo}}Loaded) 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}}Relationships) Load{{.Function.Name}}(e boil.Exec
} }
if singular && len(resultSlice) != 0 { if singular && len(resultSlice) != 0 {
if object.Relationships == nil { if object.Loaded == nil {
object.Relationships = &{{.LocalTable.NameGo}}Relationships{} object.Loaded = &{{.LocalTable.NameGo}}Loaded{}
} }
object.Relationships.{{.Function.Name}} = resultSlice[0] object.Loaded.{{.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.Relationships == nil { if local.Loaded == nil {
local.Relationships = &{{.LocalTable.NameGo}}Relationships{} local.Loaded = &{{.LocalTable.NameGo}}Loaded{}
} }
local.Relationships.{{.Function.Name}} = foreign local.Loaded.{{.Function.Name}} = foreign
break break
} }
} }

View file

@ -9,8 +9,8 @@
{{- $arg := printf "maybe%s" $rel.LocalTable.NameGo -}} {{- $arg := printf "maybe%s" $rel.LocalTable.NameGo -}}
{{- $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
// relationships structs of the objects. // loaded structs of the objects.
func (r *{{$rel.LocalTable.NameGo}}Relationships) Load{{$rel.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error { func (r *{{$rel.LocalTable.NameGo}}Loaded) 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}}Relationships) Load{{$rel.Function.Name}}(e b
{{end}} {{end}}
if singular { if singular {
if object.Relationships == nil { if object.Loaded == nil {
object.Relationships = &{{$rel.LocalTable.NameGo}}Relationships{} object.Loaded = &{{$rel.LocalTable.NameGo}}Loaded{}
} }
object.Relationships.{{$rel.Function.Name}} = resultSlice object.Loaded.{{$rel.Function.Name}} = resultSlice
return nil return nil
} }
@ -94,10 +94,10 @@ func (r *{{$rel.LocalTable.NameGo}}Relationships) Load{{$rel.Function.Name}}(e b
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.Relationships == nil { if local.Loaded == nil {
local.Relationships = &{{$rel.LocalTable.NameGo}}Relationships{} local.Loaded = &{{$rel.LocalTable.NameGo}}Loaded{}
} }
local.Relationships.{{$rel.Function.Name}} = append(local.Relationships.{{$rel.Function.Name}}, foreign) local.Loaded.{{$rel.Function.Name}} = append(local.Loaded.{{$rel.Function.Name}}, foreign)
break break
} }
} }
@ -106,10 +106,10 @@ func (r *{{$rel.LocalTable.NameGo}}Relationships) Load{{$rel.Function.Name}}(e b
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.Relationships == nil { if local.Loaded == nil {
local.Relationships = &{{$rel.LocalTable.NameGo}}Relationships{} local.Loaded = &{{$rel.LocalTable.NameGo}}Loaded{}
} }
local.Relationships.{{$rel.Function.Name}} = append(local.Relationships.{{$rel.Function.Name}}, foreign) local.Loaded.{{$rel.Function.Name}} = append(local.Loaded.{{$rel.Function.Name}}, foreign)
break break
} }
} }

View file

@ -74,18 +74,18 @@ func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
t.Error("expected to find c") t.Error("expected to find c")
} }
if err = a.Relationships.Load{{$rel.Function.Name}}(tx, false, {{$rel.LocalTable.NameGo}}Slice{&a}); err != nil { if err = a.Loaded.Load{{$rel.Function.Name}}(tx, false, {{$rel.LocalTable.NameGo}}Slice{&a}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if got := len(a.Relationships.{{$rel.Function.Name}}); got != 2 { if got := len(a.Loaded.{{$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.Relationships.{{$rel.Function.Name}} = nil a.Loaded.{{$rel.Function.Name}} = nil
if err = a.Relationships.Load{{$rel.Function.Name}}(tx, true, &a); err != nil { if err = a.Loaded.Load{{$rel.Function.Name}}(tx, true, &a); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if got := len(a.Relationships.{{$rel.Function.Name}}); got != 2 { if got := len(a.Loaded.{{$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)
} }

View file

@ -41,18 +41,18 @@ func test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(
t.Errorf("want: %v, got %v", foreign.{{.Function.ForeignAssignment}}, check.{{.Function.ForeignAssignment}}) t.Errorf("want: %v, got %v", foreign.{{.Function.ForeignAssignment}}, check.{{.Function.ForeignAssignment}})
} }
if err = local.Relationships.Load{{.Function.Name}}(tx, false, {{.LocalTable.NameGo}}Slice{&local}); err != nil { if err = local.Loaded.Load{{.Function.Name}}(tx, false, {{.LocalTable.NameGo}}Slice{&local}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if local.Relationships.{{.Function.Name}} == nil { if local.Loaded.{{.Function.Name}} == nil {
t.Error("struct should have been eager loaded") t.Error("struct should have been eager loaded")
} }
local.Relationships.{{.Function.Name}} = nil local.Loaded.{{.Function.Name}} = nil
if err = local.Relationships.Load{{.Function.Name}}(tx, true, &local); err != nil { if err = local.Loaded.Load{{.Function.Name}}(tx, true, &local); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if local.Relationships.{{.Function.Name}} == nil { if local.Loaded.{{.Function.Name}} == nil {
t.Error("struct should have been eager loaded") t.Error("struct should have been eager loaded")
} }
} }