From d778401a7bd111144c5fafb988ec97e5c58c207a Mon Sep 17 00:00:00 2001 From: Aaron L Date: Sat, 27 Aug 2016 17:20:26 -0700 Subject: [PATCH] Another iteration on getting text helpers correct --- text_helpers.go | 15 ++++++++++----- text_helpers_test.go | 7 ++++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/text_helpers.go b/text_helpers.go index e2cfdf6..a5e86da 100644 --- a/text_helpers.go +++ b/text_helpers.go @@ -28,6 +28,7 @@ type RelationshipToOneTexts struct { Function struct { PackageName string Name string + ForeignName string Varname string Receiver string @@ -54,6 +55,7 @@ func textsFromForeignKey(packageName string, tables []bdb.Table, table bdb.Table r.Function.PackageName = packageName 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.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.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 return rel } @@ -113,8 +116,9 @@ type RelationshipToManyTexts struct { } Function struct { - Name string - Receiver string + Name string + ForeignName string + Receiver string LocalAssignment string ForeignAssignment 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.Name = mkFunctionName(r.LocalTable.NameSingular, r.ForeignTable.NamePluralGo, rel.ForeignColumn, rel.ToJoinTable) + r.Function.ForeignName = strmangle.TitleCase(strmangle.Plural(table.Name)) if rel.Nullable { 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) // 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 -func mkFunctionName(localTableSingular, foreignTablePluralGo, foreignColumn string, toJoinTable bool) string { - colName := strings.TrimSuffix(foreignColumn, "_id") - if toJoinTable || localTableSingular == colName { +func mkFunctionName(fkeyTableSingular, foreignTablePluralGo, fkeyColumn string, toJoinTable bool) string { + colName := strings.TrimSuffix(fkeyColumn, "_id") + if toJoinTable || fkeyTableSingular == colName { return foreignTablePluralGo } diff --git a/text_helpers_test.go b/text_helpers_test.go index 929930d..4948880 100644 --- a/text_helpers_test.go +++ b/text_helpers_test.go @@ -34,6 +34,7 @@ func TestTextsFromForeignKey(t *testing.T) { expect.Function.PackageName = "models" expect.Function.Name = "Pilot" + expect.Function.ForeignName = "Jets" expect.Function.Varname = "pilot" expect.Function.Receiver = "j" expect.Function.ReverseInserts = false @@ -59,7 +60,7 @@ func TestTextsFromOneToOneRelationship(t *testing.T) { expect := RelationshipToOneTexts{} expect.ForeignKey = bdb.ForeignKey{ - Table: "jets", + Table: "pilots", Name: "none", Column: "id", Nullable: false, @@ -82,6 +83,7 @@ func TestTextsFromOneToOneRelationship(t *testing.T) { expect.Function.PackageName = "models" expect.Function.Name = "Jet" + expect.Function.ForeignName = "Pilot" expect.Function.Varname = "jet" expect.Function.Receiver = "p" expect.Function.ReverseInserts = true @@ -117,6 +119,7 @@ func TestTextsFromRelationship(t *testing.T) { expect.ForeignTable.Slice = "JetSlice" expect.Function.Name = "Jets" + expect.Function.ForeignName = "Pilots" expect.Function.Receiver = "p" expect.Function.LocalAssignment = "ID" expect.Function.ForeignAssignment = "PilotID.Int" @@ -139,6 +142,7 @@ func TestTextsFromRelationship(t *testing.T) { expect.ForeignTable.Slice = "LicenseSlice" expect.Function.Name = "Licenses" + expect.Function.ForeignName = "Pilots" expect.Function.Receiver = "p" expect.Function.LocalAssignment = "ID" expect.Function.ForeignAssignment = "PilotID" @@ -161,6 +165,7 @@ func TestTextsFromRelationship(t *testing.T) { expect.ForeignTable.Slice = "LanguageSlice" expect.Function.Name = "Languages" + expect.Function.ForeignName = "Pilots" expect.Function.Receiver = "p" expect.Function.LocalAssignment = "ID" expect.Function.ForeignAssignment = "ID"