From 1513661e15c639a725e098c46a5197f069fbaa0e Mon Sep 17 00:00:00 2001 From: Aaron L Date: Sun, 17 Jul 2016 17:48:08 -0700 Subject: [PATCH] New texts to reverse a "to_many" into "to_one" --- templates.go | 5 +++-- text_helpers.go | 24 ++++++++++++++++++++-- text_helpers_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 5 deletions(-) diff --git a/templates.go b/templates.go index 5c2ba5e..4ab2756 100644 --- a/templates.go +++ b/templates.go @@ -158,8 +158,9 @@ var templateFunctions = template.FuncMap{ "whereClause": strmangle.WhereClause, // Text helpers - "textsFromForeignKey": textsFromForeignKey, - "textsFromRelationship": textsFromRelationship, + "textsFromForeignKey": textsFromForeignKey, + "textsFromOneToOneRelationship": textsFromOneToOneRelationship, + "textsFromRelationship": textsFromRelationship, // dbdrivers ops "driverUsesLastInsertID": strmangle.DriverUsesLastInsertID, diff --git a/text_helpers.go b/text_helpers.go index b9cfec2..d84ef34 100644 --- a/text_helpers.go +++ b/text_helpers.go @@ -28,8 +28,9 @@ type RelationshipToOneTexts struct { PackageName string Name string - Varname string - Receiver string + Varname string + Receiver string + ReverseArgs bool LocalAssignment string ForeignAssignment string @@ -72,6 +73,25 @@ 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 { + fkey := bdb.ForeignKey{ + Name: "none", + Column: toMany.Column, + Nullable: toMany.Nullable, + Unique: toMany.Unique, + + ForeignTable: toMany.ForeignTable, + ForeignColumn: toMany.ForeignColumn, + ForeignColumnNullable: toMany.ForeignColumnNullable, + ForeignColumnUnique: toMany.ForeignColumnUnique, + } + + rel := textsFromForeignKey(packageName, tables, table, fkey) + rel.Function.Name = strmangle.TitleCase(strmangle.Singular(toMany.ForeignTable)) + rel.Function.ReverseArgs = true + return rel +} + // RelationshipToManyTexts contains text that will be used by templates. type RelationshipToManyTexts struct { LocalTable struct { diff --git a/text_helpers_test.go b/text_helpers_test.go index 011a88d..fa22b43 100644 --- a/text_helpers_test.go +++ b/text_helpers_test.go @@ -20,7 +20,7 @@ func (fakeDB) Columns(tableName string) ([]bdb.Column, error) { "contests": []bdb.Column{{Name: "id", Type: "int32", Nullable: true}}, "videos": []bdb.Column{ {Name: "id", Type: "int32"}, - {Name: "user_id", Type: "int32", Nullable: true}, + {Name: "user_id", Type: "int32", Nullable: true, Unique: true}, {Name: "contest_id", Type: "int32"}, }, "notifications": []bdb.Column{ @@ -92,6 +92,7 @@ func TestTextsFromForeignKey(t *testing.T) { expect.Function.Name = "User" expect.Function.Varname = "user" expect.Function.Receiver = "v" + expect.Function.ReverseArgs = false expect.Function.LocalAssignment = "UserID.Int32" expect.Function.ForeignAssignment = "ID" @@ -101,6 +102,52 @@ func TestTextsFromForeignKey(t *testing.T) { } } +func TestTextsFromOneToOneRelationship(t *testing.T) { + t.Parallel() + + tables, err := bdb.Tables(fakeDB(0)) + if err != nil { + t.Fatal(err) + } + + users := bdb.GetTable(tables, "users") + texts := textsFromOneToOneRelationship("models", tables, users, users.ToManyRelationships[0]) + expect := RelationshipToOneTexts{} + + expect.ForeignKey = bdb.ForeignKey{ + Name: "none", + Column: "id", + Nullable: false, + Unique: false, + + ForeignTable: "videos", + ForeignColumn: "user_id", + ForeignColumnNullable: true, + ForeignColumnUnique: true, + } + + expect.LocalTable.NameGo = "User" + expect.LocalTable.ColumnNameGo = "ID" + + expect.ForeignTable.Name = "videos" + expect.ForeignTable.NameGo = "Video" + expect.ForeignTable.ColumnName = "user_id" + expect.ForeignTable.ColumnNameGo = "UserID" + + expect.Function.PackageName = "models" + expect.Function.Name = "Video" + expect.Function.Varname = "video" + expect.Function.Receiver = "u" + expect.Function.ReverseArgs = true + + expect.Function.LocalAssignment = "ID" + expect.Function.ForeignAssignment = "UserID.Int32" + + if !reflect.DeepEqual(expect, texts) { + t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts)) + } +} + func TestTextsFromRelationship(t *testing.T) { t.Parallel()