Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue #2616 #3307

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 104 additions & 1 deletion cmd/gf/internal/cmd/cmd_z_unit_gen_dao_test.go
Original file line number Diff line number Diff line change
@@ -287,6 +287,109 @@ func Test_Gen_Dao_Issue2572(t *testing.T) {
_, err = gendao.CGenDao{}.Dao(ctx, in)
t.AssertNil(err)

fmt.Println(path)
generatedFiles, err := gfile.ScanDir(path, "*.go", true)
t.AssertNil(err)
t.Assert(len(generatedFiles), 8)
for i, generatedFile := range generatedFiles {
generatedFiles[i] = gstr.TrimLeftStr(generatedFile, path)
}
t.Assert(gstr.InArray(generatedFiles, "/dao/internal/user_1.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/dao/internal/user_2.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/dao/user_1.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/dao/user_2.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/model/do/user_1.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/model/do/user_2.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/model/entity/user_1.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/model/entity/user_2.go"), true)
})
}

func Test_Gen_Dao_Issue2616(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
err error
db = testDB
table1 = "user1"
table2 = "user2"
issueDirPath = gtest.DataPath(`issue`, `2616`)
)
t.AssertNil(execSqlFile(db, gtest.DataPath(`issue`, `2616`, `sql1.sql`)))
t.AssertNil(execSqlFile(db, gtest.DataPath(`issue`, `2616`, `sql2.sql`)))
defer dropTableWithDb(db, table1)
defer dropTableWithDb(db, table2)

var (
path = gfile.Temp(guid.S())
group = "test"
in = gendao.CGenDaoInput{
Path: path,
Link: link,
Tables: "",
TablesEx: "",
Group: group,
Prefix: "",
RemovePrefix: "",
JsonCase: "SnakeScreaming",
ImportPrefix: "",
DaoPath: "",
DoPath: "",
EntityPath: "",
TplDaoIndexPath: "",
TplDaoInternalPath: "",
TplDaoDoPath: "",
TplDaoEntityPath: "",
StdTime: false,
WithTime: false,
GJsonSupport: false,
OverwriteDao: false,
DescriptionTag: false,
NoJsonTag: false,
NoModelComment: false,
Clear: false,
TypeMapping: nil,
}
)
err = gutil.FillStructWithDefault(&in)
t.AssertNil(err)

err = gfile.Copy(issueDirPath, path)
t.AssertNil(err)

defer gfile.Remove(path)

pwd := gfile.Pwd()
err = gfile.Chdir(path)
t.AssertNil(err)

defer gfile.Chdir(pwd)

_, err = gendao.CGenDao{}.Dao(ctx, in)
t.AssertNil(err)

generatedFiles, err := gfile.ScanDir(path, "*.go", true)
t.AssertNil(err)
t.Assert(len(generatedFiles), 8)
for i, generatedFile := range generatedFiles {
generatedFiles[i] = gstr.TrimLeftStr(generatedFile, path)
}
t.Assert(gstr.InArray(generatedFiles, "/dao/internal/user_1.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/dao/internal/user_2.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/dao/user_1.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/dao/user_2.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/model/do/user_1.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/model/do/user_2.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/model/entity/user_1.go"), true)
t.Assert(gstr.InArray(generatedFiles, "/model/entity/user_2.go"), true)

// Key string to check if overwrite the dao files.
// dao user1 is not be overwritten as configured in config.yaml.
// dao user2 is to be overwritten as configured in config.yaml.
var (
keyStr = `// I am not overwritten.`
daoUser1Content = gfile.GetContents(path + "/dao/user_1.go")
daoUser2Content = gfile.GetContents(path + "/dao/user_2.go")
)
t.Assert(gstr.Contains(daoUser1Content, keyStr), true)
t.Assert(gstr.Contains(daoUser2Content, keyStr), false)
})
}
9 changes: 5 additions & 4 deletions cmd/gf/internal/cmd/gendao/gendao_dao.go
Original file line number Diff line number Diff line change
@@ -105,6 +105,11 @@ type generateDaoIndexInput struct {

func generateDaoIndex(in generateDaoIndexInput) {
path := filepath.FromSlash(gfile.Join(in.DirPathDao, in.FileName+".go"))
// It should add path to result slice whenever it would generate the path file or not.
in.generatedFilePaths.DaoFilePaths = append(
in.generatedFilePaths.DaoFilePaths,
path,
)
if in.OverwriteDao || !gfile.Exists(path) {
indexContent := gstr.ReplaceByMap(
getTemplateFromPathOrDefault(in.TplDaoIndexPath, consts.TemplateGenDaoIndexContent),
@@ -115,10 +120,6 @@ func generateDaoIndex(in generateDaoIndexInput) {
tplVarTableNameCamelLowerCase: in.TableNameCamelLowerCase,
})
indexContent = replaceDefaultVar(in.CGenDaoInternalInput, indexContent)
in.generatedFilePaths.DaoFilePaths = append(
in.generatedFilePaths.DaoFilePaths,
path,
)
if err := gfile.PutContents(path, strings.TrimSpace(indexContent)); err != nil {
mlog.Fatalf("writing content to '%s' failed: %v", path, err)
} else {
20 changes: 20 additions & 0 deletions cmd/gf/internal/cmd/testdata/issue/2616/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
gfcli:
gen:
dao:
- link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
tables: "user1"
descriptionTag: true
noModelComment: true
group: "sys"
clear: true
overwriteDao: false
- link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
tables: "user2"
descriptionTag: true
noModelComment: true
group: "book"
clear: true
overwriteDao: true



85 changes: 85 additions & 0 deletions cmd/gf/internal/cmd/testdata/issue/2616/dao/internal/user_1.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 85 additions & 0 deletions cmd/gf/internal/cmd/testdata/issue/2616/dao/internal/user_2.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 85 additions & 0 deletions cmd/gf/internal/cmd/testdata/issue/2616/dao/internal/user_3.go
85 changes: 85 additions & 0 deletions cmd/gf/internal/cmd/testdata/issue/2616/dao/internal/user_4.go
29 changes: 29 additions & 0 deletions cmd/gf/internal/cmd/testdata/issue/2616/dao/user_1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================

// I am not overwritten.

package dao

import (
"/internal"
)

// internalUser1Dao is internal type for wrapping internal DAO implements.
type internalUser1Dao = *internal.User1Dao

// user1Dao is the data access object for table user1.
// You can define custom methods on it to extend its functionality as you wish.
type user1Dao struct {
internalUser1Dao
}

var (
// User1 is globally public accessible object for table user1 operations.
User1 = user1Dao{
internal.NewUser1Dao(),
}
)

// Fill with you ideas below.
29 changes: 29 additions & 0 deletions cmd/gf/internal/cmd/testdata/issue/2616/dao/user_2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================

// I am not overwritten.

package dao

import (
"/internal"
)

// internalUser2Dao is internal type for wrapping internal DAO implements.
type internalUser2Dao = *internal.User2Dao

// user2Dao is the data access object for table user2.
// You can define custom methods on it to extend its functionality as you wish.
type user2Dao struct {
internalUser2Dao
}

var (
// User2 is globally public accessible object for table user2 operations.
User2 = user2Dao{
internal.NewUser2Dao(),
}
)

// Fill with you ideas below.
27 changes: 27 additions & 0 deletions cmd/gf/internal/cmd/testdata/issue/2616/dao/user_3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================

package dao

import (
"/internal"
)

// internalUser3Dao is internal type for wrapping internal DAO implements.
type internalUser3Dao = *internal.User3Dao

// user3Dao is the data access object for table user3.
// You can define custom methods on it to extend its functionality as you wish.
type user3Dao struct {
internalUser3Dao
}

var (
// User3 is globally public accessible object for table user3 operations.
User3 = user3Dao{
internal.NewUser3Dao(),
}
)

// Fill with you ideas below.
27 changes: 27 additions & 0 deletions cmd/gf/internal/cmd/testdata/issue/2616/dao/user_4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================

package dao

import (
"/internal"
)

// internalUser4Dao is internal type for wrapping internal DAO implements.
type internalUser4Dao = *internal.User4Dao

// user4Dao is the data access object for table user4.
// You can define custom methods on it to extend its functionality as you wish.
type user4Dao struct {
internalUser4Dao
}

var (
// User4 is globally public accessible object for table user4 operations.
User4 = user4Dao{
internal.NewUser4Dao(),
}
)

// Fill with you ideas below.
22 changes: 22 additions & 0 deletions cmd/gf/internal/cmd/testdata/issue/2616/model/do/user_3.go
22 changes: 22 additions & 0 deletions cmd/gf/internal/cmd/testdata/issue/2616/model/do/user_4.go
20 changes: 20 additions & 0 deletions cmd/gf/internal/cmd/testdata/issue/2616/model/entity/user_3.go
20 changes: 20 additions & 0 deletions cmd/gf/internal/cmd/testdata/issue/2616/model/entity/user_4.go
10 changes: 10 additions & 0 deletions cmd/gf/internal/cmd/testdata/issue/2616/sql1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE `user1` (
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'User ID',
`passport` varchar(45) NOT NULL COMMENT 'User Passport',
`password` varchar(45) NOT NULL COMMENT 'User Password',
`nickname` varchar(45) NOT NULL COMMENT 'User Nickname',
`score` decimal(10,2) unsigned DEFAULT NULL COMMENT 'Total score amount.',
`create_at` datetime DEFAULT NULL COMMENT 'Created Time',
`update_at` datetime DEFAULT NULL COMMENT 'Updated Time',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
10 changes: 10 additions & 0 deletions cmd/gf/internal/cmd/testdata/issue/2616/sql2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE `user2` (
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'User ID',
`passport` varchar(45) NOT NULL COMMENT 'User Passport',
`password` varchar(45) NOT NULL COMMENT 'User Password',
`nickname` varchar(45) NOT NULL COMMENT 'User Nickname',
`score` decimal(10,2) unsigned DEFAULT NULL COMMENT 'Total score amount.',
`create_at` datetime DEFAULT NULL COMMENT 'Created Time',
`update_at` datetime DEFAULT NULL COMMENT 'Updated Time',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;