From feab1b0f738686b48065500365d7d766e6f4c808 Mon Sep 17 00:00:00 2001 From: Yusuke Sakurai Date: Tue, 24 Jan 2023 18:38:36 +0900 Subject: [PATCH] fix: reverted unnecessary breaking change in generator (#43) --- generator.go | 4 ++-- model/fields.go | 4 ++-- model/group_users.go | 4 ++-- model/user_groups.go | 4 ++-- model/user_login_histories.go | 4 ++-- model/users.go | 4 ++-- query_test.go | 10 +--------- tool/modelgen/main.go | 7 ++++++- 8 files changed, 19 insertions(+), 22 deletions(-) diff --git a/generator.go b/generator.go index e6fcf53..b46b397 100644 --- a/generator.go +++ b/generator.go @@ -155,7 +155,7 @@ type {{.Model}} struct { {{.Fields}} } -func ({{.M}} {{.Model}}) TableName() string { +func ({{.M}} *{{.Model}}) TableName() string { return {{.Model}}TableName } @@ -163,7 +163,7 @@ type Update{{.Model}} struct { {{.UpdaterFields}} } -func ({{.M}} Update{{.Model}}) UpdateTableName() string { +func ({{.M}} *Update{{.Model}}) UpdateTableName() string { return {{.Model}}TableName } diff --git a/model/fields.go b/model/fields.go index 1881c06..a6df172 100755 --- a/model/fields.go +++ b/model/fields.go @@ -63,7 +63,7 @@ type Fields struct { JsonNullField null.JSON `exql:"column:json_null_field;type:json" json:"json_null_field"` } -func (f Fields) TableName() string { +func (f *Fields) TableName() string { return FieldsTableName } @@ -125,7 +125,7 @@ type UpdateFields struct { JsonNullField *null.JSON `exql:"column:json_null_field;type:json" json:"json_null_field"` } -func (f UpdateFields) UpdateTableName() string { +func (f *UpdateFields) UpdateTableName() string { return FieldsTableName } diff --git a/model/group_users.go b/model/group_users.go index c4d0905..24c6d9d 100644 --- a/model/group_users.go +++ b/model/group_users.go @@ -7,7 +7,7 @@ type GroupUsers struct { GroupId int64 `exql:"column:group_id;type:int(11);not null" json:"group_id"` } -func (g GroupUsers) TableName() string { +func (g *GroupUsers) TableName() string { return GroupUsersTableName } @@ -17,7 +17,7 @@ type UpdateGroupUsers struct { GroupId *int64 `exql:"column:group_id;type:int(11);not null" json:"group_id"` } -func (g UpdateGroupUsers) UpdateTableName() string { +func (g *UpdateGroupUsers) UpdateTableName() string { return GroupUsersTableName } diff --git a/model/user_groups.go b/model/user_groups.go index f65be89..2f345bd 100644 --- a/model/user_groups.go +++ b/model/user_groups.go @@ -6,7 +6,7 @@ type UserGroups struct { Name string `exql:"column:name;type:varchar(255);not null" json:"name"` } -func (u UserGroups) TableName() string { +func (u *UserGroups) TableName() string { return UserGroupsTableName } @@ -15,7 +15,7 @@ type UpdateUserGroups struct { Name *string `exql:"column:name;type:varchar(255);not null" json:"name"` } -func (u UpdateUserGroups) UpdateTableName() string { +func (u *UpdateUserGroups) UpdateTableName() string { return UserGroupsTableName } diff --git a/model/user_login_histories.go b/model/user_login_histories.go index dd8b24a..871d5b8 100644 --- a/model/user_login_histories.go +++ b/model/user_login_histories.go @@ -9,7 +9,7 @@ type UserLoginHistories struct { CreatedAt time.Time `exql:"column:created_at;type:datetime;primary;not null" json:"created_at"` } -func (u UserLoginHistories) TableName() string { +func (u *UserLoginHistories) TableName() string { return UserLoginHistoriesTableName } @@ -19,7 +19,7 @@ type UpdateUserLoginHistories struct { CreatedAt *time.Time `exql:"column:created_at;type:datetime;primary;not null" json:"created_at"` } -func (u UpdateUserLoginHistories) UpdateTableName() string { +func (u *UpdateUserLoginHistories) UpdateTableName() string { return UserLoginHistoriesTableName } diff --git a/model/users.go b/model/users.go index 1fa1c87..cbceace 100755 --- a/model/users.go +++ b/model/users.go @@ -9,7 +9,7 @@ type Users struct { LastName null.String `exql:"column:last_name;type:varchar(255)" json:"last_name"` } -func (u Users) TableName() string { +func (u *Users) TableName() string { return UsersTableName } @@ -19,7 +19,7 @@ type UpdateUsers struct { LastName *null.String `exql:"column:last_name;type:varchar(255)" json:"last_name"` } -func (u UpdateUsers) UpdateTableName() string { +func (u *UpdateUsers) UpdateTableName() string { return UsersTableName } diff --git a/query_test.go b/query_test.go index 3a33662..166c200 100644 --- a/query_test.go +++ b/query_test.go @@ -51,7 +51,7 @@ func TestQueryForBulkInsert(t *testing.T) { }, args) }) t.Run("error if args empty", func(t *testing.T) { - q, err := exql.QueryForBulkInsert[model.Users]() + q, err := exql.QueryForBulkInsert[*model.Users]() assert.Nil(t, q) assert.EqualError(t, err, "empty list") }) @@ -95,10 +95,6 @@ func TestAggregateModelMetadata(t *testing.T) { t.Run("should error if dest is nil", func(t *testing.T) { assertInvalid(t, nil, "pointer is nil") }) - t.Run("should error if dest is not pointer", func(t *testing.T) { - user := model.Users{} - assertInvalid(t, user, "object must be pointer of struct") - }) t.Run("should error if TableName() doesn't return string", func(t *testing.T) { assertInvalid(t, &testmodel.BadTableName{}, "empty table name") }) @@ -139,10 +135,6 @@ func TestQueryForUpdateModel(t *testing.T) { _, err := exql.QueryForUpdateModel(nil, nil) assert.EqualError(t, err, "pointer is nil") }) - t.Run("should error if not pointer", func(t *testing.T) { - _, err := exql.QueryForUpdateModel(model.UpdateUsers{}, nil) - assert.EqualError(t, err, "must be pointer of struct") - }) t.Run("should error if has invalid tag", func(t *testing.T) { _, err := exql.QueryForUpdateModel(&upSampleInvalidTag{}, nil) assert.EqualError(t, err, "invalid tag format") diff --git a/tool/modelgen/main.go b/tool/modelgen/main.go index f5b0d66..da09b0a 100644 --- a/tool/modelgen/main.go +++ b/tool/modelgen/main.go @@ -1,6 +1,8 @@ package main import ( + "log" + _ "github.com/go-sql-driver/mysql" "github.com/loilo-inc/exql/v2" ) @@ -10,7 +12,10 @@ func main() { Url: "root:@tcp(127.0.0.1:13326)/exql?charset=utf8mb4&parseTime=True&loc=Local", }) g := exql.NewGenerator(db.DB()) - g.Generate(&exql.GenerateOptions{ + err := g.Generate(&exql.GenerateOptions{ OutDir: "model", }) + if err != nil { + log.Fatal(err) + } }