Skip to content

Commit

Permalink
fix(cmd/gf): custom tags repeatedly added using command gf gen pb w…
Browse files Browse the repository at this point in the history
…ith `-a` option (#3966)
  • Loading branch information
wangle201210 authored Dec 5, 2024
1 parent 532e665 commit e2cafa3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
39 changes: 39 additions & 0 deletions cmd/gf/internal/cmd/cmd_z_unit_gen_pb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,42 @@ func TestGenPbIssue3882(t *testing.T) {
t.Assert(gstr.Contains(genContent, exceptText), true)
})
}

// This issue only occurs when executing multiple times
// and the subsequent OutputApi is the parent directory of the previous execution
func TestGenPbIssue3953(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
outputPath = gfile.Temp(guid.S())
outputApiPath = filepath.Join(outputPath, "api")
outputCtrlPath = filepath.Join(outputPath, "controller")

protobufFolder = gtest.DataPath("issue", "3953")
in = genpb.CGenPbInput{
Path: protobufFolder,
OutputApi: outputApiPath,
OutputCtrl: outputCtrlPath,
}
err error
)
err = gfile.Mkdir(outputApiPath)
t.AssertNil(err)
err = gfile.Mkdir(outputCtrlPath)
t.AssertNil(err)
defer gfile.Remove(outputPath)

_, err = genpb.CGenPb{}.Pb(ctx, in)
// do twice,and set outputApi to outputPath
in.OutputApi = outputPath
_, err = genpb.CGenPb{}.Pb(ctx, in)
t.AssertNil(err)

var (
genContent = gfile.GetContents(filepath.Join(outputApiPath, "issue3953.pb.go"))
// The old version would have appeared `v:"required" v:"required"`
// but the new version of the code will appear `v:"required"` only once
notExceptText = `v:"required" v:"required"`
)
t.Assert(gstr.Contains(genContent, notExceptText), false)
})
}
4 changes: 4 additions & 0 deletions cmd/gf/internal/cmd/genpb/genpb_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (c CGenPb) doTagReplacement(ctx context.Context, content string) (string, e
if !lineTagMap.IsEmpty() {
tagContent := c.listMapToStructTag(lineTagMap)
lineTagMap.Clear()
// If already have it, don't add it anymore
if gstr.Contains(gstr.StrTill(line, "` //"), tagContent) {
continue
}
line, _ = gregex.ReplaceString("`(.+)`", fmt.Sprintf("`$1 %s`", tagContent), line)
}
lines[index] = line
Expand Down
16 changes: 16 additions & 0 deletions cmd/gf/internal/cmd/testdata/issue/3953/issue3953.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
syntax = "proto3";
package account;

option go_package = "account/v1";

service Account {
rpc getUserByIds (Req) returns (Resp) {
}
}

message Req {
repeated int64 ids = 1; // v: required
}
message Resp {
repeated string data = 1;
}

0 comments on commit e2cafa3

Please sign in to comment.