From d18d7294e2a5d5e267360ccae599d09842ffc3d2 Mon Sep 17 00:00:00 2001 From: oldme Date: Sun, 21 Apr 2024 21:09:21 +0800 Subject: [PATCH 1/2] up --- .../internal/cmd/cmd_z_unit_gen_dao_test.go | 100 +++++++++++++++++- cmd/gf/internal/cmd/gendao/gendao.go | 4 +- .../cmd/testdata/issue/3459/config.yaml | 5 + 3 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 cmd/gf/internal/cmd/testdata/issue/3459/config.yaml diff --git a/cmd/gf/internal/cmd/cmd_z_unit_gen_dao_test.go b/cmd/gf/internal/cmd/cmd_z_unit_gen_dao_test.go index 5b29ed85640..2cf2684ac88 100644 --- a/cmd/gf/internal/cmd/cmd_z_unit_gen_dao_test.go +++ b/cmd/gf/internal/cmd/cmd_z_unit_gen_dao_test.go @@ -13,6 +13,8 @@ import ( "github.com/gogf/gf/cmd/gf/v2/internal/cmd/gendao" "github.com/gogf/gf/v2/database/gdb" + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/os/gcfg" "github.com/gogf/gf/v2/os/gfile" "github.com/gogf/gf/v2/test/gtest" "github.com/gogf/gf/v2/text/gstr" @@ -244,7 +246,7 @@ func Test_Gen_Dao_Issue2572(t *testing.T) { group = "test" in = gendao.CGenDaoInput{ Path: path, - Link: link, + Link: "", Tables: "", TablesEx: "", Group: group, @@ -468,3 +470,99 @@ func Test_Gen_Dao_Issue2746(t *testing.T) { t.Assert(expectContent, gfile.GetContents(file)) }) } + +// https://github.com/gogf/gf/issues/3459 +func Test_Gen_Dao_Issue3459(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + var ( + err error + db = testDB + table = "table_user" + sqlContent = fmt.Sprintf( + gtest.DataContent(`gendao`, `user.tpl.sql`), + table, + ) + ) + dropTableWithDb(db, table) + array := gstr.SplitAndTrim(sqlContent, ";") + for _, v := range array { + if _, err = db.Exec(ctx, v); err != nil { + t.AssertNil(err) + } + } + defer dropTableWithDb(db, table) + + var ( + confDir = gtest.DataPath("issue", "3459") + 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 = g.Cfg().GetAdapter().(*gcfg.AdapterFile).SetPath(confDir) + t.AssertNil(err) + + err = gutil.FillStructWithDefault(&in) + t.AssertNil(err) + + err = gfile.Mkdir(path) + t.AssertNil(err) + + // for go mod import path auto retrieve. + err = gfile.Copy( + gtest.DataPath("gendao", "go.mod.txt"), + gfile.Join(path, "go.mod"), + ) + t.AssertNil(err) + + _, err = gendao.CGenDao{}.Dao(ctx, in) + t.AssertNil(err) + defer gfile.Remove(path) + + // files + files, err := gfile.ScanDir(path, "*.go", true) + t.AssertNil(err) + t.Assert(files, []string{ + filepath.FromSlash(path + "/dao/internal/table_user.go"), + filepath.FromSlash(path + "/dao/table_user.go"), + filepath.FromSlash(path + "/model/do/table_user.go"), + filepath.FromSlash(path + "/model/entity/table_user.go"), + }) + // content + testPath := gtest.DataPath("gendao", "generated_user") + expectFiles := []string{ + filepath.FromSlash(testPath + "/dao/internal/table_user.go"), + filepath.FromSlash(testPath + "/dao/table_user.go"), + filepath.FromSlash(testPath + "/model/do/table_user.go"), + filepath.FromSlash(testPath + "/model/entity/table_user.go"), + } + for i, _ := range files { + t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i])) + } + }) +} diff --git a/cmd/gf/internal/cmd/gendao/gendao.go b/cmd/gf/internal/cmd/gendao/gendao.go index 0a64c416c92..4482c6f9559 100644 --- a/cmd/gf/internal/cmd/gendao/gendao.go +++ b/cmd/gf/internal/cmd/gendao/gendao.go @@ -222,7 +222,9 @@ type ( func (c CGenDao) Dao(ctx context.Context, in CGenDaoInput) (out *CGenDaoOutput, err error) { in.genItems = newCGenDaoInternalGenItems() - if g.Cfg().Available(ctx) { + if in.Link != "" { + doGenDaoForArray(ctx, -1, in) + } else if g.Cfg().Available(ctx) { v := g.Cfg().MustGet(ctx, CGenDaoConfig) if v.IsSlice() { for i := 0; i < len(v.Interfaces()); i++ { diff --git a/cmd/gf/internal/cmd/testdata/issue/3459/config.yaml b/cmd/gf/internal/cmd/testdata/issue/3459/config.yaml new file mode 100644 index 00000000000..ae62cf67fb4 --- /dev/null +++ b/cmd/gf/internal/cmd/testdata/issue/3459/config.yaml @@ -0,0 +1,5 @@ +gfcli: + gen: + dao: + - link: "pgsql:postgres:postgres@tcp(127.0.0.1:5432)/postgres" + tablesEx: "ex_table1,ex_table2" \ No newline at end of file From 7c5baf74dbff2e73d46b48dfe61ca7ff9072f7ae Mon Sep 17 00:00:00 2001 From: oldme Date: Tue, 23 Apr 2024 14:07:14 +0800 Subject: [PATCH 2/2] up --- cmd/gf/internal/cmd/cmd_z_unit_gen_dao_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/gf/internal/cmd/cmd_z_unit_gen_dao_test.go b/cmd/gf/internal/cmd/cmd_z_unit_gen_dao_test.go index 2cf2684ac88..eeb840f7355 100644 --- a/cmd/gf/internal/cmd/cmd_z_unit_gen_dao_test.go +++ b/cmd/gf/internal/cmd/cmd_z_unit_gen_dao_test.go @@ -325,7 +325,7 @@ func Test_Gen_Dao_Issue2616(t *testing.T) { group = "test" in = gendao.CGenDaoInput{ Path: path, - Link: link, + Link: "", Tables: "", TablesEx: "", Group: group,