From 4cce8557e699f300ef7ab546227cf52c03288103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=B7=E4=BA=AE?= <739476267@qq.com> Date: Thu, 21 Dec 2023 10:16:14 +0800 Subject: [PATCH] fix: #2924 (#3177) --- cmd/gf/internal/cmd/cmd_z_init_test.go | 31 +++++++- .../internal/cmd/cmd_z_unit_gen_dao_test.go | 24 ++----- .../cmd/cmd_z_unit_gen_pbentity_test.go | 70 +++++++++++++++++++ .../internal/cmd/genpbentity/genpbentity.go | 29 ++------ .../generated_user/table_user.proto | 21 ++++++ .../cmd/testdata/genpbentity/user.tpl.sql | 10 +++ 6 files changed, 138 insertions(+), 47 deletions(-) create mode 100644 cmd/gf/internal/cmd/cmd_z_unit_gen_pbentity_test.go create mode 100644 cmd/gf/internal/cmd/testdata/genpbentity/generated_user/table_user.proto create mode 100644 cmd/gf/internal/cmd/testdata/genpbentity/user.tpl.sql diff --git a/cmd/gf/internal/cmd/cmd_z_init_test.go b/cmd/gf/internal/cmd/cmd_z_init_test.go index 8ad499ff427..1e71cbd8bb6 100644 --- a/cmd/gf/internal/cmd/cmd_z_init_test.go +++ b/cmd/gf/internal/cmd/cmd_z_init_test.go @@ -6,6 +6,33 @@ package cmd -import "context" +import ( + "context" + "fmt" -var ctx = context.Background() + "github.com/gogf/gf/v2/database/gdb" + "github.com/gogf/gf/v2/test/gtest" +) + +var ( + ctx = context.Background() + testDB gdb.DB + link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test?loc=Local&parseTime=true" +) + +func init() { + var err error + testDB, err = gdb.New(gdb.ConfigNode{ + Link: link, + }) + if err != nil { + panic(err) + } +} + +func dropTableWithDb(db gdb.DB, table string) { + dropTableStmt := fmt.Sprintf("DROP TABLE IF EXISTS `%s`", table) + if _, err := db.Exec(ctx, dropTableStmt); err != nil { + gtest.Error(err) + } +} 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 18323b03482..71818fb82dc 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 @@ -12,7 +12,6 @@ import ( "testing" "github.com/gogf/gf/cmd/gf/v2/internal/cmd/gendao" - "github.com/gogf/gf/v2/database/gdb" "github.com/gogf/gf/v2/os/gfile" "github.com/gogf/gf/v2/test/gtest" "github.com/gogf/gf/v2/text/gstr" @@ -20,22 +19,11 @@ import ( "github.com/gogf/gf/v2/util/gutil" ) -func dropTableWithDb(db gdb.DB, table string) { - dropTableStmt := fmt.Sprintf("DROP TABLE IF EXISTS `%s`", table) - if _, err := db.Exec(ctx, dropTableStmt); err != nil { - gtest.Error(err) - } -} - func Test_Gen_Dao_Default(t *testing.T) { - link := "mysql:root:12345678@tcp(127.0.0.1:3306)/test?loc=Local&parseTime=true" - db, err := gdb.New(gdb.ConfigNode{ - Link: link, - }) - gtest.AssertNil(err) - gtest.C(t, func(t *gtest.T) { var ( + err error + db = testDB table = "table_user" sqlContent = fmt.Sprintf( gtest.DataContent(`gendao`, `user.tpl.sql`), @@ -123,14 +111,10 @@ func Test_Gen_Dao_Default(t *testing.T) { } func Test_Gen_Dao_TypeMapping(t *testing.T) { - link := "mysql:root:12345678@tcp(127.0.0.1:3306)/test?loc=Local&parseTime=true" - db, err := gdb.New(gdb.ConfigNode{ - Link: link, - }) - gtest.AssertNil(err) - gtest.C(t, func(t *gtest.T) { var ( + err error + db = testDB table = "table_user" sqlContent = fmt.Sprintf( gtest.DataContent(`gendao`, `user.tpl.sql`), diff --git a/cmd/gf/internal/cmd/cmd_z_unit_gen_pbentity_test.go b/cmd/gf/internal/cmd/cmd_z_unit_gen_pbentity_test.go new file mode 100644 index 00000000000..48ebdb9a4ff --- /dev/null +++ b/cmd/gf/internal/cmd/cmd_z_unit_gen_pbentity_test.go @@ -0,0 +1,70 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package cmd + +import ( + "fmt" + "os" + "path/filepath" + "testing" + + "github.com/gogf/gf/v2/os/gcmd" + "github.com/gogf/gf/v2/os/gfile" + "github.com/gogf/gf/v2/test/gtest" + "github.com/gogf/gf/v2/text/gstr" + "github.com/gogf/gf/v2/util/guid" +) + +func Test_Gen_Pbentity_NameCase(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + var ( + err error + db = testDB + table = "table_user" + sqlContent = fmt.Sprintf( + gtest.DataContent(`genpbentity`, `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()) + err = gfile.Mkdir(path) + t.AssertNil(err) + defer gfile.Remove(path) + + root, err := gcmd.NewFromObject(GF) + t.AssertNil(err) + err = root.AddObject( + Gen, + ) + t.AssertNil(err) + os.Args = []string{"gf", "gen", "pbentity", "-l", link, "-p", path, "-package=unittest", "-nameCase=SnakeScreaming"} + + err = root.RunWithError(ctx) + t.AssertNil(err) + + files := []string{ + filepath.FromSlash(path + "/table_user.proto"), + } + + testPath := gtest.DataPath("genpbentity", "generated_user") + expectFiles := []string{ + filepath.FromSlash(testPath + "/table_user.proto"), + } + // check files content + for i := range files { + t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i])) + } + }) +} diff --git a/cmd/gf/internal/cmd/genpbentity/genpbentity.go b/cmd/gf/internal/cmd/genpbentity/genpbentity.go index 055d241b8bf..1263ac1e21e 100644 --- a/cmd/gf/internal/cmd/genpbentity/genpbentity.go +++ b/cmd/gf/internal/cmd/genpbentity/genpbentity.go @@ -40,7 +40,7 @@ type ( RemovePrefix string `name:"removePrefix" short:"r" brief:"{CGenPbEntityBriefRemovePrefix}"` RemoveFieldPrefix string `name:"removeFieldPrefix" short:"rf" brief:"{CGenPbEntityBriefRemoveFieldPrefix}"` NameCase string `name:"nameCase" short:"n" brief:"{CGenPbEntityBriefNameCase}" d:"Camel"` - JsonCase string `name:"jsonCase" short:"j" brief:"{CGenPbEntityBriefJsonCase}" d:"CamelLower"` + JsonCase string `name:"jsonCase" short:"j" brief:"{CGenPbEntityBriefJsonCase}" d:"none"` Option string `name:"option" short:"o" brief:"{CGenPbEntityBriefOption}"` } CGenPbEntityOutput struct{} @@ -342,6 +342,7 @@ func generateMessageFieldForPbEntity(index int, field *gdb.TableField, in CGenPb comment = gstr.Replace(comment, `\n`, " ") comment, _ = gregex.ReplaceString(`\s{2,}`, ` `, comment) if jsonTagName := formatCase(field.Name, in.JsonCase); jsonTagName != "" { + jsonTagStr = fmt.Sprintf(`[json_name = "%s"]`, jsonTagName) // beautiful indent. if index < 10 { // 3 spaces @@ -378,32 +379,10 @@ func getTplPbEntityContent(tplEntityPath string) string { // formatCase call gstr.Case* function to convert the s to specified case. func formatCase(str, caseStr string) string { - switch gstr.ToLower(caseStr) { - case gstr.ToLower("Camel"): - return gstr.CaseCamel(str) - - case gstr.ToLower("CamelLower"): - return gstr.CaseCamelLower(str) - - case gstr.ToLower("Kebab"): - return gstr.CaseKebab(str) - - case gstr.ToLower("KebabScreaming"): - return gstr.CaseKebabScreaming(str) - - case gstr.ToLower("Snake"): - return gstr.CaseSnake(str) - - case gstr.ToLower("SnakeFirstUpper"): - return gstr.CaseSnakeFirstUpper(str) - - case gstr.ToLower("SnakeScreaming"): - return gstr.CaseSnakeScreaming(str) - - case "none": + if caseStr == "none" { return "" } - return str + return gstr.CaseConvert(str, gstr.CaseTypeMatch(caseStr)) } func sortFieldKeyForPbEntity(fieldMap map[string]*gdb.TableField) []string { diff --git a/cmd/gf/internal/cmd/testdata/genpbentity/generated_user/table_user.proto b/cmd/gf/internal/cmd/testdata/genpbentity/generated_user/table_user.proto new file mode 100644 index 00000000000..365999ece70 --- /dev/null +++ b/cmd/gf/internal/cmd/testdata/genpbentity/generated_user/table_user.proto @@ -0,0 +1,21 @@ +// ========================================================================== +// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. +// ========================================================================== + +syntax = "proto3"; + +package unittest; + +option go_package = "unittest"; + +import "google/protobuf/timestamp.proto"; + +message TableUser { + uint32 ID = 1; // User ID + string PASSPORT = 2; // User Passport + string PASSWORD = 3; // User Password + string NICKNAME = 4; // User Nickname + string SCORE = 5; // Total score amount. + google.protobuf.Timestamp CREATE_AT = 6; // Created Time + google.protobuf.Timestamp UPDATE_AT = 7; // Updated Time +} \ No newline at end of file diff --git a/cmd/gf/internal/cmd/testdata/genpbentity/user.tpl.sql b/cmd/gf/internal/cmd/testdata/genpbentity/user.tpl.sql new file mode 100644 index 00000000000..f27fc11bd31 --- /dev/null +++ b/cmd/gf/internal/cmd/testdata/genpbentity/user.tpl.sql @@ -0,0 +1,10 @@ +CREATE TABLE `%s` ( + `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'User ID', + `passport` varchar(45) NOT NULL COMMENT 'User Passport', + `password` varchar(45) NOT NULL COMMENT 'User Password', + `nickname` varchar(45) NOT NULL COMMENT 'User Nickname', + `score` decimal(10,2) unsigned DEFAULT NULL COMMENT 'Total score amount.', + `create_at` datetime DEFAULT NULL COMMENT 'Created Time', + `update_at` datetime DEFAULT NULL COMMENT 'Updated Time', + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8;