Skip to content

Commit c7e9c83

Browse files
authored
Merge branch 'gogf:master' into feat-3539
2 parents 50dc29b + 5b7cae7 commit c7e9c83

File tree

14 files changed

+422
-72
lines changed

14 files changed

+422
-72
lines changed

cmd/gf/internal/cmd/cmd_z_unit_gen_dao_test.go

+107
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func Test_Gen_Dao_Default(t *testing.T) {
7171
NoModelComment: false,
7272
Clear: false,
7373
TypeMapping: nil,
74+
FieldMapping: nil,
7475
}
7576
)
7677
err = gutil.FillStructWithDefault(&in)
@@ -171,6 +172,7 @@ func Test_Gen_Dao_TypeMapping(t *testing.T) {
171172
Import: "github.com/shopspring/decimal",
172173
},
173174
},
175+
FieldMapping: nil,
174176
}
175177
)
176178
err = gutil.FillStructWithDefault(&in)
@@ -213,6 +215,108 @@ func Test_Gen_Dao_TypeMapping(t *testing.T) {
213215
})
214216
}
215217

218+
func Test_Gen_Dao_FieldMapping(t *testing.T) {
219+
gtest.C(t, func(t *gtest.T) {
220+
var (
221+
err error
222+
db = testDB
223+
table = "table_user"
224+
sqlContent = fmt.Sprintf(
225+
gtest.DataContent(`gendao`, `user.tpl.sql`),
226+
table,
227+
)
228+
)
229+
defer dropTableWithDb(db, table)
230+
array := gstr.SplitAndTrim(sqlContent, ";")
231+
for _, v := range array {
232+
if _, err = db.Exec(ctx, v); err != nil {
233+
t.AssertNil(err)
234+
}
235+
}
236+
defer dropTableWithDb(db, table)
237+
238+
var (
239+
path = gfile.Temp(guid.S())
240+
group = "test"
241+
in = gendao.CGenDaoInput{
242+
Path: path,
243+
Link: link,
244+
Tables: "table_user",
245+
TablesEx: "",
246+
Group: group,
247+
Prefix: "",
248+
RemovePrefix: "",
249+
JsonCase: "",
250+
ImportPrefix: "",
251+
DaoPath: "",
252+
DoPath: "",
253+
EntityPath: "",
254+
TplDaoIndexPath: "",
255+
TplDaoInternalPath: "",
256+
TplDaoDoPath: "",
257+
TplDaoEntityPath: "",
258+
StdTime: false,
259+
WithTime: false,
260+
GJsonSupport: false,
261+
OverwriteDao: false,
262+
DescriptionTag: false,
263+
NoJsonTag: false,
264+
NoModelComment: false,
265+
Clear: false,
266+
TypeMapping: map[gendao.DBFieldTypeName]gendao.CustomAttributeType{
267+
"int": {
268+
Type: "int64",
269+
Import: "",
270+
},
271+
},
272+
FieldMapping: map[gendao.DBTableFieldName]gendao.CustomAttributeType{
273+
"table_user.score": {
274+
Type: "decimal.Decimal",
275+
Import: "github.com/shopspring/decimal",
276+
},
277+
},
278+
}
279+
)
280+
err = gutil.FillStructWithDefault(&in)
281+
t.AssertNil(err)
282+
283+
err = gfile.Mkdir(path)
284+
t.AssertNil(err)
285+
286+
// for go mod import path auto retrieve.
287+
err = gfile.Copy(
288+
gtest.DataPath("gendao", "go.mod.txt"),
289+
gfile.Join(path, "go.mod"),
290+
)
291+
t.AssertNil(err)
292+
293+
_, err = gendao.CGenDao{}.Dao(ctx, in)
294+
t.AssertNil(err)
295+
defer gfile.Remove(path)
296+
297+
// files
298+
files, err := gfile.ScanDir(path, "*.go", true)
299+
t.AssertNil(err)
300+
t.Assert(files, []string{
301+
filepath.FromSlash(path + "/dao/internal/table_user.go"),
302+
filepath.FromSlash(path + "/dao/table_user.go"),
303+
filepath.FromSlash(path + "/model/do/table_user.go"),
304+
filepath.FromSlash(path + "/model/entity/table_user.go"),
305+
})
306+
// content
307+
testPath := gtest.DataPath("gendao", "generated_user_field_mapping")
308+
expectFiles := []string{
309+
filepath.FromSlash(testPath + "/dao/internal/table_user.go"),
310+
filepath.FromSlash(testPath + "/dao/table_user.go"),
311+
filepath.FromSlash(testPath + "/model/do/table_user.go"),
312+
filepath.FromSlash(testPath + "/model/entity/table_user.go"),
313+
}
314+
for i, _ := range files {
315+
t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i]))
316+
}
317+
})
318+
}
319+
216320
func execSqlFile(db gdb.DB, filePath string, args ...any) error {
217321
sqlContent := fmt.Sprintf(
218322
gfile.GetContents(filePath),
@@ -271,6 +375,7 @@ func Test_Gen_Dao_Issue2572(t *testing.T) {
271375
NoModelComment: false,
272376
Clear: false,
273377
TypeMapping: nil,
378+
FieldMapping: nil,
274379
}
275380
)
276381
err = gutil.FillStructWithDefault(&in)
@@ -359,6 +464,7 @@ func Test_Gen_Dao_Issue2616(t *testing.T) {
359464
NoModelComment: false,
360465
Clear: false,
361466
TypeMapping: nil,
467+
FieldMapping: nil,
362468
}
363469
)
364470
err = gutil.FillStructWithDefault(&in)
@@ -469,6 +575,7 @@ func Test_Gen_Dao_Issue2746(t *testing.T) {
469575
NoModelComment: false,
470576
Clear: false,
471577
TypeMapping: nil,
578+
FieldMapping: nil,
472579
}
473580
)
474581
err = gutil.FillStructWithDefault(&in)

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

+14-6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ CONFIGURATION SUPPORT
5858
import: github.com/shopspring/decimal
5959
numeric:
6060
type: string
61+
fieldMapping:
62+
table_name.field_name:
63+
type: decimal.Decimal
64+
import: github.com/shopspring/decimal
6165
`
6266
CGenDaoBriefPath = `directory path for generated files`
6367
CGenDaoBriefLink = `database configuration, the same as the ORM configuration of GoFrame`
@@ -81,6 +85,7 @@ CONFIGURATION SUPPORT
8185
CGenDaoBriefNoModelComment = `no model comment will be added for each field`
8286
CGenDaoBriefClear = `delete all generated go files that do not exist in database`
8387
CGenDaoBriefTypeMapping = `custom local type mapping for generated struct attributes relevant to fields of table`
88+
CGenDaoBriefFieldMapping = `custom local type mapping for generated struct attributes relevant to specific fields of table`
8489
CGenDaoBriefGroup = `
8590
specifying the configuration group name of database for generated ORM instance,
8691
it's not necessary and the default value is "default"
@@ -162,6 +167,7 @@ func init() {
162167
`CGenDaoBriefNoModelComment`: CGenDaoBriefNoModelComment,
163168
`CGenDaoBriefClear`: CGenDaoBriefClear,
164169
`CGenDaoBriefTypeMapping`: CGenDaoBriefTypeMapping,
170+
`CGenDaoBriefFieldMapping`: CGenDaoBriefFieldMapping,
165171
`CGenDaoBriefGroup`: CGenDaoBriefGroup,
166172
`CGenDaoBriefJsonCase`: CGenDaoBriefJsonCase,
167173
`CGenDaoBriefTplDaoIndexPath`: CGenDaoBriefTplDaoIndexPath,
@@ -201,8 +207,9 @@ type (
201207
NoModelComment bool `name:"noModelComment" short:"m" brief:"{CGenDaoBriefNoModelComment}" orphan:"true"`
202208
Clear bool `name:"clear" short:"a" brief:"{CGenDaoBriefClear}" orphan:"true"`
203209

204-
TypeMapping map[DBFieldTypeName]CustomAttributeType `name:"typeMapping" short:"y" brief:"{CGenDaoBriefTypeMapping}" orphan:"true"`
205-
genItems *CGenDaoInternalGenItems
210+
TypeMapping map[DBFieldTypeName]CustomAttributeType `name:"typeMapping" short:"y" brief:"{CGenDaoBriefTypeMapping}" orphan:"true"`
211+
FieldMapping map[DBTableFieldName]CustomAttributeType `name:"fieldMapping" short:"fm" brief:"{CGenDaoBriefFieldMapping}" orphan:"true"`
212+
genItems *CGenDaoInternalGenItems
206213
}
207214
CGenDaoOutput struct{}
208215

@@ -212,7 +219,7 @@ type (
212219
TableNames []string
213220
NewTableNames []string
214221
}
215-
222+
DBTableFieldName = string
216223
DBFieldTypeName = string
217224
CustomAttributeType struct {
218225
Type string `brief:"custom attribute type name"`
@@ -363,7 +370,7 @@ func getImportPartContent(ctx context.Context, source string, isDo bool, appendI
363370
}
364371

365372
// Check and update imports in go.mod
366-
if appendImports != nil && len(appendImports) > 0 {
373+
if len(appendImports) > 0 {
367374
goModPath := utils.GetModPath()
368375
if goModPath == "" {
369376
mlog.Fatal("go.mod not found in current project")
@@ -381,8 +388,9 @@ func getImportPartContent(ctx context.Context, source string, isDo bool, appendI
381388
}
382389
}
383390
if !found {
384-
err = gproc.ShellRun(ctx, `go get `+appendImport)
385-
mlog.Fatalf(`%+v`, err)
391+
if err = gproc.ShellRun(ctx, `go get `+appendImport); err != nil {
392+
mlog.Fatalf(`%+v`, err)
393+
}
386394
}
387395
packageImportsArray.Append(fmt.Sprintf(`"%s"`, appendImport))
388396
}

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

+8
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ func generateStructFieldDefinition(
131131
for _, v := range removeFieldPrefixArray {
132132
newFiledName = gstr.TrimLeftStr(newFiledName, v, 1)
133133
}
134+
135+
if in.FieldMapping != nil && len(in.FieldMapping) > 0 {
136+
if typeMapping, ok := in.FieldMapping[fmt.Sprintf("%s.%s", in.Tables, newFiledName)]; ok {
137+
localTypeNameStr = typeMapping.Type
138+
appendImport = typeMapping.Import
139+
}
140+
}
141+
134142
attrLines = []string{
135143
" #" + gstr.CaseCamel(newFiledName),
136144
" #" + localTypeNameStr,

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

+85
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// =================================================================================
2+
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
3+
// =================================================================================
4+
5+
package dao
6+
7+
import (
8+
"for-gendao-test/pkg/dao/internal"
9+
)
10+
11+
// internalTableUserDao is internal type for wrapping internal DAO implements.
12+
type internalTableUserDao = *internal.TableUserDao
13+
14+
// tableUserDao is the data access object for table table_user.
15+
// You can define custom methods on it to extend its functionality as you wish.
16+
type tableUserDao struct {
17+
internalTableUserDao
18+
}
19+
20+
var (
21+
// TableUser is globally public accessible object for table table_user operations.
22+
TableUser = tableUserDao{
23+
internal.NewTableUserDao(),
24+
}
25+
)
26+
27+
// Fill with you ideas below.

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

+22
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_field_mapping/model/entity/table_user.go

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contrib/rpc/grpcx/grpcx_unit_z_grpc_server_config_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func Test_Grpcx_Grpc_Server_Config_Logger(t *testing.T) {
9494
logFilePath = fmt.Sprintf("/tmp/log/%s.log", gtime.Now().Format("Y-m-d"))
9595
logFileContent = gfile.GetContents(logFilePath)
9696
)
97+
defer gfile.Remove(logFilePath)
9798
t.Assert(gfile.Exists(logFilePath), true)
9899
t.Assert(gstr.Contains(logFileContent, "TestLogger "), true)
99100
})

0 commit comments

Comments
 (0)