Skip to content

Commit

Permalink
Remove additional reliance on the null package
Browse files Browse the repository at this point in the history
We now have the ability to use custom types everywhere. The null package
could be replaced or removed or what have you. But there were subtle hints
everywhere of it's existence. These assumptions have been removed and
in their place we assume that types were are dealing with will implement
sql.Scanner/driver.Valuer so that we can assign and read their values.

This comes with some small performance penalties because more
reflection. Shouldn't be noticeable though.

- Add some functions to help assign/compare and generally work with the
  sql.Scanner and driver.Valuer interfaces.
- Remove all references to null package in the templates.
  • Loading branch information
aarondl committed Jun 8, 2018
1 parent e79bcb9 commit 4f1ec23
Show file tree
Hide file tree
Showing 21 changed files with 654 additions and 212 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ difference.
- The concept of TestMain is now gone from both templates and imports. It's
been superceded by the new driver abilities to supply templates and imports.
The drivers add their mains to the TestSingleton templates.
- Remove the reliance on the null package in the templates. Instead favor
using the sql.Scanner and driver.Valuer interface that most types will
implement anyway (and the null package does too).

### Fixed

Expand Down
62 changes: 23 additions & 39 deletions boilingcore/text_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ type TxtToOne struct {
Name string
ForeignName string

UsesBytes bool

LocalAssignment string
ForeignAssignment string
UsesPrimitives bool
}
}

Expand All @@ -51,23 +48,11 @@ func txtsFromFKey(tables []drivers.Table, table drivers.Table, fkey drivers.Fore

r.Function.Name, r.Function.ForeignName = txtNameToOne(fkey)

if fkey.Nullable {
col := table.GetColumn(fkey.Column)
r.Function.LocalAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(fkey.Column), strings.TrimPrefix(col.Type, "null."))
} else {
r.Function.LocalAssignment = strmangle.TitleCase(fkey.Column)
}

localColumn := table.GetColumn(fkey.Column)
foreignTable := drivers.GetTable(tables, fkey.ForeignTable)
foreignColumn := foreignTable.GetColumn(fkey.ForeignColumn)

if fkey.ForeignColumnNullable {
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"
r.Function.UsesPrimitives = isPrimitive(localColumn.Type) && isPrimitive(foreignColumn.Type)

return r
}
Expand All @@ -87,14 +72,12 @@ func txtsFromOneToOne(tables []drivers.Table, table drivers.Table, oneToOne driv
}

rel := txtsFromFKey(tables, table, fkey)
col := table.GetColumn(oneToOne.Column)

// Reverse foreign key
rel.ForeignKey.Table, rel.ForeignKey.ForeignTable = rel.ForeignKey.ForeignTable, rel.ForeignKey.Table
rel.ForeignKey.Column, rel.ForeignKey.ForeignColumn = rel.ForeignKey.ForeignColumn, rel.ForeignKey.Column
rel.ForeignKey.Nullable, rel.ForeignKey.ForeignColumnNullable = rel.ForeignKey.ForeignColumnNullable, rel.ForeignKey.Nullable
rel.ForeignKey.Unique, rel.ForeignKey.ForeignColumnUnique = rel.ForeignKey.ForeignColumnUnique, rel.ForeignKey.Unique
rel.Function.UsesBytes = col.Type == "[]byte"
rel.Function.ForeignName, rel.Function.Name = txtNameToOne(drivers.ForeignKey{
Table: oneToOne.ForeignTable,
Column: oneToOne.ForeignColumn,
Expand Down Expand Up @@ -124,10 +107,7 @@ type TxtToMany struct {
Name string
ForeignName string

UsesBytes bool

LocalAssignment string
ForeignAssignment string
UsesPrimitives bool
}
}

Expand All @@ -148,21 +128,9 @@ func txtsFromToMany(tables []drivers.Table, table drivers.Table, rel drivers.ToM
r.Function.Name, r.Function.ForeignName = txtNameToMany(rel)

col := table.GetColumn(rel.Column)
if rel.Nullable {
r.Function.LocalAssignment = fmt.Sprintf("%s.%s", strmangle.TitleCase(rel.Column), strings.TrimPrefix(col.Type, "null."))
} else {
r.Function.LocalAssignment = strmangle.TitleCase(rel.Column)
}

if rel.ForeignColumnNullable {
foreignTable := drivers.GetTable(tables, rel.ForeignTable)
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"
foreignTable := drivers.GetTable(tables, rel.ForeignTable)
foreignCol := foreignTable.GetColumn(rel.ForeignColumn)
r.Function.UsesPrimitives = isPrimitive(col.Type) && isPrimitive(foreignCol.Type)

return r
}
Expand Down Expand Up @@ -286,3 +254,19 @@ func trimSuffixes(str string) string {

return str
}

func isPrimitive(typ string) bool {
switch typ {
// Numeric
case "int", "int8", "int16", "int32", "int64":
return true
case "uint", "uint8", "uint16", "uint32", "uint64":
return true
case "float32", "float64":
return true
case "byte", "rune", "string":
return true
}

return false
}
18 changes: 5 additions & 13 deletions boilingcore/text_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ func TestTxtsFromOne(t *testing.T) {

expect.Function.Name = "Pilot"
expect.Function.ForeignName = "Jet"

expect.Function.LocalAssignment = "PilotID.Int"
expect.Function.ForeignAssignment = "ID"
expect.Function.UsesPrimitives = false

if !reflect.DeepEqual(expect, texts) {
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
Expand All @@ -55,9 +53,7 @@ func TestTxtsFromOne(t *testing.T) {

expect.Function.Name = "Airport"
expect.Function.ForeignName = "Jets"

expect.Function.LocalAssignment = "AirportID"
expect.Function.ForeignAssignment = "ID"
expect.Function.UsesPrimitives = true

if !reflect.DeepEqual(expect, texts) {
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
Expand Down Expand Up @@ -104,9 +100,7 @@ func TestTxtsFromOneToOne(t *testing.T) {

expect.Function.Name = "Jet"
expect.Function.ForeignName = "Pilot"

expect.Function.LocalAssignment = "ID"
expect.Function.ForeignAssignment = "PilotID.Int"
expect.Function.UsesPrimitives = false

if !reflect.DeepEqual(expect, texts) {
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
Expand Down Expand Up @@ -135,8 +129,7 @@ func TestTxtsFromMany(t *testing.T) {

expect.Function.Name = "Licenses"
expect.Function.ForeignName = "Pilot"
expect.Function.LocalAssignment = "ID"
expect.Function.ForeignAssignment = "PilotID"
expect.Function.UsesPrimitives = true

if !reflect.DeepEqual(expect, texts) {
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
Expand All @@ -155,8 +148,7 @@ func TestTxtsFromMany(t *testing.T) {

expect.Function.Name = "Languages"
expect.Function.ForeignName = "Pilots"
expect.Function.LocalAssignment = "ID"
expect.Function.ForeignAssignment = "ID"
expect.Function.UsesPrimitives = true

if !reflect.DeepEqual(expect, texts) {
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
Expand Down
2 changes: 1 addition & 1 deletion importers/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ func NewDefaultImports() Collection {

col.All = Set{
Standard: List{
`"bytes"`,
`"database/sql"`,
`"fmt"`,
`"reflect"`,
Expand Down Expand Up @@ -210,6 +209,7 @@ func NewDefaultImports() Collection {
},
ThirdParty: List{
`"github.com/volatiletech/sqlboiler/boil"`,
`"github.com/volatiletech/sqlboiler/queries"`,
`"github.com/volatiletech/sqlboiler/randomize"`,
`"github.com/volatiletech/sqlboiler/strmangle"`,
},
Expand Down
Loading

0 comments on commit 4f1ec23

Please sign in to comment.