Skip to content
Open
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
2 changes: 2 additions & 0 deletions cmd/hz/protobuf/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,15 @@ 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
} else {
opt = gopkg + "/" + opt
}
}
opt = strings.TrimRight(opt, "/")
impt, _ := plugin.fixModelPathAndPackage(opt)
*f.Options.GoPackage = impt
}
Expand Down
17 changes: 17 additions & 0 deletions cmd/hz/protobuf/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package protobuf

import (
"google.golang.org/protobuf/types/descriptorpb"
"io/ioutil"
"strings"
"testing"
Expand Down Expand Up @@ -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())
}
}