New texts to reverse a "to_many" into "to_one"
This commit is contained in:
parent
423d4b66bf
commit
1513661e15
3 changed files with 73 additions and 5 deletions
|
@ -158,8 +158,9 @@ var templateFunctions = template.FuncMap{
|
||||||
"whereClause": strmangle.WhereClause,
|
"whereClause": strmangle.WhereClause,
|
||||||
|
|
||||||
// Text helpers
|
// Text helpers
|
||||||
"textsFromForeignKey": textsFromForeignKey,
|
"textsFromForeignKey": textsFromForeignKey,
|
||||||
"textsFromRelationship": textsFromRelationship,
|
"textsFromOneToOneRelationship": textsFromOneToOneRelationship,
|
||||||
|
"textsFromRelationship": textsFromRelationship,
|
||||||
|
|
||||||
// dbdrivers ops
|
// dbdrivers ops
|
||||||
"driverUsesLastInsertID": strmangle.DriverUsesLastInsertID,
|
"driverUsesLastInsertID": strmangle.DriverUsesLastInsertID,
|
||||||
|
|
|
@ -28,8 +28,9 @@ type RelationshipToOneTexts struct {
|
||||||
PackageName string
|
PackageName string
|
||||||
Name string
|
Name string
|
||||||
|
|
||||||
Varname string
|
Varname string
|
||||||
Receiver string
|
Receiver string
|
||||||
|
ReverseArgs bool
|
||||||
|
|
||||||
LocalAssignment string
|
LocalAssignment string
|
||||||
ForeignAssignment string
|
ForeignAssignment string
|
||||||
|
@ -72,6 +73,25 @@ func textsFromForeignKey(packageName string, tables []bdb.Table, table bdb.Table
|
||||||
return r
|
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.
|
// RelationshipToManyTexts contains text that will be used by templates.
|
||||||
type RelationshipToManyTexts struct {
|
type RelationshipToManyTexts struct {
|
||||||
LocalTable struct {
|
LocalTable struct {
|
||||||
|
|
|
@ -20,7 +20,7 @@ func (fakeDB) Columns(tableName string) ([]bdb.Column, error) {
|
||||||
"contests": []bdb.Column{{Name: "id", Type: "int32", Nullable: true}},
|
"contests": []bdb.Column{{Name: "id", Type: "int32", Nullable: true}},
|
||||||
"videos": []bdb.Column{
|
"videos": []bdb.Column{
|
||||||
{Name: "id", Type: "int32"},
|
{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"},
|
{Name: "contest_id", Type: "int32"},
|
||||||
},
|
},
|
||||||
"notifications": []bdb.Column{
|
"notifications": []bdb.Column{
|
||||||
|
@ -92,6 +92,7 @@ func TestTextsFromForeignKey(t *testing.T) {
|
||||||
expect.Function.Name = "User"
|
expect.Function.Name = "User"
|
||||||
expect.Function.Varname = "user"
|
expect.Function.Varname = "user"
|
||||||
expect.Function.Receiver = "v"
|
expect.Function.Receiver = "v"
|
||||||
|
expect.Function.ReverseArgs = false
|
||||||
|
|
||||||
expect.Function.LocalAssignment = "UserID.Int32"
|
expect.Function.LocalAssignment = "UserID.Int32"
|
||||||
expect.Function.ForeignAssignment = "ID"
|
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) {
|
func TestTextsFromRelationship(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue