Clean up the byte primary key stuff

This commit is contained in:
Aaron L 2016-09-16 08:02:32 -07:00
parent c624943f53
commit 32561717b1
7 changed files with 28 additions and 29 deletions

View file

@ -4,6 +4,9 @@
{{- with .Rel -}} {{- with .Rel -}}
{{- $arg := printf "maybe%s" .LocalTable.NameGo -}} {{- $arg := printf "maybe%s" .LocalTable.NameGo -}}
{{- $slice := printf "%sSlice" .LocalTable.NameGo -}} {{- $slice := printf "%sSlice" .LocalTable.NameGo -}}
{{- $foreignTable := getTable $dot.Tables .ForeignKey.ForeignTable -}}
{{- $foreignTableFKeyCol := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}}
{{- $usesBytes := eq "[]byte" $foreignTableFKeyCol.Type}}
// Load{{.Function.Name}} allows an eager lookup of values, cached into the // Load{{.Function.Name}} allows an eager lookup of values, cached into the
// loaded structs of the objects. // loaded structs of the objects.
func ({{$varNameSingular}}L) Load{{.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error { func ({{$varNameSingular}}L) Load{{.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error {
@ -67,11 +70,7 @@ func ({{$varNameSingular}}L) Load{{.Function.Name}}(e boil.Executor, singular bo
for _, foreign := range resultSlice { for _, foreign := range resultSlice {
for _, local := range slice { for _, local := range slice {
{{/* Use the foreign table (the one without the fkey) to avoid null types in the id type */}} {{if $usesBytes -}}
{{- $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}}) { if 0 == bytes.Compare(local.{{.Function.LocalAssignment}}, foreign.{{.Function.ForeignAssignment}}) {
{{else -}} {{else -}}
if local.{{.Function.LocalAssignment}} == foreign.{{.Function.ForeignAssignment}} { if local.{{.Function.LocalAssignment}} == foreign.{{.Function.ForeignAssignment}} {

View file

@ -14,6 +14,8 @@
{{- $arg := printf "maybe%s" $txt.LocalTable.NameGo -}} {{- $arg := printf "maybe%s" $txt.LocalTable.NameGo -}}
{{- $slice := printf "%sSlice" $txt.LocalTable.NameGo -}} {{- $slice := printf "%sSlice" $txt.LocalTable.NameGo -}}
{{- $schemaForeignTable := .ForeignTable | $dot.SchemaTable -}} {{- $schemaForeignTable := .ForeignTable | $dot.SchemaTable -}}
{{- $fkeyCol := $dot.Table.GetColumn .Column -}}
{{- $usesBytes := eq "[]byte" $fkeyCol.Type -}}
// Load{{$txt.Function.Name}} allows an eager lookup of values, cached into the // Load{{$txt.Function.Name}} allows an eager lookup of values, cached into the
// loaded structs of the objects. // loaded structs of the objects.
func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error { func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error {
@ -106,13 +108,11 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singula
return nil return nil
} }
{{$column := $dot.Table.GetColumn .Column -}}
{{- $type := $column.Type -}}
{{if .ToJoinTable -}} {{if .ToJoinTable -}}
for i, foreign := range resultSlice { for i, foreign := range resultSlice {
localJoinCol := localJoinCols[i] localJoinCol := localJoinCols[i]
for _, local := range slice { for _, local := range slice {
{{if eq $type "[]byte" -}} {{if $usesBytes -}}
if 0 == bytes.Compare(local.{{$txt.Function.LocalAssignment}}, localJoinCol) { if 0 == bytes.Compare(local.{{$txt.Function.LocalAssignment}}, localJoinCol) {
{{else -}} {{else -}}
if local.{{$txt.Function.LocalAssignment}} == localJoinCol { if local.{{$txt.Function.LocalAssignment}} == localJoinCol {
@ -128,7 +128,7 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singula
{{else -}} {{else -}}
for _, foreign := range resultSlice { for _, foreign := range resultSlice {
for _, local := range slice { for _, local := range slice {
{{if eq $type "[]byte" -}} {{if $usesBytes -}}
if 0 == bytes.Compare(local.{{$txt.Function.LocalAssignment}}, foreign.{{$txt.Function.ForeignAssignment}}) { if 0 == bytes.Compare(local.{{$txt.Function.LocalAssignment}}, foreign.{{$txt.Function.ForeignAssignment}}) {
{{else -}} {{else -}}
if local.{{$txt.Function.LocalAssignment}} == foreign.{{$txt.Function.ForeignAssignment}} { if local.{{$txt.Function.LocalAssignment}} == foreign.{{$txt.Function.ForeignAssignment}} {

View file

@ -2,7 +2,10 @@
{{- $dot := .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 -}} {{- with .Rel -}}
{{- $varNameSingular := .ForeignKey.ForeignTable | singular | camelCase -}} {{- $varNameSingular := .ForeignKey.ForeignTable | singular | camelCase -}}
{{- $localNameSingular := .ForeignKey.Table | singular | camelCase}} {{- $localNameSingular := .ForeignKey.Table | singular | camelCase -}}
{{- $foreignTable := getTable $dot.Tables .ForeignKey.ForeignTable -}}
{{- $foreignTableFKeyCol := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}}
{{- $usesBytes := eq "[]byte" $foreignTableFKeyCol.Type -}}
// Set{{.Function.Name}} of the {{.ForeignKey.Table | singular}} to the related item. // Set{{.Function.Name}} of the {{.ForeignKey.Table | singular}} to the related item.
// Sets {{.Function.Receiver}}.R.{{.Function.Name}} to related. // Sets {{.Function.Receiver}}.R.{{.Function.Name}} to related.
// Adds {{.Function.Receiver}} to related.R.{{.Function.ForeignName}}. // Adds {{.Function.Receiver}} to related.R.{{.Function.ForeignName}}.
@ -75,10 +78,7 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Remove{{.Function.Name}}(e
related.R.{{.Function.ForeignName}} = nil related.R.{{.Function.ForeignName}} = nil
{{else -}} {{else -}}
for i, ri := range related.R.{{.Function.ForeignName}} { for i, ri := range related.R.{{.Function.ForeignName}} {
{{- $foreignTable := getTable $dot.Tables .ForeignKey.ForeignTable -}} {{if $usesBytes -}}
{{- $column := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}}
{{- $type := $column.Type}}
{{if eq $type "[]byte" -}}
if 0 != bytes.Compare({{.Function.Receiver}}.{{.Function.LocalAssignment}}, ri.{{.Function.LocalAssignment}}) { if 0 != bytes.Compare({{.Function.Receiver}}.{{.Function.LocalAssignment}}, ri.{{.Function.LocalAssignment}}) {
{{else -}} {{else -}}
if {{.Function.Receiver}}.{{.Function.LocalAssignment}} != ri.{{.Function.LocalAssignment}} { if {{.Function.Receiver}}.{{.Function.LocalAssignment}} != ri.{{.Function.LocalAssignment}} {

View file

@ -8,6 +8,8 @@
{{- template "relationship_to_one_test_helper" (preserveDot $dot $txt) -}} {{- template "relationship_to_one_test_helper" (preserveDot $dot $txt) -}}
{{- else -}} {{- else -}}
{{- $rel := textsFromRelationship $dot.Tables $table . -}} {{- $rel := textsFromRelationship $dot.Tables $table . -}}
{{- $fkeyCol := $dot.Table.GetColumn .Column -}}
{{- $usesBytes := eq "[]byte" $fkeyCol.Type}}
func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) { func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
var err error var err error
tx := MustTx(boil.Begin()) tx := MustTx(boil.Begin())
@ -60,9 +62,7 @@ func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
bFound, cFound := false, false bFound, cFound := false, false
for _, v := range {{$varname}} { for _, v := range {{$varname}} {
{{- $column := $dot.Table.GetColumn .Column -}} {{if $usesBytes -}}
{{- $type := $column.Type}}
{{if eq $type "[]byte" -}}
if 0 == bytes.Compare(v.{{$rel.Function.ForeignAssignment}}, b.{{$rel.Function.ForeignAssignment}}) { if 0 == bytes.Compare(v.{{$rel.Function.ForeignAssignment}}, b.{{$rel.Function.ForeignAssignment}}) {
bFound = true bFound = true
} }

View file

@ -9,8 +9,8 @@
{{- $varNameSingular := .Table | singular | camelCase -}} {{- $varNameSingular := .Table | singular | camelCase -}}
{{- $foreignVarNameSingular := .ForeignTable | singular | camelCase -}} {{- $foreignVarNameSingular := .ForeignTable | singular | camelCase -}}
{{- $rel := textsFromRelationship $dot.Tables $table . -}} {{- $rel := textsFromRelationship $dot.Tables $table . -}}
{{- $column := $dot.Table.GetColumn .Column -}} {{- $fkeyCol := $dot.Table.GetColumn .Column -}}
{{- $type := $column.Type}} {{- $usesBytes := eq "[]byte" $fkeyCol.Type}}
func test{{$rel.LocalTable.NameGo}}ToManyAddOp{{$rel.Function.Name}}(t *testing.T) { func test{{$rel.LocalTable.NameGo}}ToManyAddOp{{$rel.Function.Name}}(t *testing.T) {
var err error var err error
@ -64,7 +64,7 @@ func test{{$rel.LocalTable.NameGo}}ToManyAddOp{{$rel.Function.Name}}(t *testing.
} }
{{- else}} {{- else}}
{{if eq $type "[]byte" -}} {{if $usesBytes -}}
if 0 != bytes.Compare(a.{{$rel.Function.LocalAssignment}}, first.{{$rel.Function.ForeignAssignment}}) { 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}}) t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, first.{{$rel.Function.ForeignAssignment}})
} }
@ -184,7 +184,7 @@ func test{{$rel.LocalTable.NameGo}}ToManySetOp{{$rel.Function.Name}}(t *testing.
if c.{{$rel.ForeignTable.ColumnNameGo}}.Valid { if c.{{$rel.ForeignTable.ColumnNameGo}}.Valid {
t.Error("want c's foreign key value to be nil") t.Error("want c's foreign key value to be nil")
} }
{{if eq $type "[]byte" -}} {{if $usesBytes -}}
if 0 != bytes.Compare(a.{{$rel.Function.LocalAssignment}}, d.{{$rel.Function.ForeignAssignment}}) { 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}}) t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, d.{{$rel.Function.ForeignAssignment}})
} }

View file

@ -1,6 +1,9 @@
{{- define "relationship_to_one_test_helper"}} {{- define "relationship_to_one_test_helper"}}
{{- $dot := .Dot -}} {{- $dot := .Dot -}}
{{- with .Rel -}} {{- with .Rel -}}
{{- $foreignTable := getTable $dot.Tables .ForeignKey.ForeignTable -}}
{{- $foreignTableFKeyCol := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}}
{{- $usesBytes := eq "[]byte" $foreignTableFKeyCol.Type -}}
func test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) { func test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) {
tx := MustTx(boil.Begin()) tx := MustTx(boil.Begin())
defer tx.Rollback() defer tx.Rollback()
@ -39,10 +42,7 @@ func test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(
t.Fatal(err) t.Fatal(err)
} }
{{$foreignTable := getTable $dot.Tables .ForeignKey.ForeignTable -}} {{if $usesBytes -}}
{{- $column := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}}
{{- $type := $column.Type -}}
{{if eq $type "[]byte" -}}
if 0 == bytes.Compare(check.{{.Function.ForeignAssignment}}, foreign.{{.Function.ForeignAssignment}}) { if 0 == bytes.Compare(check.{{.Function.ForeignAssignment}}, foreign.{{.Function.ForeignAssignment}}) {
{{else -}} {{else -}}
if check.{{.Function.ForeignAssignment}} != foreign.{{.Function.ForeignAssignment}} { if check.{{.Function.ForeignAssignment}} != foreign.{{.Function.ForeignAssignment}} {

View file

@ -4,8 +4,8 @@
{{- $varNameSingular := .ForeignKey.Table | singular | camelCase -}} {{- $varNameSingular := .ForeignKey.Table | singular | camelCase -}}
{{- $foreignVarNameSingular := .ForeignKey.ForeignTable | singular | camelCase}} {{- $foreignVarNameSingular := .ForeignKey.ForeignTable | singular | camelCase}}
{{- $foreignTable := getTable $dot.Tables .ForeignKey.ForeignTable -}} {{- $foreignTable := getTable $dot.Tables .ForeignKey.ForeignTable -}}
{{- $column := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}} {{- $foreignTableFKeyCol := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}}
{{- $type := $column.Type -}} {{- $usesBytes := eq "[]byte" $foreignTableFKeyCol.Type -}}
func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) { func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) {
var err error var err error
@ -39,7 +39,7 @@ func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Na
t.Fatal(err) t.Fatal(err)
} }
{{if eq $type "[]byte" -}} {{if $usesBytes -}}
if 0 != bytes.Compare(a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}}) { if 0 != bytes.Compare(a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}}) {
{{else -}} {{else -}}
if a.{{.Function.LocalAssignment}} != x.{{.Function.ForeignAssignment}} { if a.{{.Function.LocalAssignment}} != x.{{.Function.ForeignAssignment}} {
@ -57,7 +57,7 @@ func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Na
t.Fatal("failed to reload", err) t.Fatal("failed to reload", err)
} }
{{if eq $type "[]byte" -}} {{if $usesBytes -}}
if 0 != bytes.Compare(a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}}) { if 0 != bytes.Compare(a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}}) {
{{else -}} {{else -}}
if a.{{.Function.LocalAssignment}} != x.{{.Function.ForeignAssignment}} { if a.{{.Function.LocalAssignment}} != x.{{.Function.ForeignAssignment}} {