Finish text helpers for relationships.
This commit is contained in:
parent
b15889591e
commit
225a74423a
2 changed files with 85 additions and 20 deletions
|
@ -32,9 +32,9 @@ type RelationshipToManyTexts struct {
|
|||
}
|
||||
}
|
||||
|
||||
// createTextsFromRelationship 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.
|
||||
func createTextsFromRelationship(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRelationship) RelationshipToManyTexts {
|
||||
func textsFromRelationship(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRelationship) RelationshipToManyTexts {
|
||||
r := RelationshipToManyTexts{}
|
||||
r.LocalTable.NameSingular = strmangle.Singular(table.Name)
|
||||
r.LocalTable.NameGo = strmangle.TitleCase(r.LocalTable.NameSingular)
|
||||
|
@ -50,7 +50,7 @@ func createTextsFromRelationship(tables []bdb.Table, table bdb.Table, rel bdb.To
|
|||
// Check to see if the foreign key name is the same as the local table name.
|
||||
// Simple case: yes - we can name the function the same as the plural table name
|
||||
// Not simple case: We have to name the function based off the foreign key and
|
||||
if colName := strings.TrimSuffix(rel.ForeignColumn, "_id"); r.LocalTable.NameSingular == colName {
|
||||
if colName := strings.TrimSuffix(rel.ForeignColumn, "_id"); rel.ToJoinTable || r.LocalTable.NameSingular == colName {
|
||||
r.Function.Name = r.ForeignTable.NamePluralGo
|
||||
} else {
|
||||
r.Function.Name = strmangle.TitleCase(colName) + r.ForeignTable.NamePluralGo
|
||||
|
@ -63,16 +63,12 @@ func createTextsFromRelationship(tables []bdb.Table, table bdb.Table, rel bdb.To
|
|||
r.Function.LocalAssignment = strmangle.TitleCase(rel.Column)
|
||||
}
|
||||
|
||||
if !rel.ToJoinTable {
|
||||
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."))
|
||||
} else {
|
||||
r.Function.ForeignAssignment = strmangle.TitleCase(rel.ForeignColumn)
|
||||
}
|
||||
|
||||
return r
|
||||
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."))
|
||||
} else {
|
||||
r.Function.ForeignAssignment = strmangle.TitleCase(rel.ForeignColumn)
|
||||
}
|
||||
|
||||
/*if rel.ForeignColumnNullable {
|
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
|
@ -43,7 +44,7 @@ func (fakeDB) ForeignKeyInfo(tableName string) ([]bdb.ForeignKey, error) {
|
|||
{Name: "notifications_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
|
||||
{Name: "notifications_source_id_fk", Column: "source_id", ForeignTable: "users", ForeignColumn: "id"},
|
||||
},
|
||||
"users_video_tags": []bdb.ForeignKey{
|
||||
"users_videos_tags": []bdb.ForeignKey{
|
||||
{Name: "user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
|
||||
{Name: "video_id_fk", Column: "video_id", ForeignTable: "videos", ForeignColumn: "id"},
|
||||
},
|
||||
|
@ -55,11 +56,19 @@ func (fakeDB) TranslateColumnType(c bdb.Column) bdb.Column {
|
|||
}
|
||||
return c
|
||||
}
|
||||
func (fakeDB) PrimaryKeyInfo(tableName string) (*bdb.PrimaryKey, error) { return nil, nil }
|
||||
func (fakeDB) Open() error { return nil }
|
||||
func (fakeDB) Close() {}
|
||||
func (fakeDB) PrimaryKeyInfo(tableName string) (*bdb.PrimaryKey, error) {
|
||||
fmt.Println(tableName)
|
||||
return map[string]*bdb.PrimaryKey{
|
||||
"users_videos_tags": &bdb.PrimaryKey{
|
||||
Name: "user_video_id_pkey",
|
||||
Columns: []string{"user_id", "video_id"},
|
||||
},
|
||||
}[tableName], nil
|
||||
}
|
||||
func (fakeDB) Open() error { return nil }
|
||||
func (fakeDB) Close() {}
|
||||
|
||||
func TestCreateTextsFromRelationship(t *testing.T) {
|
||||
func TesttextsFromRelationship(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tables, err := bdb.Tables(fakeDB(0))
|
||||
|
@ -67,9 +76,9 @@ func TestCreateTextsFromRelationship(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
spew.Dump(tables)
|
||||
users := bdb.GetTable(tables, "users")
|
||||
texts := createTextsFromRelationship(tables, users, users.ToManyRelationships[0])
|
||||
|
||||
texts := textsFromRelationship(tables, users, users.ToManyRelationships[0])
|
||||
expect := RelationshipToManyTexts{}
|
||||
expect.LocalTable.NameGo = "User"
|
||||
expect.LocalTable.NameSingular = "user"
|
||||
|
@ -88,4 +97,64 @@ func TestCreateTextsFromRelationship(t *testing.T) {
|
|||
if !reflect.DeepEqual(expect, texts) {
|
||||
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
|
||||
}
|
||||
|
||||
texts = textsFromRelationship(tables, users, users.ToManyRelationships[1])
|
||||
expect = RelationshipToManyTexts{}
|
||||
expect.LocalTable.NameGo = "User"
|
||||
expect.LocalTable.NameSingular = "user"
|
||||
|
||||
expect.ForeignTable.NameGo = "Notification"
|
||||
expect.ForeignTable.NameSingular = "notification"
|
||||
expect.ForeignTable.NamePluralGo = "Notifications"
|
||||
expect.ForeignTable.NameHumanReadable = "notifications"
|
||||
expect.ForeignTable.Slice = "notificationSlice"
|
||||
|
||||
expect.Function.Name = "Notifications"
|
||||
expect.Function.Receiver = "u"
|
||||
expect.Function.LocalAssignment = "ID"
|
||||
expect.Function.ForeignAssignment = "UserID"
|
||||
|
||||
if !reflect.DeepEqual(expect, texts) {
|
||||
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
|
||||
}
|
||||
|
||||
texts = textsFromRelationship(tables, users, users.ToManyRelationships[2])
|
||||
expect = RelationshipToManyTexts{}
|
||||
expect.LocalTable.NameGo = "User"
|
||||
expect.LocalTable.NameSingular = "user"
|
||||
|
||||
expect.ForeignTable.NameGo = "Notification"
|
||||
expect.ForeignTable.NameSingular = "notification"
|
||||
expect.ForeignTable.NamePluralGo = "Notifications"
|
||||
expect.ForeignTable.NameHumanReadable = "notifications"
|
||||
expect.ForeignTable.Slice = "notificationSlice"
|
||||
|
||||
expect.Function.Name = "SourceNotifications"
|
||||
expect.Function.Receiver = "u"
|
||||
expect.Function.LocalAssignment = "ID"
|
||||
expect.Function.ForeignAssignment = "SourceID.Int32"
|
||||
|
||||
if !reflect.DeepEqual(expect, texts) {
|
||||
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
|
||||
}
|
||||
|
||||
texts = textsFromRelationship(tables, users, users.ToManyRelationships[3])
|
||||
expect = RelationshipToManyTexts{}
|
||||
expect.LocalTable.NameGo = "User"
|
||||
expect.LocalTable.NameSingular = "user"
|
||||
|
||||
expect.ForeignTable.NameGo = "Video"
|
||||
expect.ForeignTable.NameSingular = "video"
|
||||
expect.ForeignTable.NamePluralGo = "Videos"
|
||||
expect.ForeignTable.NameHumanReadable = "videos"
|
||||
expect.ForeignTable.Slice = "videoSlice"
|
||||
|
||||
expect.Function.Name = "Videos"
|
||||
expect.Function.Receiver = "u"
|
||||
expect.Function.LocalAssignment = "ID"
|
||||
expect.Function.ForeignAssignment = "ID"
|
||||
|
||||
if !reflect.DeepEqual(expect, texts) {
|
||||
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue