diff --git a/boil/reflect.go b/boil/reflect.go index 760ab6a..77e0c09 100644 --- a/boil/reflect.go +++ b/boil/reflect.go @@ -100,10 +100,10 @@ func (q *Query) Bind(obj interface{}) error { // loadRelationships dynamically calls the template generated eager load // 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: -// - 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. // - 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 @@ -115,11 +115,11 @@ func (q *Query) loadRelationships(obj interface{}, singular bool) error { typ = typ.Elem() } - rel, found := typ.FieldByName("Relationships") - // If the users object has no Relationships struct, it must be + rel, found := typ.FieldByName("Loaded") + // 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 relationship field") + return errors.New("load query mod was used but bound struct contained no Loaded field") } for _, relationship := range q.load { diff --git a/boil/reflect_test.go b/boil/reflect_test.go index b29f4e4..7d78c67 100644 --- a/boil/reflect_test.go +++ b/boil/reflect_test.go @@ -105,9 +105,9 @@ func TestBindSingular(t *testing.T) { 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 return nil } @@ -117,8 +117,8 @@ func TestLoadRelationshipsSlice(t *testing.T) { loadFunctionCalled = false testSlice := []*struct { - ID int - Relationships *testRelationshipsStruct + ID int + Loaded *testLoadedStruct }{} q := Query{load: []string{"TestOne"}, executor: nil} @@ -136,8 +136,8 @@ func TestLoadRelationshipsSingular(t *testing.T) { loadFunctionCalled = false testSingular := &struct { - ID int - Relationships *testRelationshipsStruct + ID int + Loaded *testLoadedStruct }{} q := Query{load: []string{"TestOne"}, executor: nil} diff --git a/templates/00_struct.tpl b/templates/00_struct.tpl index c8484f1..50eabbf 100644 --- a/templates/00_struct.tpl +++ b/templates/00_struct.tpl @@ -11,16 +11,15 @@ type {{$modelName}} struct { {{end -}} {{- if .Table.IsJoinTable -}} {{- else}} - Relationships *{{$modelName}}Relationships `boil:"-" json:"-" toml:"-" yaml:"-"` + Loaded *{{$modelName}}Loaded `boil:"-" json:"-" toml:"-" yaml:"-"` {{end -}} } {{- $dot := . -}} {{- if .Table.IsJoinTable -}} {{- else}} -// {{$modelName}}Relationships are where relationships are both cached -// and eagerly loaded. -type {{$modelName}}Relationships struct { +// {{$modelName}}Loaded are where relationships are eagerly loaded. +type {{$modelName}}Loaded 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 65f047f..b2f135f 100644 --- a/templates/06_relationship_to_one_eager.tpl +++ b/templates/06_relationship_to_one_eager.tpl @@ -2,8 +2,8 @@ {{- $arg := printf "maybe%s" .LocalTable.NameGo -}} {{- $slice := printf "%sSlice" .LocalTable.NameGo -}} // Load{{.Function.Name}} allows an eager lookup of values, cached into the -// relationships structs of the objects. -func (r *{{.LocalTable.NameGo}}Relationships) Load{{.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error { +// loaded structs of the objects. +func (r *{{.LocalTable.NameGo}}Loaded) 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}}Relationships) Load{{.Function.Name}}(e boil.Exec } if singular && len(resultSlice) != 0 { - if object.Relationships == nil { - object.Relationships = &{{.LocalTable.NameGo}}Relationships{} + if object.Loaded == nil { + object.Loaded = &{{.LocalTable.NameGo}}Loaded{} } - object.Relationships.{{.Function.Name}} = resultSlice[0] + object.Loaded.{{.Function.Name}} = resultSlice[0] return nil } for _, foreign := range resultSlice { for _, local := range slice { if local.{{.Function.LocalAssignment}} == foreign.{{.Function.ForeignAssignment}} { - if local.Relationships == nil { - local.Relationships = &{{.LocalTable.NameGo}}Relationships{} + if local.Loaded == nil { + local.Loaded = &{{.LocalTable.NameGo}}Loaded{} } - local.Relationships.{{.Function.Name}} = foreign + local.Loaded.{{.Function.Name}} = foreign break } } diff --git a/templates/07_relationship_to_many_eager.tpl b/templates/07_relationship_to_many_eager.tpl index f241d74..082045d 100644 --- a/templates/07_relationship_to_many_eager.tpl +++ b/templates/07_relationship_to_many_eager.tpl @@ -9,8 +9,8 @@ {{- $arg := printf "maybe%s" $rel.LocalTable.NameGo -}} {{- $slice := printf "%sSlice" $rel.LocalTable.NameGo -}} // Load{{$rel.Function.Name}} allows an eager lookup of values, cached into the -// relationships structs of the objects. -func (r *{{$rel.LocalTable.NameGo}}Relationships) Load{{$rel.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error { +// loaded structs of the objects. +func (r *{{$rel.LocalTable.NameGo}}Loaded) 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}}Relationships) Load{{$rel.Function.Name}}(e b {{end}} if singular { - if object.Relationships == nil { - object.Relationships = &{{$rel.LocalTable.NameGo}}Relationships{} + if object.Loaded == nil { + object.Loaded = &{{$rel.LocalTable.NameGo}}Loaded{} } - object.Relationships.{{$rel.Function.Name}} = resultSlice + object.Loaded.{{$rel.Function.Name}} = resultSlice return nil } @@ -94,10 +94,10 @@ func (r *{{$rel.LocalTable.NameGo}}Relationships) Load{{$rel.Function.Name}}(e b localJoinCol := localJoinCols[i] for _, local := range slice { if local.{{$rel.Function.LocalAssignment}} == localJoinCol { - if local.Relationships == nil { - local.Relationships = &{{$rel.LocalTable.NameGo}}Relationships{} + if local.Loaded == nil { + 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 } } @@ -106,10 +106,10 @@ func (r *{{$rel.LocalTable.NameGo}}Relationships) Load{{$rel.Function.Name}}(e b for _, foreign := range resultSlice { for _, local := range slice { if local.{{$rel.Function.LocalAssignment}} == foreign.{{$rel.Function.ForeignAssignment}} { - if local.Relationships == nil { - local.Relationships = &{{$rel.LocalTable.NameGo}}Relationships{} + if local.Loaded == nil { + 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 } } diff --git a/templates_test/relationship_to_many.tpl b/templates_test/relationship_to_many.tpl index dcc974f..74340ae 100644 --- a/templates_test/relationship_to_many.tpl +++ b/templates_test/relationship_to_many.tpl @@ -74,18 +74,18 @@ func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) { 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) } - 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) } - a.Relationships.{{$rel.Function.Name}} = nil - if err = a.Relationships.Load{{$rel.Function.Name}}(tx, true, &a); err != nil { + a.Loaded.{{$rel.Function.Name}} = nil + if err = a.Loaded.Load{{$rel.Function.Name}}(tx, true, &a); err != nil { 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) } diff --git a/templates_test/relationship_to_one.tpl b/templates_test/relationship_to_one.tpl index 10b520c..488b2c7 100644 --- a/templates_test/relationship_to_one.tpl +++ b/templates_test/relationship_to_one.tpl @@ -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}}) } - 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) } - if local.Relationships.{{.Function.Name}} == nil { + if local.Loaded.{{.Function.Name}} == nil { t.Error("struct should have been eager loaded") } - local.Relationships.{{.Function.Name}} = nil - if err = local.Relationships.Load{{.Function.Name}}(tx, true, &local); err != nil { + local.Loaded.{{.Function.Name}} = nil + if err = local.Loaded.Load{{.Function.Name}}(tx, true, &local); err != nil { t.Fatal(err) } - if local.Relationships.{{.Function.Name}} == nil { + if local.Loaded.{{.Function.Name}} == nil { t.Error("struct should have been eager loaded") } }