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 index 1946e91d9..4a105697d 100644 --- a/cmd/gf/internal/cmd/cmd_z_unit_gen_pbentity_test.go +++ b/cmd/gf/internal/cmd/cmd_z_unit_gen_pbentity_test.go @@ -144,3 +144,67 @@ func Test_Gen_Pbentity_NameCase_SnakeScreaming(t *testing.T) { } }) } + +// https://github.com/gogf/gf/issues/3545 +func Test_Issue_3545(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()) + in = genpbentity.CGenPbEntityInput{ + Path: path, + Package: "", + Link: link, + Tables: "", + Prefix: "", + RemovePrefix: "", + RemoveFieldPrefix: "", + NameCase: "", + JsonCase: "", + Option: "", + } + ) + err = gutil.FillStructWithDefault(&in) + t.AssertNil(err) + + err = gfile.Mkdir(path) + t.AssertNil(err) + defer gfile.Remove(path) + + _, err = genpbentity.CGenPbEntity{}.PbEntity(ctx, in) + t.AssertNil(err) + + // files + files, err := gfile.ScanDir(path, "*.proto", false) + t.AssertNil(err) + t.Assert(files, []string{ + path + filepath.FromSlash("/table_user.proto"), + }) + + // contents + testPath := gtest.DataPath("issue", "3545") + expectFiles := []string{ + testPath + filepath.FromSlash("/table_user.proto"), + } + 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 1263ac1e2..0c375593d 100644 --- a/cmd/gf/internal/cmd/genpbentity/genpbentity.go +++ b/cmd/gf/internal/cmd/genpbentity/genpbentity.go @@ -13,6 +13,7 @@ import ( "path/filepath" "strings" + "github.com/gogf/gf/cmd/gf/v2/internal/utility/utils" "github.com/olekukonko/tablewriter" "github.com/gogf/gf/cmd/gf/v2/internal/consts" @@ -176,20 +177,8 @@ func doGenPbEntityForArray(ctx context.Context, index int, in CGenPbEntityInput) } if in.Package == "" { mlog.Debug(`package parameter is empty, trying calculating the package path using go.mod`) - if !gfile.Exists("go.mod") { - mlog.Fatal("go.mod does not exist in current working directory") - } - var ( - modName string - goModContent = gfile.GetContents("go.mod") - match, _ = gregex.MatchString(`^module\s+(.+)\s*`, goModContent) - ) - if len(match) > 1 { - modName = gstr.Trim(match[1]) - in.Package = modName + "/" + defaultPackageSuffix - } else { - mlog.Fatal("module name does not found in go.mod") - } + modName := utils.GetImportPath(gfile.Pwd()) + in.Package = modName + "/" + defaultPackageSuffix } removePrefixArray := gstr.SplitAndTrim(in.RemovePrefix, ",") // It uses user passed database configuration. diff --git a/cmd/gf/internal/cmd/testdata/issue/3545/table_user.proto b/cmd/gf/internal/cmd/testdata/issue/3545/table_user.proto new file mode 100644 index 000000000..6282f3a61 --- /dev/null +++ b/cmd/gf/internal/cmd/testdata/issue/3545/table_user.proto @@ -0,0 +1,21 @@ +// ========================================================================== +// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. +// ========================================================================== + +syntax = "proto3"; + +package pbentity; + +option go_package = "github.com/gogf/gf/cmd/gf/v2/internal/cmd/api/pbentity"; + +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 CreateAt = 6; // Created Time + google.protobuf.Timestamp UpdateAt = 7; // Updated Time +} \ No newline at end of file