Skip to content

Commit 59a959a

Browse files
authored
fix: #3459 gf gen dao should ignore link configuration from file as it is passed from arguments (#3531)
1 parent ce57b26 commit 59a959a

File tree

3 files changed

+108
-3
lines changed

3 files changed

+108
-3
lines changed

cmd/gf/internal/cmd/cmd_z_unit_gen_dao_test.go

+100-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313

1414
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/gendao"
1515
"github.com/gogf/gf/v2/database/gdb"
16+
"github.com/gogf/gf/v2/frame/g"
17+
"github.com/gogf/gf/v2/os/gcfg"
1618
"github.com/gogf/gf/v2/os/gfile"
1719
"github.com/gogf/gf/v2/test/gtest"
1820
"github.com/gogf/gf/v2/text/gstr"
@@ -245,7 +247,7 @@ func Test_Gen_Dao_Issue2572(t *testing.T) {
245247
group = "test"
246248
in = gendao.CGenDaoInput{
247249
Path: path,
248-
Link: link,
250+
Link: "",
249251
Tables: "",
250252
TablesEx: "",
251253
Group: group,
@@ -333,7 +335,7 @@ func Test_Gen_Dao_Issue2616(t *testing.T) {
333335
group = "test"
334336
in = gendao.CGenDaoInput{
335337
Path: path,
336-
Link: link,
338+
Link: "",
337339
Tables: "",
338340
TablesEx: "",
339341
Group: group,
@@ -486,3 +488,99 @@ func Test_Gen_Dao_Issue2746(t *testing.T) {
486488
t.Assert(expectContent, gfile.GetContents(file))
487489
})
488490
}
491+
492+
// https://github.com/gogf/gf/issues/3459
493+
func Test_Gen_Dao_Issue3459(t *testing.T) {
494+
gtest.C(t, func(t *gtest.T) {
495+
var (
496+
err error
497+
db = testDB
498+
table = "table_user"
499+
sqlContent = fmt.Sprintf(
500+
gtest.DataContent(`gendao`, `user.tpl.sql`),
501+
table,
502+
)
503+
)
504+
dropTableWithDb(db, table)
505+
array := gstr.SplitAndTrim(sqlContent, ";")
506+
for _, v := range array {
507+
if _, err = db.Exec(ctx, v); err != nil {
508+
t.AssertNil(err)
509+
}
510+
}
511+
defer dropTableWithDb(db, table)
512+
513+
var (
514+
confDir = gtest.DataPath("issue", "3459")
515+
path = gfile.Temp(guid.S())
516+
group = "test"
517+
in = gendao.CGenDaoInput{
518+
Path: path,
519+
Link: link,
520+
Tables: "",
521+
TablesEx: "",
522+
Group: group,
523+
Prefix: "",
524+
RemovePrefix: "",
525+
JsonCase: "SnakeScreaming",
526+
ImportPrefix: "",
527+
DaoPath: "",
528+
DoPath: "",
529+
EntityPath: "",
530+
TplDaoIndexPath: "",
531+
TplDaoInternalPath: "",
532+
TplDaoDoPath: "",
533+
TplDaoEntityPath: "",
534+
StdTime: false,
535+
WithTime: false,
536+
GJsonSupport: false,
537+
OverwriteDao: false,
538+
DescriptionTag: false,
539+
NoJsonTag: false,
540+
NoModelComment: false,
541+
Clear: false,
542+
TypeMapping: nil,
543+
}
544+
)
545+
err = g.Cfg().GetAdapter().(*gcfg.AdapterFile).SetPath(confDir)
546+
t.AssertNil(err)
547+
548+
err = gutil.FillStructWithDefault(&in)
549+
t.AssertNil(err)
550+
551+
err = gfile.Mkdir(path)
552+
t.AssertNil(err)
553+
554+
// for go mod import path auto retrieve.
555+
err = gfile.Copy(
556+
gtest.DataPath("gendao", "go.mod.txt"),
557+
gfile.Join(path, "go.mod"),
558+
)
559+
t.AssertNil(err)
560+
561+
_, err = gendao.CGenDao{}.Dao(ctx, in)
562+
t.AssertNil(err)
563+
defer gfile.Remove(path)
564+
565+
// files
566+
files, err := gfile.ScanDir(path, "*.go", true)
567+
t.AssertNil(err)
568+
t.Assert(files, []string{
569+
filepath.FromSlash(path + "/dao/internal/table_user.go"),
570+
filepath.FromSlash(path + "/dao/table_user.go"),
571+
filepath.FromSlash(path + "/model/do/table_user.go"),
572+
filepath.FromSlash(path + "/model/entity/table_user.go"),
573+
})
574+
// content
575+
testPath := gtest.DataPath("gendao", "generated_user")
576+
expectFiles := []string{
577+
filepath.FromSlash(testPath + "/dao/internal/table_user.go"),
578+
filepath.FromSlash(testPath + "/dao/table_user.go"),
579+
filepath.FromSlash(testPath + "/model/do/table_user.go"),
580+
filepath.FromSlash(testPath + "/model/entity/table_user.go"),
581+
}
582+
for i, _ := range files {
583+
t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i]))
584+
}
585+
})
586+
}

cmd/gf/internal/cmd/gendao/gendao.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ type (
222222

223223
func (c CGenDao) Dao(ctx context.Context, in CGenDaoInput) (out *CGenDaoOutput, err error) {
224224
in.genItems = newCGenDaoInternalGenItems()
225-
if g.Cfg().Available(ctx) {
225+
if in.Link != "" {
226+
doGenDaoForArray(ctx, -1, in)
227+
} else if g.Cfg().Available(ctx) {
226228
v := g.Cfg().MustGet(ctx, CGenDaoConfig)
227229
if v.IsSlice() {
228230
for i := 0; i < len(v.Interfaces()); i++ {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
gfcli:
2+
gen:
3+
dao:
4+
- link: "pgsql:postgres:postgres@tcp(127.0.0.1:5432)/postgres"
5+
tablesEx: "ex_table1,ex_table2"

0 commit comments

Comments
 (0)