From c624943f53a3339c64ba97a0212393cab3457260 Mon Sep 17 00:00:00 2001 From: Aaron L Date: Fri, 16 Sep 2016 00:22:12 -0700 Subject: [PATCH] Make a rough sketch of what this would look like Needs cleanup: Do not merge --- imports.go | 8 ++++--- templates/01_types.tpl | 8 +++++-- templates/06_relationship_to_one_eager.tpl | 8 +++++++ templates/07_relationship_to_many_eager.tpl | 10 ++++++++ templates/08_relationship_to_one_setops.tpl | 13 ++++++++--- templates_test/relationship_to_many.tpl | 14 ++++++++++- .../relationship_to_many_setops.tpl | 23 +++++++++++++++++-- templates_test/relationship_to_one.tpl | 14 +++++++++-- templates_test/relationship_to_one_setops.tpl | 23 +++++++++++++++---- templates_test/types.tpl | 5 +++- 10 files changed, 107 insertions(+), 19 deletions(-) diff --git a/imports.go b/imports.go index bf96930..224e763 100644 --- a/imports.go +++ b/imports.go @@ -143,10 +143,11 @@ func removeDuplicates(dedup []string) []string { var defaultTemplateImports = imports{ standard: importList{ - `"fmt"`, - `"strings"`, + `"bytes"`, `"database/sql"`, + `"fmt"`, `"reflect"`, + `"strings"`, `"sync"`, `"time"`, }, @@ -177,8 +178,9 @@ var defaultSingletonTemplateImports = map[string]imports{ var defaultTestTemplateImports = imports{ standard: importList{ - `"testing"`, + `"bytes"`, `"reflect"`, + `"testing"`, }, thirdParty: importList{ `"github.com/vattle/sqlboiler/boil"`, diff --git a/templates/01_types.tpl b/templates/01_types.tpl index 27f4f66..721c11a 100644 --- a/templates/01_types.tpl +++ b/templates/01_types.tpl @@ -36,6 +36,10 @@ var ( {{$varNameSingular}}UpsertCache = make(map[string]insertCache) ) -// Force time package dependency for automated UpdatedAt/CreatedAt. -var _ = time.Second +var ( + // Force time package dependency for automated UpdatedAt/CreatedAt. + _ = time.Second + // Force bytes in case of primary key column that uses []byte (for relationship compares) + _ = bytes.MinRead +) {{end -}} diff --git a/templates/06_relationship_to_one_eager.tpl b/templates/06_relationship_to_one_eager.tpl index 0911029..903b7f6 100644 --- a/templates/06_relationship_to_one_eager.tpl +++ b/templates/06_relationship_to_one_eager.tpl @@ -67,7 +67,15 @@ func ({{$varNameSingular}}L) Load{{.Function.Name}}(e boil.Executor, singular bo for _, foreign := range resultSlice { for _, local := range slice { + {{/* Use the foreign table (the one without the fkey) to avoid null types in the id type */}} + {{- $foreignTable := getTable $dot.Tables .ForeignKey.ForeignTable -}} + {{- $column := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}} + {{- $type := $column.Type -}} + {{if eq $type "[]byte" -}} + if 0 == bytes.Compare(local.{{.Function.LocalAssignment}}, foreign.{{.Function.ForeignAssignment}}) { + {{else -}} if local.{{.Function.LocalAssignment}} == foreign.{{.Function.ForeignAssignment}} { + {{end -}} if local.R == nil { local.R = &{{$varNameSingular}}R{} } diff --git a/templates/07_relationship_to_many_eager.tpl b/templates/07_relationship_to_many_eager.tpl index aae8c4e..484dfb2 100644 --- a/templates/07_relationship_to_many_eager.tpl +++ b/templates/07_relationship_to_many_eager.tpl @@ -106,11 +106,17 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singula return nil } + {{$column := $dot.Table.GetColumn .Column -}} + {{- $type := $column.Type -}} {{if .ToJoinTable -}} for i, foreign := range resultSlice { localJoinCol := localJoinCols[i] for _, local := range slice { + {{if eq $type "[]byte" -}} + if 0 == bytes.Compare(local.{{$txt.Function.LocalAssignment}}, localJoinCol) { + {{else -}} if local.{{$txt.Function.LocalAssignment}} == localJoinCol { + {{end -}} if local.R == nil { local.R = &{{$varNameSingular}}R{} } @@ -122,7 +128,11 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singula {{else -}} for _, foreign := range resultSlice { for _, local := range slice { + {{if eq $type "[]byte" -}} + if 0 == bytes.Compare(local.{{$txt.Function.LocalAssignment}}, foreign.{{$txt.Function.ForeignAssignment}}) { + {{else -}} if local.{{$txt.Function.LocalAssignment}} == foreign.{{$txt.Function.ForeignAssignment}} { + {{end -}} if local.R == nil { local.R = &{{$varNameSingular}}R{} } diff --git a/templates/08_relationship_to_one_setops.tpl b/templates/08_relationship_to_one_setops.tpl index e4e93d6..2ada61a 100644 --- a/templates/08_relationship_to_one_setops.tpl +++ b/templates/08_relationship_to_one_setops.tpl @@ -1,5 +1,5 @@ {{- define "relationship_to_one_setops_helper" -}} - {{- $tmplData := .Dot -}}{{/* .Dot holds the root templateData struct, passed in through preserveDot */}} + {{- $dot := .Dot -}}{{/* .Dot holds the root templateData struct, passed in through preserveDot */}} {{- with .Rel -}} {{- $varNameSingular := .ForeignKey.ForeignTable | singular | camelCase -}} {{- $localNameSingular := .ForeignKey.Table | singular | camelCase}} @@ -75,7 +75,14 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Remove{{.Function.Name}}(e related.R.{{.Function.ForeignName}} = nil {{else -}} for i, ri := range related.R.{{.Function.ForeignName}} { + {{- $foreignTable := getTable $dot.Tables .ForeignKey.ForeignTable -}} + {{- $column := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}} + {{- $type := $column.Type}} + {{if eq $type "[]byte" -}} + if 0 != bytes.Compare({{.Function.Receiver}}.{{.Function.LocalAssignment}}, ri.{{.Function.LocalAssignment}}) { + {{else -}} if {{.Function.Receiver}}.{{.Function.LocalAssignment}} != ri.{{.Function.LocalAssignment}} { + {{end -}} continue } @@ -90,8 +97,8 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Remove{{.Function.Name}}(e return nil } - {{- end -}}{{/* if foreignkey nullable */}} - {{end -}}{{/* end with */}} +{{end -}}{{/* if foreignkey nullable */}} +{{- end -}}{{/* end with */}} {{- end -}}{{/* end define */}} {{- /* Begin execution of template for one-to-one setops */ -}} diff --git a/templates_test/relationship_to_many.tpl b/templates_test/relationship_to_many.tpl index cade6cd..73604b8 100644 --- a/templates_test/relationship_to_many.tpl +++ b/templates_test/relationship_to_many.tpl @@ -4,7 +4,8 @@ {{- $table := .Table }} {{- range .Table.ToManyRelationships -}} {{- if (and .ForeignColumnUnique (not .ToJoinTable)) -}} -{{- template "relationship_to_one_test_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table .) -}} + {{- $txt := textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table . -}} +{{- template "relationship_to_one_test_helper" (preserveDot $dot $txt) -}} {{- else -}} {{- $rel := textsFromRelationship $dot.Tables $table . -}} func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) { @@ -59,12 +60,23 @@ func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) { bFound, cFound := false, false for _, v := range {{$varname}} { + {{- $column := $dot.Table.GetColumn .Column -}} + {{- $type := $column.Type}} + {{if eq $type "[]byte" -}} + if 0 == bytes.Compare(v.{{$rel.Function.ForeignAssignment}}, b.{{$rel.Function.ForeignAssignment}}) { + bFound = true + } + if 0 == bytes.Compare(v.{{$rel.Function.ForeignAssignment}}, c.{{$rel.Function.ForeignAssignment}}) { + cFound = true + } + {{else -}} if v.{{$rel.Function.ForeignAssignment}} == b.{{$rel.Function.ForeignAssignment}} { bFound = true } if v.{{$rel.Function.ForeignAssignment}} == c.{{$rel.Function.ForeignAssignment}} { cFound = true } + {{end -}} } if !bFound { diff --git a/templates_test/relationship_to_many_setops.tpl b/templates_test/relationship_to_many_setops.tpl index e653d36..0950d86 100644 --- a/templates_test/relationship_to_many_setops.tpl +++ b/templates_test/relationship_to_many_setops.tpl @@ -8,8 +8,9 @@ {{- else -}} {{- $varNameSingular := .Table | singular | camelCase -}} {{- $foreignVarNameSingular := .ForeignTable | singular | camelCase -}} - {{- $rel := textsFromRelationship $dot.Tables $table .}} - + {{- $rel := textsFromRelationship $dot.Tables $table . -}} + {{- $column := $dot.Table.GetColumn .Column -}} + {{- $type := $column.Type}} func test{{$rel.LocalTable.NameGo}}ToManyAddOp{{$rel.Function.Name}}(t *testing.T) { var err error @@ -63,12 +64,21 @@ func test{{$rel.LocalTable.NameGo}}ToManyAddOp{{$rel.Function.Name}}(t *testing. } {{- else}} + {{if eq $type "[]byte" -}} + if 0 != bytes.Compare(a.{{$rel.Function.LocalAssignment}}, first.{{$rel.Function.ForeignAssignment}}) { + t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, first.{{$rel.Function.ForeignAssignment}}) + } + if 0 != bytes.Compare(a.{{$rel.Function.LocalAssignment}}, second.{{$rel.Function.ForeignAssignment}}) { + t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, second.{{$rel.Function.ForeignAssignment}}) + } + {{else -}} if a.{{$rel.Function.LocalAssignment}} != first.{{$rel.Function.ForeignAssignment}} { t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, first.{{$rel.Function.ForeignAssignment}}) } if a.{{$rel.Function.LocalAssignment}} != second.{{$rel.Function.ForeignAssignment}} { t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, second.{{$rel.Function.ForeignAssignment}}) } + {{- end}} if first.R.{{$rel.Function.ForeignName}} != &a { t.Error("relationship was not added properly to the foreign slice") @@ -174,12 +184,21 @@ func test{{$rel.LocalTable.NameGo}}ToManySetOp{{$rel.Function.Name}}(t *testing. if c.{{$rel.ForeignTable.ColumnNameGo}}.Valid { t.Error("want c's foreign key value to be nil") } + {{if eq $type "[]byte" -}} + if 0 != bytes.Compare(a.{{$rel.Function.LocalAssignment}}, d.{{$rel.Function.ForeignAssignment}}) { + t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, d.{{$rel.Function.ForeignAssignment}}) + } + if 0 != bytes.Compare(a.{{$rel.Function.LocalAssignment}}, e.{{$rel.Function.ForeignAssignment}}) { + t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, e.{{$rel.Function.ForeignAssignment}}) + } + {{else -}} if a.{{$rel.Function.LocalAssignment}} != d.{{$rel.Function.ForeignAssignment}} { t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, d.{{$rel.Function.ForeignAssignment}}) } if a.{{$rel.Function.LocalAssignment}} != e.{{$rel.Function.ForeignAssignment}} { t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, e.{{$rel.Function.ForeignAssignment}}) } + {{- end}} if b.R.{{$rel.Function.ForeignName}} != nil { t.Error("relationship was not removed properly from the foreign struct") diff --git a/templates_test/relationship_to_one.tpl b/templates_test/relationship_to_one.tpl index f3a60b4..5a91318 100644 --- a/templates_test/relationship_to_one.tpl +++ b/templates_test/relationship_to_one.tpl @@ -1,4 +1,6 @@ {{- define "relationship_to_one_test_helper"}} +{{- $dot := .Dot -}} +{{- with .Rel -}} func test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) { tx := MustTx(boil.Begin()) defer tx.Rollback() @@ -37,7 +39,14 @@ func test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}( t.Fatal(err) } + {{$foreignTable := getTable $dot.Tables .ForeignKey.ForeignTable -}} + {{- $column := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}} + {{- $type := $column.Type -}} + {{if eq $type "[]byte" -}} + if 0 == bytes.Compare(check.{{.Function.ForeignAssignment}}, foreign.{{.Function.ForeignAssignment}}) { + {{else -}} if check.{{.Function.ForeignAssignment}} != foreign.{{.Function.ForeignAssignment}} { + {{end -}} t.Errorf("want: %v, got %v", foreign.{{.Function.ForeignAssignment}}, check.{{.Function.ForeignAssignment}}) } @@ -59,11 +68,12 @@ func test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}( } {{end -}} +{{- end -}} {{- if .Table.IsJoinTable -}} {{- else -}} {{- $dot := . -}} {{- range .Table.FKeys -}} - {{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}} -{{- template "relationship_to_one_test_helper" $rel -}} + {{- $txt := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}} +{{- template "relationship_to_one_test_helper" (preserveDot $dot $txt) -}} {{end -}} {{- end -}} diff --git a/templates_test/relationship_to_one_setops.tpl b/templates_test/relationship_to_one_setops.tpl index 64b8f99..1d984ae 100644 --- a/templates_test/relationship_to_one_setops.tpl +++ b/templates_test/relationship_to_one_setops.tpl @@ -1,6 +1,11 @@ {{- define "relationship_to_one_setops_test_helper" -}} +{{- $dot := .Dot -}} +{{- with .Rel -}} {{- $varNameSingular := .ForeignKey.Table | singular | camelCase -}} {{- $foreignVarNameSingular := .ForeignKey.ForeignTable | singular | camelCase}} +{{- $foreignTable := getTable $dot.Tables .ForeignKey.ForeignTable -}} +{{- $column := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}} +{{- $type := $column.Type -}} func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) { var err error @@ -34,7 +39,11 @@ func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Na t.Fatal(err) } + {{if eq $type "[]byte" -}} + if 0 != bytes.Compare(a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}}) { + {{else -}} if a.{{.Function.LocalAssignment}} != x.{{.Function.ForeignAssignment}} { + {{end -}} t.Error("foreign key was wrong value", a.{{.Function.LocalAssignment}}) } if a.R.{{.Function.Name}} != x { @@ -48,7 +57,11 @@ func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Na t.Fatal("failed to reload", err) } + {{if eq $type "[]byte" -}} + if 0 != bytes.Compare(a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}}) { + {{else -}} if a.{{.Function.LocalAssignment}} != x.{{.Function.ForeignAssignment}} { + {{end -}} t.Error("foreign key was wrong value", a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}}) } @@ -120,14 +133,14 @@ func test{{.LocalTable.NameGo}}ToOneRemoveOp{{.ForeignTable.NameGo}}_{{.Function } {{end -}} } -{{end -}} -{{- end -}} +{{end -}}{{/* end if foreign key nullable */}} +{{- end -}}{{/* with rel */}} +{{- end -}}{{/* define */}} {{- if .Table.IsJoinTable -}} {{- else -}} {{- $dot := . -}} {{- range .Table.FKeys -}} - {{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table .}} - -{{template "relationship_to_one_setops_test_helper" $rel -}} + {{- $txt := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table .}} +{{template "relationship_to_one_setops_test_helper" (preserveDot $dot $txt) -}} {{- end -}} {{- end -}} diff --git a/templates_test/types.tpl b/templates_test/types.tpl index e434f10..477a215 100644 --- a/templates_test/types.tpl +++ b/templates_test/types.tpl @@ -1,2 +1,5 @@ {{- $varNameSingular := .Table.Name | singular | camelCase -}} -var {{$varNameSingular}}DBTypes = map[string]string{{"{"}}{{.Table.Columns | columnDBTypes | makeStringMap}}{{"}"}} +var ( + {{$varNameSingular}}DBTypes = map[string]string{{"{"}}{{.Table.Columns | columnDBTypes | makeStringMap}}{{"}"}} + _ = bytes.MinRead +)