-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(cmd/gf): support generating pointer type field in DO files for command gf gen dao
#4653
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -211,7 +211,7 @@ func Test_Gen_Dao_TypeMapping(t *testing.T) { | |||||
| filepath.FromSlash(testPath + "/model/entity/table_user.go"), | ||||||
| } | ||||||
| for i := range files { | ||||||
| //_ = gfile.PutContents(expectFiles[i], gfile.GetContents(files[i])) | ||||||
| // _ = gfile.PutContents(expectFiles[i], gfile.GetContents(files[i])) | ||||||
| t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i])) | ||||||
| } | ||||||
| }) | ||||||
|
|
@@ -315,7 +315,7 @@ func Test_Gen_Dao_FieldMapping(t *testing.T) { | |||||
| filepath.FromSlash(testPath + "/model/entity/table_user.go"), | ||||||
| } | ||||||
| for i := range files { | ||||||
| //_ = gfile.PutContents(expectFiles[i], gfile.GetContents(files[i])) | ||||||
| // _ = gfile.PutContents(expectFiles[i], gfile.GetContents(files[i])) | ||||||
| t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i])) | ||||||
| } | ||||||
| }) | ||||||
|
|
@@ -407,7 +407,312 @@ func Test_Gen_Dao_Sqlite3(t *testing.T) { | |||||
| filepath.FromSlash(testPath + "/model/entity/table_user.go"), | ||||||
| } | ||||||
| for i := range files { | ||||||
| //_ = gfile.PutContents(expectFiles[i], gfile.GetContents(files[i])) | ||||||
| // _ = gfile.PutContents(expectFiles[i], gfile.GetContents(files[i])) | ||||||
| t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i])) | ||||||
| } | ||||||
| }) | ||||||
| } | ||||||
|
|
||||||
| func Test_Gen_Dao_TypePointer_Default(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 ( | ||||||
| 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, | ||||||
| GenTable: false, | ||||||
| TypePointerSupport: true, | ||||||
| TypeMapping: nil, | ||||||
| FieldMapping: nil, | ||||||
| } | ||||||
| ) | ||||||
| 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_type_pointer/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])) | ||||||
| } | ||||||
| }) | ||||||
| } | ||||||
|
|
||||||
| func Test_Gen_Dao_TypePointer_TypeMapping(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, | ||||||
| ) | ||||||
| ) | ||||||
| defer 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 ( | ||||||
| path = gfile.Temp(guid.S()) | ||||||
| group = "test" | ||||||
| in = gendao.CGenDaoInput{ | ||||||
| Path: path, | ||||||
| Link: link, | ||||||
| Tables: "", | ||||||
| TablesEx: "", | ||||||
| Group: group, | ||||||
| Prefix: "", | ||||||
| RemovePrefix: "", | ||||||
| JsonCase: "", | ||||||
| ImportPrefix: "", | ||||||
| DaoPath: "", | ||||||
| DoPath: "", | ||||||
| EntityPath: "", | ||||||
| TplDaoIndexPath: "", | ||||||
| TplDaoInternalPath: "", | ||||||
| TplDaoDoPath: "", | ||||||
| TplDaoEntityPath: "", | ||||||
| StdTime: false, | ||||||
| WithTime: false, | ||||||
| GJsonSupport: false, | ||||||
| OverwriteDao: false, | ||||||
| DescriptionTag: false, | ||||||
| NoJsonTag: false, | ||||||
| NoModelComment: false, | ||||||
| Clear: false, | ||||||
| GenTable: false, | ||||||
| TypePointerSupport: true, | ||||||
| TypeMapping: map[gendao.DBFieldTypeName]gendao.CustomAttributeType{ | ||||||
| "int": { | ||||||
| Type: "int64", | ||||||
| Import: "", | ||||||
| }, | ||||||
| "decimal": { | ||||||
| Type: "decimal.Decimal", | ||||||
| Import: "github.com/shopspring/decimal", | ||||||
| }, | ||||||
| }, | ||||||
| FieldMapping: nil, | ||||||
| } | ||||||
| ) | ||||||
| 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_type_mapping") | ||||||
| expectFiles := []string{ | ||||||
| filepath.FromSlash(testPath + "/dao/internal/table_user.go"), | ||||||
| filepath.FromSlash(testPath + "/dao/table_user.go"), | ||||||
| filepath.FromSlash(testPath + "/model/do_type_pointer/table_user.go"), | ||||||
| filepath.FromSlash(testPath + "/model/entity/table_user.go"), | ||||||
| } | ||||||
| for i := range files { | ||||||
| _ = gfile.PutContents(expectFiles[i], gfile.GetContents(files[i])) | ||||||
|
||||||
| _ = gfile.PutContents(expectFiles[i], gfile.GetContents(files[i])) | |
| // Do not overwrite golden files here; just compare contents. |
Copilot
AI
Feb 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same table setup pattern issue as above: there’s no upfront dropTableWithDb before running the CREATE statements, and dropTableWithDb is deferred twice. This can make the test flaky when the table already exists. Prefer dropping before setup and using a single deferred cleanup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test sets up the table without dropping it first, and also defers
dropTableWithDbtwice. If the table already exists (e.g., when running this test in isolation), the CREATE will fail before the deferred drop runs. Consider callingdropTableWithDb(db, table)before executing the SQL and keep only a singledefer dropTableWithDb(db, table)after successful setup.