Rename Relationships struct to Loaded
This commit is contained in:
parent
48dc259841
commit
509fe44f4e
7 changed files with 43 additions and 44 deletions
|
@ -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 {
|
||||
|
|
|
@ -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}
|
||||
|
|
|
@ -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}}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue