Fix one-to-one text_helper ripple

- Rename a bunch of things.
This commit is contained in:
Aaron L 2016-09-17 23:36:10 -07:00
parent b918e9ef9e
commit 64284a7748
3 changed files with 55 additions and 90 deletions

View file

@ -166,11 +166,10 @@ var templateFunctions = template.FuncMap{
// Database related mangling // Database related mangling
"whereClause": strmangle.WhereClause, "whereClause": strmangle.WhereClause,
// Text helpers // Relationship text helpers
"textsFromForeignKey": textsFromForeignKey, "textsFromForeignKey": txtsFromFKey,
"textsFromOneToOneRelationship": textsFromOneToOneRelationship, "textsFromOneToOneRelationship": txtsFromOneToOne,
"textsFromRelationship": textsFromRelationship, "textsFromRelationship": txtsFromToMany,
"preserveDot": preserveDot,
// dbdrivers ops // dbdrivers ops
"filterColumnsByDefault": bdb.FilterColumnsByDefault, "filterColumnsByDefault": bdb.FilterColumnsByDefault,

View file

@ -8,8 +8,9 @@ import (
"github.com/vattle/sqlboiler/strmangle" "github.com/vattle/sqlboiler/strmangle"
) )
// RelationshipToOneTexts contains text that will be used by templates. // TxtToOne contains text that will be used by templates for a one-to-many or
type RelationshipToOneTexts struct { // a one-to-one relationship.
type TxtToOne struct {
ForeignKey bdb.ForeignKey ForeignKey bdb.ForeignKey
LocalTable struct { LocalTable struct {
@ -32,7 +33,6 @@ type RelationshipToOneTexts struct {
Varname string Varname string
Receiver string Receiver string
OneToOne bool
UsesBytes bool UsesBytes bool
LocalAssignment string LocalAssignment string
@ -40,8 +40,8 @@ type RelationshipToOneTexts struct {
} }
} }
func textsFromForeignKey(packageName string, tables []bdb.Table, table bdb.Table, fkey bdb.ForeignKey) RelationshipToOneTexts { func txtsFromFKey(packageName string, tables []bdb.Table, table bdb.Table, fkey bdb.ForeignKey) TxtToOne {
r := RelationshipToOneTexts{} r := TxtToOne{}
r.ForeignKey = fkey r.ForeignKey = fkey
@ -85,32 +85,37 @@ func textsFromForeignKey(packageName string, tables []bdb.Table, table bdb.Table
return r return r
} }
func textsFromOneToOneRelationship(packageName string, tables []bdb.Table, table bdb.Table, toMany bdb.ToManyRelationship) RelationshipToOneTexts { func txtsFromOneToOne(packageName string, tables []bdb.Table, table bdb.Table, oneToOne bdb.ToOneRelationship) TxtToOne {
fkey := bdb.ForeignKey{ fkey := bdb.ForeignKey{
Table: toMany.Table, Table: oneToOne.Table,
Name: "none", Name: "none",
Column: toMany.Column, Column: oneToOne.Column,
Nullable: toMany.Nullable, Nullable: oneToOne.Nullable,
Unique: toMany.Unique, Unique: oneToOne.Unique,
ForeignTable: toMany.ForeignTable, ForeignTable: oneToOne.ForeignTable,
ForeignColumn: toMany.ForeignColumn, ForeignColumn: oneToOne.ForeignColumn,
ForeignColumnNullable: toMany.ForeignColumnNullable, ForeignColumnNullable: oneToOne.ForeignColumnNullable,
ForeignColumnUnique: toMany.ForeignColumnUnique, ForeignColumnUnique: oneToOne.ForeignColumnUnique,
} }
rel := textsFromForeignKey(packageName, tables, table, fkey) rel := txtsFromFKey(packageName, tables, table, fkey)
rel.Function.Name = strmangle.TitleCase(strmangle.Singular(toMany.ForeignTable)) col := table.GetColumn(oneToOne.Column)
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) // 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.Name = strmangle.TitleCase(strmangle.Singular(oneToOne.ForeignTable))
rel.Function.ForeignName = mkFunctionName(strmangle.Singular(oneToOne.Table), strmangle.TitleCase(strmangle.Singular(oneToOne.Table)), oneToOne.ForeignColumn, false)
rel.Function.UsesBytes = col.Type == "[]byte" rel.Function.UsesBytes = col.Type == "[]byte"
return rel return rel
} }
// RelationshipToManyTexts contains text that will be used by templates. // TxtToMany contains text that will be used by many-to-one relationships.
type RelationshipToManyTexts struct { type TxtToMany struct {
LocalTable struct { LocalTable struct {
NameGo string NameGo string
NameSingular string NameSingular string
@ -140,8 +145,8 @@ type RelationshipToManyTexts struct {
// textsFromRelationship creates a struct that does a lot of the text // textsFromRelationship creates a struct that does a lot of the text
// transformation in advance for a given relationship. // transformation in advance for a given relationship.
func textsFromRelationship(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRelationship) RelationshipToManyTexts { func txtsFromToMany(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRelationship) TxtToMany {
r := RelationshipToManyTexts{} r := TxtToMany{}
r.LocalTable.NameSingular = strmangle.Singular(table.Name) r.LocalTable.NameSingular = strmangle.Singular(table.Name)
r.LocalTable.NameGo = strmangle.TitleCase(r.LocalTable.NameSingular) r.LocalTable.NameGo = strmangle.TitleCase(r.LocalTable.NameSingular)
r.LocalTable.ColumnNameGo = strmangle.TitleCase(rel.Column) r.LocalTable.ColumnNameGo = strmangle.TitleCase(rel.Column)
@ -194,17 +199,3 @@ func mkFunctionName(fkeyTableSingular, foreignTablePluralGo, fkeyColumn string,
return strmangle.TitleCase(colName) + foreignTablePluralGo return strmangle.TitleCase(colName) + foreignTablePluralGo
} }
// PreserveDot allows us to pass in templateData to relationship templates
// called with the template function.
type PreserveDot struct {
Dot templateData
Rel RelationshipToOneTexts
}
func preserveDot(data templateData, obj RelationshipToOneTexts) PreserveDot {
return PreserveDot{
Dot: data,
Rel: obj,
}
}

View file

@ -9,7 +9,7 @@ import (
"github.com/vattle/sqlboiler/bdb/drivers" "github.com/vattle/sqlboiler/bdb/drivers"
) )
func TestTextsFromForeignKey(t *testing.T) { func TestTxtsFromOne(t *testing.T) {
t.Parallel() t.Parallel()
tables, err := bdb.Tables(&drivers.MockDriver{}, "public", nil, nil) tables, err := bdb.Tables(&drivers.MockDriver{}, "public", nil, nil)
@ -18,8 +18,8 @@ func TestTextsFromForeignKey(t *testing.T) {
} }
jets := bdb.GetTable(tables, "jets") jets := bdb.GetTable(tables, "jets")
texts := textsFromForeignKey("models", tables, jets, jets.FKeys[0]) texts := txtsFromFKey("models", tables, jets, jets.FKeys[0])
expect := RelationshipToOneTexts{} expect := TxtToOne{}
expect.ForeignKey = jets.FKeys[0] expect.ForeignKey = jets.FKeys[0]
@ -37,7 +37,6 @@ func TestTextsFromForeignKey(t *testing.T) {
expect.Function.ForeignName = "Jet" expect.Function.ForeignName = "Jet"
expect.Function.Varname = "pilot" expect.Function.Varname = "pilot"
expect.Function.Receiver = "j" expect.Function.Receiver = "j"
expect.Function.OneToOne = false
expect.Function.LocalAssignment = "PilotID.Int" expect.Function.LocalAssignment = "PilotID.Int"
expect.Function.ForeignAssignment = "ID" expect.Function.ForeignAssignment = "ID"
@ -46,8 +45,8 @@ func TestTextsFromForeignKey(t *testing.T) {
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts)) t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
} }
texts = textsFromForeignKey("models", tables, jets, jets.FKeys[1]) texts = txtsFromFKey("models", tables, jets, jets.FKeys[1])
expect = RelationshipToOneTexts{} expect = TxtToOne{}
expect.ForeignKey = jets.FKeys[1] expect.ForeignKey = jets.FKeys[1]
expect.LocalTable.NameGo = "Jet" expect.LocalTable.NameGo = "Jet"
@ -64,7 +63,6 @@ func TestTextsFromForeignKey(t *testing.T) {
expect.Function.ForeignName = "Jets" expect.Function.ForeignName = "Jets"
expect.Function.Varname = "airport" expect.Function.Varname = "airport"
expect.Function.Receiver = "j" expect.Function.Receiver = "j"
expect.Function.OneToOne = false
expect.Function.LocalAssignment = "AirportID" expect.Function.LocalAssignment = "AirportID"
expect.Function.ForeignAssignment = "ID" expect.Function.ForeignAssignment = "ID"
@ -78,7 +76,7 @@ func TestTextsFromForeignKey(t *testing.T) {
} }
} }
func TestTextsFromOneToOneRelationship(t *testing.T) { func TestTxtsFromOneToOne(t *testing.T) {
t.Parallel() t.Parallel()
tables, err := bdb.Tables(&drivers.MockDriver{}, "public", nil, nil) tables, err := bdb.Tables(&drivers.MockDriver{}, "public", nil, nil)
@ -87,20 +85,21 @@ func TestTextsFromOneToOneRelationship(t *testing.T) {
} }
pilots := bdb.GetTable(tables, "pilots") pilots := bdb.GetTable(tables, "pilots")
texts := textsFromOneToOneRelationship("models", tables, pilots, pilots.ToManyRelationships[0]) texts := txtsFromOneToOne("models", tables, pilots, pilots.ToOneRelationships[0])
expect := RelationshipToOneTexts{} expect := TxtToOne{}
expect.ForeignKey = bdb.ForeignKey{ expect.ForeignKey = bdb.ForeignKey{
Table: "pilots", Name: "none",
Name: "none",
Column: "id",
Nullable: false,
Unique: false,
ForeignTable: "jets", Table: "jets",
ForeignColumn: "pilot_id", Column: "pilot_id",
ForeignColumnNullable: true, Nullable: true,
ForeignColumnUnique: true, Unique: true,
ForeignTable: "pilots",
ForeignColumn: "id",
ForeignColumnNullable: false,
ForeignColumnUnique: false,
} }
expect.LocalTable.NameGo = "Pilot" expect.LocalTable.NameGo = "Pilot"
@ -117,7 +116,6 @@ func TestTextsFromOneToOneRelationship(t *testing.T) {
expect.Function.ForeignName = "Pilot" expect.Function.ForeignName = "Pilot"
expect.Function.Varname = "jet" expect.Function.Varname = "jet"
expect.Function.Receiver = "p" expect.Function.Receiver = "p"
expect.Function.OneToOne = true
expect.Function.LocalAssignment = "ID" expect.Function.LocalAssignment = "ID"
expect.Function.ForeignAssignment = "PilotID.Int" expect.Function.ForeignAssignment = "PilotID.Int"
@ -127,7 +125,7 @@ func TestTextsFromOneToOneRelationship(t *testing.T) {
} }
} }
func TestTextsFromRelationship(t *testing.T) { func TestTxtsFromMany(t *testing.T) {
t.Parallel() t.Parallel()
tables, err := bdb.Tables(&drivers.MockDriver{}, "public", nil, nil) tables, err := bdb.Tables(&drivers.MockDriver{}, "public", nil, nil)
@ -136,31 +134,8 @@ func TestTextsFromRelationship(t *testing.T) {
} }
pilots := bdb.GetTable(tables, "pilots") pilots := bdb.GetTable(tables, "pilots")
texts := textsFromRelationship(tables, pilots, pilots.ToManyRelationships[0]) texts := txtsFromToMany(tables, pilots, pilots.ToManyRelationships[0])
expect := RelationshipToManyTexts{} expect := TxtToMany{}
expect.LocalTable.NameGo = "Pilot"
expect.LocalTable.NameSingular = "pilot"
expect.LocalTable.ColumnNameGo = "ID"
expect.ForeignTable.NameGo = "Jet"
expect.ForeignTable.NameSingular = "jet"
expect.ForeignTable.NamePluralGo = "Jets"
expect.ForeignTable.NameHumanReadable = "jets"
expect.ForeignTable.ColumnNameGo = "PilotID"
expect.ForeignTable.Slice = "JetSlice"
expect.Function.Name = "Jets"
expect.Function.ForeignName = "Pilot"
expect.Function.Receiver = "p"
expect.Function.LocalAssignment = "ID"
expect.Function.ForeignAssignment = "PilotID.Int"
if !reflect.DeepEqual(expect, texts) {
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
}
texts = textsFromRelationship(tables, pilots, pilots.ToManyRelationships[1])
expect = RelationshipToManyTexts{}
expect.LocalTable.NameGo = "Pilot" expect.LocalTable.NameGo = "Pilot"
expect.LocalTable.NameSingular = "pilot" expect.LocalTable.NameSingular = "pilot"
expect.LocalTable.ColumnNameGo = "ID" expect.LocalTable.ColumnNameGo = "ID"
@ -182,8 +157,8 @@ func TestTextsFromRelationship(t *testing.T) {
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts)) t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
} }
texts = textsFromRelationship(tables, pilots, pilots.ToManyRelationships[2]) texts = txtsFromToMany(tables, pilots, pilots.ToManyRelationships[1])
expect = RelationshipToManyTexts{} expect = TxtToMany{}
expect.LocalTable.NameGo = "Pilot" expect.LocalTable.NameGo = "Pilot"
expect.LocalTable.NameSingular = "pilot" expect.LocalTable.NameSingular = "pilot"
expect.LocalTable.ColumnNameGo = "ID" expect.LocalTable.ColumnNameGo = "ID"