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.
// Types are Go types, converted by TranslateColumnType.
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
Type string
DBType string
@ -17,6 +12,13 @@ type Column struct {
Nullable bool
Unique 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.

View file

@ -3,10 +3,7 @@
{{- $varNameSingular := $dot.Table.Name | singular | camelCase -}}
{{- with .Rel -}}
{{- $arg := printf "maybe%s" .LocalTable.NameGo -}}
{{- $slice := printf "%sSlice" .LocalTable.NameGo -}}
{{- $foreignTable := getTable $dot.Tables .ForeignKey.ForeignTable -}}
{{- $foreignTableFKeyCol := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}}
{{- $usesBytes := eq "[]byte" $foreignTableFKeyCol.Type}}
{{- $slice := printf "%sSlice" .LocalTable.NameGo}}
// Load{{.Function.Name}} allows an eager lookup of values, cached into the
// loaded structs of the objects.
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 _, local := range slice {
{{if $usesBytes -}}
{{if .Function.UsesBytes -}}
if 0 == bytes.Compare(local.{{.Function.LocalAssignment}}, foreign.{{.Function.ForeignAssignment}}) {
{{else -}}
if local.{{.Function.LocalAssignment}} == foreign.{{.Function.ForeignAssignment}} {

View file

@ -13,9 +13,7 @@
{{- $txt := textsFromRelationship $dot.Tables $dot.Table . -}}
{{- $arg := printf "maybe%s" $txt.LocalTable.NameGo -}}
{{- $slice := printf "%sSlice" $txt.LocalTable.NameGo -}}
{{- $schemaForeignTable := .ForeignTable | $dot.SchemaTable -}}
{{- $fkeyCol := $dot.Table.GetColumn .Column -}}
{{- $usesBytes := eq "[]byte" $fkeyCol.Type -}}
{{- $schemaForeignTable := .ForeignTable | $dot.SchemaTable}}
// Load{{$txt.Function.Name}} allows an eager lookup of values, cached into the
// loaded structs of the objects.
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 {
localJoinCol := localJoinCols[i]
for _, local := range slice {
{{if $usesBytes -}}
{{if $txt.Function.UsesBytes -}}
if 0 == bytes.Compare(local.{{$txt.Function.LocalAssignment}}, localJoinCol) {
{{else -}}
if local.{{$txt.Function.LocalAssignment}} == localJoinCol {
@ -128,7 +126,7 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singula
{{else -}}
for _, foreign := range resultSlice {
for _, local := range slice {
{{if $usesBytes -}}
{{if $txt.Function.UsesBytes -}}
if 0 == bytes.Compare(local.{{$txt.Function.LocalAssignment}}, foreign.{{$txt.Function.ForeignAssignment}}) {
{{else -}}
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 */}}
{{- with .Rel -}}
{{- $varNameSingular := .ForeignKey.ForeignTable | singular | camelCase -}}
{{- $localNameSingular := .ForeignKey.Table | singular | camelCase -}}
{{- $foreignTable := getTable $dot.Tables .ForeignKey.ForeignTable -}}
{{- $foreignTableFKeyCol := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}}
{{- $usesBytes := eq "[]byte" $foreignTableFKeyCol.Type -}}
{{- $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.
// 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
{{else -}}
for i, ri := range related.R.{{.Function.ForeignName}} {
{{if $usesBytes -}}
{{if .Function.UsesBytes -}}
if 0 != bytes.Compare({{.Function.Receiver}}.{{.Function.LocalAssignment}}, ri.{{.Function.LocalAssignment}}) {
{{else -}}
if {{.Function.Receiver}}.{{.Function.LocalAssignment}} != ri.{{.Function.LocalAssignment}} {

View file

@ -7,9 +7,7 @@
{{- $txt := textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table . -}}
{{- template "relationship_to_one_test_helper" (preserveDot $dot $txt) -}}
{{- else -}}
{{- $rel := textsFromRelationship $dot.Tables $table . -}}
{{- $fkeyCol := $dot.Table.GetColumn .Column -}}
{{- $usesBytes := eq "[]byte" $fkeyCol.Type}}
{{- $rel := textsFromRelationship $dot.Tables $table .}}
func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
var err error
tx := MustTx(boil.Begin())
@ -23,8 +21,8 @@ func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
}
seed := randomize.NewSeed()
randomize.Struct(seed, &b, {{$rel.ForeignTable.NameSingular | camelCase}}DBTypes, false{{if not $usesBytes}}, "{{.ForeignColumn}}"{{end}})
randomize.Struct(seed, &c, {{$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 $rel.Function.UsesBytes}}, "{{.ForeignColumn}}"{{end}})
{{if .Nullable -}}
a.{{.Column | titleCase}}.Valid = true
{{- end}}
@ -62,7 +60,7 @@ func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
bFound, cFound := false, false
for _, v := range {{$varname}} {
{{if $usesBytes -}}
{{if $rel.Function.UsesBytes -}}
if 0 == bytes.Compare(v.{{$rel.Function.ForeignAssignment}}, b.{{$rel.Function.ForeignAssignment}}) {
bFound = true
}

View file

@ -9,9 +9,7 @@
{{- else -}}
{{- $varNameSingular := .Table | singular | camelCase -}}
{{- $foreignVarNameSingular := .ForeignTable | singular | camelCase -}}
{{- $rel := textsFromRelationship $dot.Tables $table . -}}
{{- $fkeyCol := $dot.Table.GetColumn .Column -}}
{{- $usesBytes := eq "[]byte" $fkeyCol.Type}}
{{- $rel := textsFromRelationship $dot.Tables $table .}}
func test{{$rel.LocalTable.NameGo}}ToManyAddOp{{$rel.Function.Name}}(t *testing.T) {
var err error
@ -65,7 +63,7 @@ func test{{$rel.LocalTable.NameGo}}ToManyAddOp{{$rel.Function.Name}}(t *testing.
}
{{- else}}
{{if $usesBytes -}}
{{if $rel.Function.UsesBytes -}}
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}})
}
@ -185,7 +183,7 @@ 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 $usesBytes -}}
{{if $rel.Function.UsesBytes -}}
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}})
}

View file

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

View file

@ -2,10 +2,7 @@
{{- $dot := .Dot -}}
{{- with .Rel -}}
{{- $varNameSingular := .ForeignKey.Table | singular | camelCase -}}
{{- $foreignVarNameSingular := .ForeignKey.ForeignTable | singular | camelCase -}}
{{- $foreignTable := getTable $dot.Tables .ForeignKey.ForeignTable -}}
{{- $foreignTableFKeyCol := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}}
{{- $usesBytes := eq "[]byte" $foreignTableFKeyCol.Type -}}
{{- $foreignVarNameSingular := .ForeignKey.ForeignTable | singular | camelCase}}
func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) {
var err error
@ -39,7 +36,7 @@ func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Na
t.Fatal(err)
}
{{if $usesBytes -}}
{{if .Function.UsesBytes -}}
if 0 != bytes.Compare(a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}}) {
{{else -}}
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)
}
{{if $usesBytes -}}
{{if .Function.UsesBytes -}}
if 0 != bytes.Compare(a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}}) {
{{else -}}
if a.{{.Function.LocalAssignment}} != x.{{.Function.ForeignAssignment}} {

View file

@ -30,9 +30,10 @@ type RelationshipToOneTexts struct {
Name string
ForeignName string
Varname string
Receiver string
OneToOne bool
Varname string
Receiver string
OneToOne bool
UsesBytes bool
LocalAssignment string
ForeignAssignment string
@ -70,14 +71,17 @@ func textsFromForeignKey(packageName string, tables []bdb.Table, table bdb.Table
r.Function.LocalAssignment = strmangle.TitleCase(fkey.Column)
}
foreignTable := bdb.GetTable(tables, fkey.ForeignTable)
foreignColumn := foreignTable.GetColumn(fkey.ForeignColumn)
if fkey.ForeignColumnNullable {
foreignTable := bdb.GetTable(tables, fkey.ForeignTable)
col := foreignTable.GetColumn(fkey.ForeignColumn)
r.Function.ForeignAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(fkey.ForeignColumn), strings.TrimPrefix(col.Type, "null."))
r.Function.ForeignAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(fkey.ForeignColumn), strings.TrimPrefix(foreignColumn.Type, "null."))
} else {
r.Function.ForeignAssignment = strmangle.TitleCase(fkey.ForeignColumn)
}
r.Function.UsesBytes = foreignColumn.Type == "[]byte"
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.ForeignName = mkFunctionName(strmangle.Singular(toMany.Table), strmangle.TitleCase(strmangle.Singular(toMany.Table)), toMany.ForeignColumn, false)
rel.Function.OneToOne = true
col := table.GetColumn(toMany.Column)
rel.Function.UsesBytes = col.Type == "[]byte"
return rel
}
@ -124,6 +131,8 @@ type RelationshipToManyTexts struct {
ForeignName string
Receiver string
UsesBytes bool
LocalAssignment 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")))
col := table.GetColumn(rel.Column)
if rel.Nullable {
col := table.GetColumn(rel.Column)
r.Function.LocalAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(rel.Column), strings.TrimPrefix(col.Type, "null."))
} else {
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 {
foreignTable := bdb.GetTable(tables, rel.ForeignTable)
col := foreignTable.GetColumn(rel.ForeignColumn)
r.Function.ForeignAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(rel.ForeignColumn), strings.TrimPrefix(col.Type, "null."))
foreignColumn := foreignTable.GetColumn(rel.ForeignColumn)
r.Function.ForeignAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(rel.ForeignColumn), strings.TrimPrefix(foreignColumn.Type, "null."))
} else {
r.Function.ForeignAssignment = strmangle.TitleCase(rel.ForeignColumn)
}
r.Function.UsesBytes = col.Type == "[]byte"
return r
}