diff --git a/cmd/hz/protobuf/plugin.go b/cmd/hz/protobuf/plugin.go index e25803860..fa36f0c28 100644 --- a/cmd/hz/protobuf/plugin.go +++ b/cmd/hz/protobuf/plugin.go @@ -298,6 +298,7 @@ func (plugin *Plugin) fixGoPackage(req *pluginpb.CodeGeneratorRequest, pkgMap ma continue } opt := getGoPackage(f, pkgMap) + opt = strings.TrimLeft(opt, ".") if !strings.Contains(opt, gopkg) { if strings.HasPrefix(opt, "/") { opt = gopkg + opt @@ -305,6 +306,7 @@ func (plugin *Plugin) fixGoPackage(req *pluginpb.CodeGeneratorRequest, pkgMap ma opt = gopkg + "/" + opt } } + opt = strings.TrimRight(opt, "/") impt, _ := plugin.fixModelPathAndPackage(opt) *f.Options.GoPackage = impt } diff --git a/cmd/hz/protobuf/plugin_test.go b/cmd/hz/protobuf/plugin_test.go index b34d8b868..c11f1d020 100644 --- a/cmd/hz/protobuf/plugin_test.go +++ b/cmd/hz/protobuf/plugin_test.go @@ -17,6 +17,7 @@ package protobuf import ( + "google.golang.org/protobuf/types/descriptorpb" "io/ioutil" "strings" "testing" @@ -96,3 +97,19 @@ func TestFixModelPathAndPackage(t *testing.T) { } } } + +func TestFixGoPackage(t *testing.T) { + plu := &Plugin{} + plu.Package = "cloudwego/hertz" + plu.ModelDir = meta.ModelDir + req := &pluginpb.CodeGeneratorRequest{} + pbFile := &descriptorpb.FileDescriptorProto{} + fileOpt := &descriptorpb.FileOptions{} + fileOpt.GoPackage = proto.String("./") + pbFile.Options = fileOpt + req.ProtoFile = append(req.ProtoFile, pbFile) + plu.fixGoPackage(req, nil) + if req.ProtoFile[0].Options.GetGoPackage() != "cloudwego/hertz/biz/model" { + t.Fatalf("want go package: %s, but get: %s", "cloudwego/hertz/biz/model", req.ProtoFile[0].Options.GetGoPackage()) + } +}