Skip to content
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

fix(cmd/gf): fix pbentity generating fail in mono-repo #3547

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions cmd/gf/internal/cmd/cmd_z_unit_gen_pbentity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
}
})
}
17 changes: 3 additions & 14 deletions cmd/gf/internal/cmd/genpbentity/genpbentity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
21 changes: 21 additions & 0 deletions cmd/gf/internal/cmd/testdata/issue/3545/table_user.proto
Original file line number Diff line number Diff line change
@@ -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
}
Loading