Fix one-to-one text_helper ripple
- Rename a bunch of things.
This commit is contained in:
parent
b918e9ef9e
commit
64284a7748
3 changed files with 55 additions and 90 deletions
|
@ -166,11 +166,10 @@ var templateFunctions = template.FuncMap{
|
|||
// Database related mangling
|
||||
"whereClause": strmangle.WhereClause,
|
||||
|
||||
// Text helpers
|
||||
"textsFromForeignKey": textsFromForeignKey,
|
||||
"textsFromOneToOneRelationship": textsFromOneToOneRelationship,
|
||||
"textsFromRelationship": textsFromRelationship,
|
||||
"preserveDot": preserveDot,
|
||||
// Relationship text helpers
|
||||
"textsFromForeignKey": txtsFromFKey,
|
||||
"textsFromOneToOneRelationship": txtsFromOneToOne,
|
||||
"textsFromRelationship": txtsFromToMany,
|
||||
|
||||
// dbdrivers ops
|
||||
"filterColumnsByDefault": bdb.FilterColumnsByDefault,
|
||||
|
|
|
@ -8,8 +8,9 @@ import (
|
|||
"github.com/vattle/sqlboiler/strmangle"
|
||||
)
|
||||
|
||||
// RelationshipToOneTexts contains text that will be used by templates.
|
||||
type RelationshipToOneTexts struct {
|
||||
// TxtToOne contains text that will be used by templates for a one-to-many or
|
||||
// a one-to-one relationship.
|
||||
type TxtToOne struct {
|
||||
ForeignKey bdb.ForeignKey
|
||||
|
||||
LocalTable struct {
|
||||
|
@ -32,7 +33,6 @@ type RelationshipToOneTexts struct {
|
|||
|
||||
Varname string
|
||||
Receiver string
|
||||
OneToOne bool
|
||||
UsesBytes bool
|
||||
|
||||
LocalAssignment string
|
||||
|
@ -40,8 +40,8 @@ type RelationshipToOneTexts struct {
|
|||
}
|
||||
}
|
||||
|
||||
func textsFromForeignKey(packageName string, tables []bdb.Table, table bdb.Table, fkey bdb.ForeignKey) RelationshipToOneTexts {
|
||||
r := RelationshipToOneTexts{}
|
||||
func txtsFromFKey(packageName string, tables []bdb.Table, table bdb.Table, fkey bdb.ForeignKey) TxtToOne {
|
||||
r := TxtToOne{}
|
||||
|
||||
r.ForeignKey = fkey
|
||||
|
||||
|
@ -85,32 +85,37 @@ func textsFromForeignKey(packageName string, tables []bdb.Table, table bdb.Table
|
|||
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{
|
||||
Table: toMany.Table,
|
||||
Table: oneToOne.Table,
|
||||
Name: "none",
|
||||
Column: toMany.Column,
|
||||
Nullable: toMany.Nullable,
|
||||
Unique: toMany.Unique,
|
||||
Column: oneToOne.Column,
|
||||
Nullable: oneToOne.Nullable,
|
||||
Unique: oneToOne.Unique,
|
||||
|
||||
ForeignTable: toMany.ForeignTable,
|
||||
ForeignColumn: toMany.ForeignColumn,
|
||||
ForeignColumnNullable: toMany.ForeignColumnNullable,
|
||||
ForeignColumnUnique: toMany.ForeignColumnUnique,
|
||||
ForeignTable: oneToOne.ForeignTable,
|
||||
ForeignColumn: oneToOne.ForeignColumn,
|
||||
ForeignColumnNullable: oneToOne.ForeignColumnNullable,
|
||||
ForeignColumnUnique: oneToOne.ForeignColumnUnique,
|
||||
}
|
||||
|
||||
rel := textsFromForeignKey(packageName, tables, table, fkey)
|
||||
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
|
||||
rel := txtsFromFKey(packageName, tables, table, fkey)
|
||||
col := table.GetColumn(oneToOne.Column)
|
||||
|
||||
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"
|
||||
return rel
|
||||
}
|
||||
|
||||
// RelationshipToManyTexts contains text that will be used by templates.
|
||||
type RelationshipToManyTexts struct {
|
||||
// TxtToMany contains text that will be used by many-to-one relationships.
|
||||
type TxtToMany struct {
|
||||
LocalTable struct {
|
||||
NameGo string
|
||||
NameSingular string
|
||||
|
@ -140,8 +145,8 @@ type RelationshipToManyTexts struct {
|
|||
|
||||
// textsFromRelationship creates a struct that does a lot of the text
|
||||
// transformation in advance for a given relationship.
|
||||
func textsFromRelationship(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRelationship) RelationshipToManyTexts {
|
||||
r := RelationshipToManyTexts{}
|
||||
func txtsFromToMany(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRelationship) TxtToMany {
|
||||
r := TxtToMany{}
|
||||
r.LocalTable.NameSingular = strmangle.Singular(table.Name)
|
||||
r.LocalTable.NameGo = strmangle.TitleCase(r.LocalTable.NameSingular)
|
||||
r.LocalTable.ColumnNameGo = strmangle.TitleCase(rel.Column)
|
||||
|
@ -194,17 +199,3 @@ func mkFunctionName(fkeyTableSingular, foreignTablePluralGo, fkeyColumn string,
|
|||
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
"github.com/vattle/sqlboiler/bdb/drivers"
|
||||
)
|
||||
|
||||
func TestTextsFromForeignKey(t *testing.T) {
|
||||
func TestTxtsFromOne(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tables, err := bdb.Tables(&drivers.MockDriver{}, "public", nil, nil)
|
||||
|
@ -18,8 +18,8 @@ func TestTextsFromForeignKey(t *testing.T) {
|
|||
}
|
||||
|
||||
jets := bdb.GetTable(tables, "jets")
|
||||
texts := textsFromForeignKey("models", tables, jets, jets.FKeys[0])
|
||||
expect := RelationshipToOneTexts{}
|
||||
texts := txtsFromFKey("models", tables, jets, jets.FKeys[0])
|
||||
expect := TxtToOne{}
|
||||
|
||||
expect.ForeignKey = jets.FKeys[0]
|
||||
|
||||
|
@ -37,7 +37,6 @@ func TestTextsFromForeignKey(t *testing.T) {
|
|||
expect.Function.ForeignName = "Jet"
|
||||
expect.Function.Varname = "pilot"
|
||||
expect.Function.Receiver = "j"
|
||||
expect.Function.OneToOne = false
|
||||
|
||||
expect.Function.LocalAssignment = "PilotID.Int"
|
||||
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))
|
||||
}
|
||||
|
||||
texts = textsFromForeignKey("models", tables, jets, jets.FKeys[1])
|
||||
expect = RelationshipToOneTexts{}
|
||||
texts = txtsFromFKey("models", tables, jets, jets.FKeys[1])
|
||||
expect = TxtToOne{}
|
||||
expect.ForeignKey = jets.FKeys[1]
|
||||
|
||||
expect.LocalTable.NameGo = "Jet"
|
||||
|
@ -64,7 +63,6 @@ func TestTextsFromForeignKey(t *testing.T) {
|
|||
expect.Function.ForeignName = "Jets"
|
||||
expect.Function.Varname = "airport"
|
||||
expect.Function.Receiver = "j"
|
||||
expect.Function.OneToOne = false
|
||||
|
||||
expect.Function.LocalAssignment = "AirportID"
|
||||
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()
|
||||
|
||||
tables, err := bdb.Tables(&drivers.MockDriver{}, "public", nil, nil)
|
||||
|
@ -87,20 +85,21 @@ func TestTextsFromOneToOneRelationship(t *testing.T) {
|
|||
}
|
||||
|
||||
pilots := bdb.GetTable(tables, "pilots")
|
||||
texts := textsFromOneToOneRelationship("models", tables, pilots, pilots.ToManyRelationships[0])
|
||||
expect := RelationshipToOneTexts{}
|
||||
texts := txtsFromOneToOne("models", tables, pilots, pilots.ToOneRelationships[0])
|
||||
expect := TxtToOne{}
|
||||
|
||||
expect.ForeignKey = bdb.ForeignKey{
|
||||
Table: "pilots",
|
||||
Name: "none",
|
||||
Column: "id",
|
||||
Nullable: false,
|
||||
Unique: false,
|
||||
Name: "none",
|
||||
|
||||
ForeignTable: "jets",
|
||||
ForeignColumn: "pilot_id",
|
||||
ForeignColumnNullable: true,
|
||||
ForeignColumnUnique: true,
|
||||
Table: "jets",
|
||||
Column: "pilot_id",
|
||||
Nullable: true,
|
||||
Unique: true,
|
||||
|
||||
ForeignTable: "pilots",
|
||||
ForeignColumn: "id",
|
||||
ForeignColumnNullable: false,
|
||||
ForeignColumnUnique: false,
|
||||
}
|
||||
|
||||
expect.LocalTable.NameGo = "Pilot"
|
||||
|
@ -117,7 +116,6 @@ func TestTextsFromOneToOneRelationship(t *testing.T) {
|
|||
expect.Function.ForeignName = "Pilot"
|
||||
expect.Function.Varname = "jet"
|
||||
expect.Function.Receiver = "p"
|
||||
expect.Function.OneToOne = true
|
||||
|
||||
expect.Function.LocalAssignment = "ID"
|
||||
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()
|
||||
|
||||
tables, err := bdb.Tables(&drivers.MockDriver{}, "public", nil, nil)
|
||||
|
@ -136,31 +134,8 @@ func TestTextsFromRelationship(t *testing.T) {
|
|||
}
|
||||
|
||||
pilots := bdb.GetTable(tables, "pilots")
|
||||
texts := textsFromRelationship(tables, pilots, pilots.ToManyRelationships[0])
|
||||
expect := RelationshipToManyTexts{}
|
||||
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{}
|
||||
texts := txtsFromToMany(tables, pilots, pilots.ToManyRelationships[0])
|
||||
expect := TxtToMany{}
|
||||
expect.LocalTable.NameGo = "Pilot"
|
||||
expect.LocalTable.NameSingular = "pilot"
|
||||
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))
|
||||
}
|
||||
|
||||
texts = textsFromRelationship(tables, pilots, pilots.ToManyRelationships[2])
|
||||
expect = RelationshipToManyTexts{}
|
||||
texts = txtsFromToMany(tables, pilots, pilots.ToManyRelationships[1])
|
||||
expect = TxtToMany{}
|
||||
expect.LocalTable.NameGo = "Pilot"
|
||||
expect.LocalTable.NameSingular = "pilot"
|
||||
expect.LocalTable.ColumnNameGo = "ID"
|
||||
|
|
Loading…
Reference in a new issue