Unexport Relationship struct
This commit is contained in:
parent
2ecff95e42
commit
67ae024439
5 changed files with 23 additions and 17 deletions
|
@ -4,6 +4,7 @@
|
|||
|
||||
{{- $tableNameSingular := .Table.Name | singular -}}
|
||||
{{- $modelName := $tableNameSingular | titleCase -}}
|
||||
{{- $modelNameCamel := $tableNameSingular | camelCase -}}
|
||||
// {{$modelName}} is an object representing the database table.
|
||||
type {{$modelName}} struct {
|
||||
{{range $column := .Table.Columns -}}
|
||||
|
@ -11,15 +12,15 @@ type {{$modelName}} struct {
|
|||
{{end -}}
|
||||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else}}
|
||||
R *{{$modelName}}R `boil:"-" json:"-" toml:"-" yaml:"-"`
|
||||
R *{{$modelNameCamel}}R `boil:"-" json:"-" toml:"-" yaml:"-"`
|
||||
{{end -}}
|
||||
}
|
||||
|
||||
{{- $dot := . -}}
|
||||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else}}
|
||||
// {{$modelName}}R is where relationships are stored.
|
||||
type {{$modelName}}R struct {
|
||||
// {{$modelNameCamel}}R is where relationships are stored.
|
||||
type {{$modelNameCamel}}R struct {
|
||||
{{range .Table.FKeys -}}
|
||||
{{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
|
||||
{{- template "relationship_to_one_struct_helper" $rel}}
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
{{- define "relationship_to_one_eager_helper" -}}
|
||||
{{- $varNameSingular := .Dot.Table.Name | singular | camelCase -}}
|
||||
{{- $noHooks := .Dot.NoHooks -}}
|
||||
{{- with .Rel -}}
|
||||
{{- $arg := printf "maybe%s" .LocalTable.NameGo -}}
|
||||
{{- $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}}R) Load{{.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error {
|
||||
func (r *{{$varNameSingular}}R) Load{{.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error {
|
||||
var slice []*{{.LocalTable.NameGo}}
|
||||
var object *{{.LocalTable.NameGo}}
|
||||
|
||||
|
@ -58,7 +59,7 @@ func (r *{{.LocalTable.NameGo}}R) Load{{.Function.Name}}(e boil.Executor, singul
|
|||
|
||||
if singular && len(resultSlice) != 0 {
|
||||
if object.R == nil {
|
||||
object.R = &{{.LocalTable.NameGo}}R{}
|
||||
object.R = &{{$varNameSingular}}R{}
|
||||
}
|
||||
object.R.{{.Function.Name}} = resultSlice[0]
|
||||
return nil
|
||||
|
@ -68,7 +69,7 @@ func (r *{{.LocalTable.NameGo}}R) Load{{.Function.Name}}(e boil.Executor, singul
|
|||
for _, local := range slice {
|
||||
if local.{{.Function.LocalAssignment}} == foreign.{{.Function.ForeignAssignment}} {
|
||||
if local.R == nil {
|
||||
local.R = &{{.LocalTable.NameGo}}R{}
|
||||
local.R = &{{$varNameSingular}}R{}
|
||||
}
|
||||
local.R.{{.Function.Name}} = foreign
|
||||
break
|
||||
|
|
|
@ -6,12 +6,13 @@
|
|||
{{- $txt := textsFromOneToOneRelationship $dot.PkgName $dot.Tables $dot.Table . -}}
|
||||
{{- template "relationship_to_one_eager_helper" (preserveDot $dot $txt) -}}
|
||||
{{- else -}}
|
||||
{{- $varNameSingular := $dot.Table.Name | singular | camelCase -}}
|
||||
{{- $txt := textsFromRelationship $dot.Tables $dot.Table . -}}
|
||||
{{- $arg := printf "maybe%s" $txt.LocalTable.NameGo -}}
|
||||
{{- $slice := printf "%sSlice" $txt.LocalTable.NameGo -}}
|
||||
// Load{{$txt.Function.Name}} allows an eager lookup of values, cached into the
|
||||
// loaded structs of the objects.
|
||||
func (r *{{$txt.LocalTable.NameGo}}R) Load{{$txt.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error {
|
||||
func (r *{{$varNameSingular}}R) Load{{$txt.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error {
|
||||
var slice []*{{$txt.LocalTable.NameGo}}
|
||||
var object *{{$txt.LocalTable.NameGo}}
|
||||
|
||||
|
@ -94,7 +95,7 @@ func (r *{{$txt.LocalTable.NameGo}}R) Load{{$txt.Function.Name}}(e boil.Executor
|
|||
{{- end}}
|
||||
if singular {
|
||||
if object.R == nil {
|
||||
object.R = &{{$txt.LocalTable.NameGo}}R{}
|
||||
object.R = &{{$varNameSingular}}R{}
|
||||
}
|
||||
object.R.{{$txt.Function.Name}} = resultSlice
|
||||
return nil
|
||||
|
@ -106,7 +107,7 @@ func (r *{{$txt.LocalTable.NameGo}}R) Load{{$txt.Function.Name}}(e boil.Executor
|
|||
for _, local := range slice {
|
||||
if local.{{$txt.Function.LocalAssignment}} == localJoinCol {
|
||||
if local.R == nil {
|
||||
local.R = &{{$txt.LocalTable.NameGo}}R{}
|
||||
local.R = &{{$varNameSingular}}R{}
|
||||
}
|
||||
local.R.{{$txt.Function.Name}} = append(local.R.{{$txt.Function.Name}}, foreign)
|
||||
break
|
||||
|
@ -118,7 +119,7 @@ func (r *{{$txt.LocalTable.NameGo}}R) Load{{$txt.Function.Name}}(e boil.Executor
|
|||
for _, local := range slice {
|
||||
if local.{{$txt.Function.LocalAssignment}} == foreign.{{$txt.Function.ForeignAssignment}} {
|
||||
if local.R == nil {
|
||||
local.R = &{{$txt.LocalTable.NameGo}}R{}
|
||||
local.R = &{{$varNameSingular}}R{}
|
||||
}
|
||||
local.R.{{$txt.Function.Name}} = append(local.R.{{$txt.Function.Name}}, foreign)
|
||||
break
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
{{- define "relationship_to_one_setops_helper" -}}
|
||||
{{- $varNameSingular := .ForeignKey.ForeignTable | singular | camelCase}}
|
||||
{{- $varNameSingular := .ForeignKey.ForeignTable | singular | camelCase -}}
|
||||
{{- $localNameSingular := .ForeignKey.Table | singular | camelCase -}}
|
||||
|
||||
// Set{{.Function.Name}} of the {{.ForeignKey.Table | singular}} to the related item.
|
||||
// Sets {{.Function.Receiver}}.R.{{.Function.Name}} to related.
|
||||
|
@ -20,7 +21,7 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Set{{.Function.Name}}(exec
|
|||
}
|
||||
|
||||
if {{.Function.Receiver}}.R == nil {
|
||||
{{.Function.Receiver}}.R = &{{.LocalTable.NameGo}}R{
|
||||
{{.Function.Receiver}}.R = &{{$localNameSingular}}R{
|
||||
{{.Function.Name}}: related,
|
||||
}
|
||||
} else {
|
||||
|
@ -29,7 +30,7 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Set{{.Function.Name}}(exec
|
|||
|
||||
{{if (or .ForeignKey.Unique .Function.OneToOne) -}}
|
||||
if related.R == nil {
|
||||
related.R = &{{.ForeignTable.NameGo}}R{
|
||||
related.R = &{{$varNameSingular}}R{
|
||||
{{.Function.ForeignName}}: {{.Function.Receiver}},
|
||||
}
|
||||
} else {
|
||||
|
@ -37,7 +38,7 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Set{{.Function.Name}}(exec
|
|||
}
|
||||
{{else -}}
|
||||
if related.R == nil {
|
||||
related.R = &{{.ForeignTable.NameGo}}R{
|
||||
related.R = &{{$varNameSingular}}R{
|
||||
{{.Function.ForeignName}}: {{.LocalTable.NameGo}}Slice{{"{"}}{{.Function.Receiver}}{{"}"}},
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
{{- template "relationship_to_one_setops_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table .) -}}
|
||||
{{- else -}}
|
||||
{{- $rel := textsFromRelationship $dot.Tables $table .}}
|
||||
{{- $localNameSingular := .Table | singular | camelCase -}}
|
||||
{{- $foreignNameSingular := .ForeignTable | singular | camelCase -}}
|
||||
|
||||
// Add{{$rel.Function.Name}} adds the given related objects to the existing relationships
|
||||
// of the {{$table.Name | singular}}, optionally inserting them as new records.
|
||||
|
@ -51,7 +53,7 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) Add{{$rel.Function
|
|||
{{end -}}
|
||||
|
||||
if {{$rel.Function.Receiver}}.R == nil {
|
||||
{{$rel.Function.Receiver}}.R = &{{$rel.LocalTable.NameGo}}R{
|
||||
{{$rel.Function.Receiver}}.R = &{{$localNameSingular}}R{
|
||||
{{$rel.Function.Name}}: related,
|
||||
}
|
||||
} else {
|
||||
|
@ -61,7 +63,7 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) Add{{$rel.Function
|
|||
{{if .ToJoinTable -}}
|
||||
for _, rel := range related {
|
||||
if rel.R == nil {
|
||||
rel.R = &{{$rel.ForeignTable.NameGo}}R{
|
||||
rel.R = &{{$foreignNameSingular}}R{
|
||||
{{$rel.Function.ForeignName}}: {{$rel.LocalTable.NameGo}}Slice{{"{"}}{{$rel.Function.Receiver}}{{"}"}},
|
||||
}
|
||||
} else {
|
||||
|
@ -71,7 +73,7 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) Add{{$rel.Function
|
|||
{{else -}}
|
||||
for _, rel := range related {
|
||||
if rel.R == nil {
|
||||
rel.R = &{{$rel.ForeignTable.NameGo}}R{
|
||||
rel.R = &{{$foreignNameSingular}}R{
|
||||
{{$rel.Function.ForeignName}}: {{$rel.Function.Receiver}},
|
||||
}
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue