Clean up the bytes checks with text helpers

This commit is contained in:
Aaron L 2016-09-17 00:02:03 -07:00
parent 69a59043c8
commit afe2665e52
9 changed files with 46 additions and 51 deletions

View file

@ -5,11 +5,6 @@ import "github.com/vattle/sqlboiler/strmangle"
// Column holds information about a database column. // Column holds information about a database column.
// Types are Go types, converted by TranslateColumnType. // Types are Go types, converted by TranslateColumnType.
type Column struct { type Column struct {
// ArrType is the underlying data type of the Postgres
// ARRAY type. See here:
// https://www.postgresql.org/docs/9.1/static/infoschema-element-types.html
ArrType *string
UDTName string
Name string Name string
Type string Type string
DBType string DBType string
@ -17,6 +12,13 @@ type Column struct {
Nullable bool Nullable bool
Unique bool Unique bool
Validated bool Validated bool
// Postgres only extension bits
// ArrType is the underlying data type of the Postgres
// ARRAY type. See here:
// https://www.postgresql.org/docs/9.1/static/infoschema-element-types.html
ArrType *string
UDTName string
} }
// ColumnNames of the columns. // ColumnNames of the columns.

View file

@ -3,10 +3,7 @@
{{- $varNameSingular := $dot.Table.Name | singular | camelCase -}} {{- $varNameSingular := $dot.Table.Name | singular | camelCase -}}
{{- 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 {
@ -70,7 +67,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 {
{{if $usesBytes -}} {{if .Function.UsesBytes -}}
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

@ -13,9 +13,7 @@
{{- $txt := textsFromRelationship $dot.Tables $dot.Table . -}} {{- $txt := textsFromRelationship $dot.Tables $dot.Table . -}}
{{- $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 {
@ -112,7 +110,7 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singula
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 $usesBytes -}} {{if $txt.Function.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 +126,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 $usesBytes -}} {{if $txt.Function.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,10 +2,7 @@
{{- $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}}.
@ -78,7 +75,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}} {
{{if $usesBytes -}} {{if .Function.UsesBytes -}}
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

@ -7,9 +7,7 @@
{{- $txt := textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table . -}} {{- $txt := textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table . -}}
{{- 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())
@ -23,8 +21,8 @@ func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
} }
seed := randomize.NewSeed() seed := randomize.NewSeed()
randomize.Struct(seed, &b, {{$rel.ForeignTable.NameSingular | camelCase}}DBTypes, false{{if not $usesBytes}}, "{{.ForeignColumn}}"{{end}}) randomize.Struct(seed, &b, {{$rel.ForeignTable.NameSingular | camelCase}}DBTypes, false{{if not $rel.Function.UsesBytes}}, "{{.ForeignColumn}}"{{end}})
randomize.Struct(seed, &c, {{$rel.ForeignTable.NameSingular | camelCase}}DBTypes, false{{if not $usesBytes}}, "{{.ForeignColumn}}"{{end}}) randomize.Struct(seed, &c, {{$rel.ForeignTable.NameSingular | camelCase}}DBTypes, false{{if not $rel.Function.UsesBytes}}, "{{.ForeignColumn}}"{{end}})
{{if .Nullable -}} {{if .Nullable -}}
a.{{.Column | titleCase}}.Valid = true a.{{.Column | titleCase}}.Valid = true
{{- end}} {{- end}}
@ -62,7 +60,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}} {
{{if $usesBytes -}} {{if $rel.Function.UsesBytes -}}
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,9 +9,7 @@
{{- else -}} {{- else -}}
{{- $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 .}}
{{- $fkeyCol := $dot.Table.GetColumn .Column -}}
{{- $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
@ -65,7 +63,7 @@ func test{{$rel.LocalTable.NameGo}}ToManyAddOp{{$rel.Function.Name}}(t *testing.
} }
{{- else}} {{- else}}
{{if $usesBytes -}} {{if $rel.Function.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}})
} }
@ -185,7 +183,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 $usesBytes -}} {{if $rel.Function.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,9 +1,6 @@
{{- 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()
@ -42,7 +39,7 @@ func test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(
t.Fatal(err) t.Fatal(err)
} }
{{if $usesBytes -}} {{if .Function.UsesBytes -}}
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

@ -2,10 +2,7 @@
{{- $dot := .Dot -}} {{- $dot := .Dot -}}
{{- with .Rel -}} {{- with .Rel -}}
{{- $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 -}}
{{- $foreignTableFKeyCol := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}}
{{- $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 +36,7 @@ func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Na
t.Fatal(err) t.Fatal(err)
} }
{{if $usesBytes -}} {{if .Function.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 +54,7 @@ func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Na
t.Fatal("failed to reload", err) t.Fatal("failed to reload", err)
} }
{{if $usesBytes -}} {{if .Function.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}} {

View file

@ -30,9 +30,10 @@ type RelationshipToOneTexts struct {
Name string Name string
ForeignName string ForeignName string
Varname string Varname string
Receiver string Receiver string
OneToOne bool OneToOne bool
UsesBytes bool
LocalAssignment string LocalAssignment string
ForeignAssignment string ForeignAssignment string
@ -70,14 +71,17 @@ func textsFromForeignKey(packageName string, tables []bdb.Table, table bdb.Table
r.Function.LocalAssignment = strmangle.TitleCase(fkey.Column) r.Function.LocalAssignment = strmangle.TitleCase(fkey.Column)
} }
foreignTable := bdb.GetTable(tables, fkey.ForeignTable)
foreignColumn := foreignTable.GetColumn(fkey.ForeignColumn)
if fkey.ForeignColumnNullable { if fkey.ForeignColumnNullable {
foreignTable := bdb.GetTable(tables, fkey.ForeignTable) r.Function.ForeignAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(fkey.ForeignColumn), strings.TrimPrefix(foreignColumn.Type, "null."))
col := foreignTable.GetColumn(fkey.ForeignColumn)
r.Function.ForeignAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(fkey.ForeignColumn), strings.TrimPrefix(col.Type, "null."))
} else { } else {
r.Function.ForeignAssignment = strmangle.TitleCase(fkey.ForeignColumn) r.Function.ForeignAssignment = strmangle.TitleCase(fkey.ForeignColumn)
} }
r.Function.UsesBytes = foreignColumn.Type == "[]byte"
return r return r
} }
@ -99,6 +103,9 @@ func textsFromOneToOneRelationship(packageName string, tables []bdb.Table, table
rel.Function.Name = strmangle.TitleCase(strmangle.Singular(toMany.ForeignTable)) rel.Function.Name = strmangle.TitleCase(strmangle.Singular(toMany.ForeignTable))
rel.Function.ForeignName = mkFunctionName(strmangle.Singular(toMany.Table), strmangle.TitleCase(strmangle.Singular(toMany.Table)), toMany.ForeignColumn, false) rel.Function.ForeignName = mkFunctionName(strmangle.Singular(toMany.Table), strmangle.TitleCase(strmangle.Singular(toMany.Table)), toMany.ForeignColumn, false)
rel.Function.OneToOne = true rel.Function.OneToOne = true
col := table.GetColumn(toMany.Column)
rel.Function.UsesBytes = col.Type == "[]byte"
return rel return rel
} }
@ -124,6 +131,8 @@ type RelationshipToManyTexts struct {
ForeignName string ForeignName string
Receiver string Receiver string
UsesBytes bool
LocalAssignment string LocalAssignment string
ForeignAssignment string ForeignAssignment string
} }
@ -154,8 +163,8 @@ func textsFromRelationship(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRe
} }
r.Function.ForeignName = strmangle.TitleCase(plurality(strings.TrimSuffix(foreignNamingColumn, "_id"))) r.Function.ForeignName = strmangle.TitleCase(plurality(strings.TrimSuffix(foreignNamingColumn, "_id")))
col := table.GetColumn(rel.Column)
if rel.Nullable { if rel.Nullable {
col := table.GetColumn(rel.Column)
r.Function.LocalAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(rel.Column), strings.TrimPrefix(col.Type, "null.")) r.Function.LocalAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(rel.Column), strings.TrimPrefix(col.Type, "null."))
} else { } else {
r.Function.LocalAssignment = strmangle.TitleCase(rel.Column) r.Function.LocalAssignment = strmangle.TitleCase(rel.Column)
@ -163,12 +172,14 @@ func textsFromRelationship(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRe
if rel.ForeignColumnNullable { if rel.ForeignColumnNullable {
foreignTable := bdb.GetTable(tables, rel.ForeignTable) foreignTable := bdb.GetTable(tables, rel.ForeignTable)
col := foreignTable.GetColumn(rel.ForeignColumn) foreignColumn := foreignTable.GetColumn(rel.ForeignColumn)
r.Function.ForeignAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(rel.ForeignColumn), strings.TrimPrefix(col.Type, "null.")) r.Function.ForeignAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(rel.ForeignColumn), strings.TrimPrefix(foreignColumn.Type, "null."))
} else { } else {
r.Function.ForeignAssignment = strmangle.TitleCase(rel.ForeignColumn) r.Function.ForeignAssignment = strmangle.TitleCase(rel.ForeignColumn)
} }
r.Function.UsesBytes = col.Type == "[]byte"
return r return r
} }