Another iteration on getting text helpers correct

This commit is contained in:
Aaron L 2016-08-27 17:20:26 -07:00
parent 6c742e29e9
commit d778401a7b
2 changed files with 16 additions and 6 deletions

View file

@ -28,6 +28,7 @@ type RelationshipToOneTexts struct {
Function struct { Function struct {
PackageName string PackageName string
Name string Name string
ForeignName string
Varname string Varname string
Receiver string Receiver string
@ -54,6 +55,7 @@ func textsFromForeignKey(packageName string, tables []bdb.Table, table bdb.Table
r.Function.PackageName = packageName r.Function.PackageName = packageName
r.Function.Name = strmangle.TitleCase(strmangle.Singular(strings.TrimSuffix(fkey.Column, "_id"))) r.Function.Name = strmangle.TitleCase(strmangle.Singular(strings.TrimSuffix(fkey.Column, "_id")))
r.Function.ForeignName = mkFunctionName(strmangle.Singular(fkey.ForeignTable), strmangle.TitleCase(strmangle.Plural(fkey.Table)), fkey.Column, false)
r.Function.Varname = strmangle.CamelCase(strmangle.Singular(fkey.ForeignTable)) r.Function.Varname = strmangle.CamelCase(strmangle.Singular(fkey.ForeignTable))
r.Function.Receiver = strings.ToLower(table.Name[:1]) r.Function.Receiver = strings.ToLower(table.Name[:1])
@ -91,6 +93,7 @@ func textsFromOneToOneRelationship(packageName string, tables []bdb.Table, table
rel := textsFromForeignKey(packageName, tables, table, fkey) rel := textsFromForeignKey(packageName, tables, table, fkey)
rel.Function.Name = strmangle.TitleCase(strmangle.Singular(toMany.ForeignTable)) 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.ReverseInserts = true rel.Function.ReverseInserts = true
return rel return rel
} }
@ -114,6 +117,7 @@ type RelationshipToManyTexts struct {
Function struct { Function struct {
Name string Name string
ForeignName string
Receiver string Receiver string
LocalAssignment string LocalAssignment string
@ -138,6 +142,7 @@ func textsFromRelationship(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRe
r.Function.Receiver = strings.ToLower(table.Name[:1]) r.Function.Receiver = strings.ToLower(table.Name[:1])
r.Function.Name = mkFunctionName(r.LocalTable.NameSingular, r.ForeignTable.NamePluralGo, rel.ForeignColumn, rel.ToJoinTable) r.Function.Name = mkFunctionName(r.LocalTable.NameSingular, r.ForeignTable.NamePluralGo, rel.ForeignColumn, rel.ToJoinTable)
r.Function.ForeignName = strmangle.TitleCase(strmangle.Plural(table.Name))
if rel.Nullable { if rel.Nullable {
col := table.GetColumn(rel.Column) col := table.GetColumn(rel.Column)
@ -160,9 +165,9 @@ func textsFromRelationship(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRe
// mkFunctionName checks to see if the foreign key name is the same as the local table name (minus _id suffix) // mkFunctionName checks to see if the foreign key name is the same as the local table name (minus _id suffix)
// Simple case: yes - we can name the function the same as the plural 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 the foreign table name // Not simple case: We have to name the function based off the foreign key and the foreign table name
func mkFunctionName(localTableSingular, foreignTablePluralGo, foreignColumn string, toJoinTable bool) string { func mkFunctionName(fkeyTableSingular, foreignTablePluralGo, fkeyColumn string, toJoinTable bool) string {
colName := strings.TrimSuffix(foreignColumn, "_id") colName := strings.TrimSuffix(fkeyColumn, "_id")
if toJoinTable || localTableSingular == colName { if toJoinTable || fkeyTableSingular == colName {
return foreignTablePluralGo return foreignTablePluralGo
} }

View file

@ -34,6 +34,7 @@ func TestTextsFromForeignKey(t *testing.T) {
expect.Function.PackageName = "models" expect.Function.PackageName = "models"
expect.Function.Name = "Pilot" expect.Function.Name = "Pilot"
expect.Function.ForeignName = "Jets"
expect.Function.Varname = "pilot" expect.Function.Varname = "pilot"
expect.Function.Receiver = "j" expect.Function.Receiver = "j"
expect.Function.ReverseInserts = false expect.Function.ReverseInserts = false
@ -59,7 +60,7 @@ func TestTextsFromOneToOneRelationship(t *testing.T) {
expect := RelationshipToOneTexts{} expect := RelationshipToOneTexts{}
expect.ForeignKey = bdb.ForeignKey{ expect.ForeignKey = bdb.ForeignKey{
Table: "jets", Table: "pilots",
Name: "none", Name: "none",
Column: "id", Column: "id",
Nullable: false, Nullable: false,
@ -82,6 +83,7 @@ func TestTextsFromOneToOneRelationship(t *testing.T) {
expect.Function.PackageName = "models" expect.Function.PackageName = "models"
expect.Function.Name = "Jet" expect.Function.Name = "Jet"
expect.Function.ForeignName = "Pilot"
expect.Function.Varname = "jet" expect.Function.Varname = "jet"
expect.Function.Receiver = "p" expect.Function.Receiver = "p"
expect.Function.ReverseInserts = true expect.Function.ReverseInserts = true
@ -117,6 +119,7 @@ func TestTextsFromRelationship(t *testing.T) {
expect.ForeignTable.Slice = "JetSlice" expect.ForeignTable.Slice = "JetSlice"
expect.Function.Name = "Jets" expect.Function.Name = "Jets"
expect.Function.ForeignName = "Pilots"
expect.Function.Receiver = "p" expect.Function.Receiver = "p"
expect.Function.LocalAssignment = "ID" expect.Function.LocalAssignment = "ID"
expect.Function.ForeignAssignment = "PilotID.Int" expect.Function.ForeignAssignment = "PilotID.Int"
@ -139,6 +142,7 @@ func TestTextsFromRelationship(t *testing.T) {
expect.ForeignTable.Slice = "LicenseSlice" expect.ForeignTable.Slice = "LicenseSlice"
expect.Function.Name = "Licenses" expect.Function.Name = "Licenses"
expect.Function.ForeignName = "Pilots"
expect.Function.Receiver = "p" expect.Function.Receiver = "p"
expect.Function.LocalAssignment = "ID" expect.Function.LocalAssignment = "ID"
expect.Function.ForeignAssignment = "PilotID" expect.Function.ForeignAssignment = "PilotID"
@ -161,6 +165,7 @@ func TestTextsFromRelationship(t *testing.T) {
expect.ForeignTable.Slice = "LanguageSlice" expect.ForeignTable.Slice = "LanguageSlice"
expect.Function.Name = "Languages" expect.Function.Name = "Languages"
expect.Function.ForeignName = "Pilots"
expect.Function.Receiver = "p" expect.Function.Receiver = "p"
expect.Function.LocalAssignment = "ID" expect.Function.LocalAssignment = "ID"
expect.Function.ForeignAssignment = "ID" expect.Function.ForeignAssignment = "ID"