Skip to content

Commit 25a9589

Browse files
committed
feat(cmd/gf): add NullFieldPattern option for command gf gen dao to convert null field to pointer (#4119)
1 parent 22427a0 commit 25a9589

File tree

23 files changed

+347
-1
lines changed

23 files changed

+347
-1
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
2+
//
3+
// This Source Code Form is subject to the terms of the MIT License.
4+
// If a copy of the MIT was not distributed with this file,
5+
// You can obtain one at https://github.com/gogf/gf.
6+
7+
package cmd
8+
9+
import (
10+
"fmt"
11+
"path/filepath"
12+
"testing"
13+
14+
"github.com/gogf/gf/v2/os/gfile"
15+
"github.com/gogf/gf/v2/test/gtest"
16+
"github.com/gogf/gf/v2/text/gstr"
17+
"github.com/gogf/gf/v2/util/guid"
18+
"github.com/gogf/gf/v2/util/gutil"
19+
20+
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/gendao"
21+
)
22+
23+
func Test_Gen_Dao_Null_Field(t *testing.T) {
24+
gtest.C(t, func(t *gtest.T) {
25+
var (
26+
err error
27+
db = testDB
28+
table = "table_user"
29+
sqlContent = fmt.Sprintf(
30+
gtest.DataContent(`gendao`, `user.tpl.sql`),
31+
table,
32+
)
33+
)
34+
defer dropTableWithDb(db, table)
35+
array := gstr.SplitAndTrim(sqlContent, ";")
36+
for _, v := range array {
37+
if _, err = db.Exec(ctx, v); err != nil {
38+
t.AssertNil(err)
39+
}
40+
}
41+
defer dropTableWithDb(db, table)
42+
43+
var (
44+
path = gfile.Temp(guid.S())
45+
group = "test"
46+
in = gendao.CGenDaoInput{
47+
Path: path,
48+
Link: link,
49+
Group: group,
50+
NullFieldPattern: []string{
51+
"table_?",
52+
},
53+
}
54+
)
55+
56+
err = gutil.FillStructWithDefault(&in)
57+
t.AssertNil(err)
58+
59+
err = gfile.Mkdir(path)
60+
t.AssertNil(err)
61+
62+
// for go mod import path auto retrieve.
63+
err = gfile.Copy(
64+
gtest.DataPath("gendao", "go.mod.txt"),
65+
gfile.Join(path, "go.mod"),
66+
)
67+
t.AssertNil(err)
68+
69+
_, err = gendao.CGenDao{}.Dao(ctx, in)
70+
t.AssertNil(err)
71+
defer gfile.Remove(path)
72+
73+
// files
74+
files, err := gfile.ScanDir(path, "*.go", true)
75+
t.AssertNil(err)
76+
t.Assert(files, []string{
77+
filepath.FromSlash(path + "/dao/internal/table_user.go"),
78+
filepath.FromSlash(path + "/dao/table_user.go"),
79+
filepath.FromSlash(path + "/model/do/table_user.go"),
80+
filepath.FromSlash(path + "/model/entity/table_user.go"),
81+
})
82+
83+
// content
84+
testPath := gtest.DataPath("gendao", "generated_null_filed_pointer")
85+
expectFiles := []string{
86+
filepath.FromSlash(testPath + "/dao/internal/table_user.go"),
87+
filepath.FromSlash(testPath + "/dao/table_user.go"),
88+
filepath.FromSlash(testPath + "/model/do/table_user.go"),
89+
filepath.FromSlash(testPath + "/model/entity/table_user.go"),
90+
}
91+
for i, _ := range expectFiles {
92+
t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i]))
93+
}
94+
})
95+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type (
4141
TablesEx string `name:"tablesEx" short:"x" brief:"{CGenDaoBriefTablesEx}"`
4242
ShardingPattern []string `name:"shardingPattern" short:"sp" brief:"{CGenDaoBriefShardingPattern}"`
4343
Group string `name:"group" short:"g" brief:"{CGenDaoBriefGroup}" d:"default"`
44+
NullFieldPattern []string `name:"nullFieldPattern" short:"nf" brief:"{CGenDaoNullFieldPattern}"`
4445
Prefix string `name:"prefix" short:"f" brief:"{CGenDaoBriefPrefix}"`
4546
RemovePrefix string `name:"removePrefix" short:"r" brief:"{CGenDaoBriefRemovePrefix}"`
4647
RemoveFieldPrefix string `name:"removeFieldPrefix" short:"rf" brief:"{CGenDaoBriefRemoveFieldPrefix}"`

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ func generateStructFieldDefinition(
133133
localTypeNameStr = "string"
134134
}
135135
}
136+
137+
// convert null filed in the database to pointer in package entity.
138+
if !in.IsDo && field.Null && !strings.HasPrefix(localTypeNameStr, "*") && len(in.NullFieldPattern) > 0 {
139+
for _, pattern := range in.NullFieldPattern {
140+
regPattern := gstr.Replace(pattern, "?", `(.+)`)
141+
match := gregex.IsMatchString(regPattern, in.TableName)
142+
if match {
143+
localTypeNameStr = "*" + localTypeNameStr
144+
break
145+
}
146+
}
147+
}
136148
}
137149

138150
var (

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ CONFIGURATION SUPPORT
7575
CGenDaoBriefGroup = `
7676
specifying the configuration group name of database for generated ORM instance,
7777
it's not necessary and the default value is "default"
78+
`
79+
CGenDaoBriefNullFieldPattern = `
80+
pattern for table fields that are NULL by default, the generated entity struct attribute type
81+
will be pointer type *T instead of T, e.g. "table_?"
7882
`
7983
CGenDaoBriefJsonCase = `
8084
generated json tag case for model struct, cases are as follows:
@@ -141,6 +145,7 @@ func init() {
141145
`CGenDaoBriefFieldMapping`: CGenDaoBriefFieldMapping,
142146
`CGenDaoBriefShardingPattern`: CGenDaoBriefShardingPattern,
143147
`CGenDaoBriefGroup`: CGenDaoBriefGroup,
148+
`CGenDaoBriefNullFieldPattern`: CGenDaoBriefNullFieldPattern,
144149
`CGenDaoBriefJsonCase`: CGenDaoBriefJsonCase,
145150
`CGenDaoBriefTplDaoIndexPath`: CGenDaoBriefTplDaoIndexPath,
146151
`CGenDaoBriefTplDaoInternalPath`: CGenDaoBriefTplDaoInternalPath,

cmd/gf/internal/cmd/testdata/gendao/generated_null_filed_pointer/dao/internal/table_user.go

Lines changed: 97 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// =================================================================================
2+
// This file is auto-generated by the GoFrame CLI tool. You may modify it as needed.
3+
// =================================================================================
4+
5+
package dao
6+
7+
import (
8+
"for-gendao-test/pkg/dao/internal"
9+
)
10+
11+
// tableUserDao is the data access object for the table table_user.
12+
// You can define custom methods on it to extend its functionality as needed.
13+
type tableUserDao struct {
14+
*internal.TableUserDao
15+
}
16+
17+
var (
18+
// TableUser is a globally accessible object for table table_user operations.
19+
TableUser = tableUserDao{internal.NewTableUserDao()}
20+
)
21+
22+
// Add your custom methods and functionality below.

cmd/gf/internal/cmd/testdata/gendao/generated_null_filed_pointer/model/do/table_user.go

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/gf/internal/cmd/testdata/gendao/generated_null_filed_pointer/model/entity/table_user.go

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/gf/internal/cmd/testdata/gendao/generated_user/dao/internal/table_user.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/gf/internal/cmd/testdata/gendao/generated_user/model/do/table_user.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)