Skip to content

Commit

Permalink
fix issue #861 (#862)
Browse files Browse the repository at this point in the history
* fix issue #861

* fix issue #861

Co-authored-by: anqiansong <[email protected]>
  • Loading branch information
kesonan and anqiansong authored Aug 1, 2021
1 parent a5e1d0d commit c8428a7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
28 changes: 23 additions & 5 deletions tools/goctl/rpc/generator/genpb.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,32 @@ func (g *DefaultGenerator) GenPb(ctx DirContext, protoImportPath []string, proto
directory = filepath.Clean(directory)
cw.WriteString("protoc ")
protoImportPathSet := collection.NewSet()
isSamePackage := true
for _, ip := range protoImportPath {
pip := " --proto_path=" + ip
if protoImportPathSet.Contains(pip) {
continue
}

abs, err := filepath.Abs(ip)
if err != nil {
return err
}

if abs == directory {
isSamePackage = true
} else {
isSamePackage = false
}

protoImportPathSet.AddStr(pip)
cw.WriteString(pip)
}
currentPath := " --proto_path=" + directory
if !protoImportPathSet.Contains(currentPath) {
cw.WriteString(currentPath)
}

cw.WriteString(" " + proto.Name)
if strings.Contains(proto.GoPackage, "/") {
cw.WriteString(" --go_out=plugins=grpc:" + ctx.GetMain().Filename)
Expand All @@ -57,13 +70,18 @@ func (g *DefaultGenerator) GenPb(ctx DirContext, protoImportPath []string, proto
}

var currentFileOpt string
if filepath.IsAbs(proto.GoPackage) {
currentFileOpt = " --go_opt=M" + base + "=" + proto.GoPackage
} else if strings.Contains(proto.GoPackage, string(filepath.Separator)) {
currentFileOpt = " --go_opt=M" + base + "=./" + proto.GoPackage
if !isSamePackage || (len(proto.GoPackage) > 0 && proto.GoPackage != proto.Package.Name) {
if filepath.IsAbs(proto.GoPackage) {
currentFileOpt = " --go_opt=M" + base + "=" + proto.GoPackage
} else if strings.Contains(proto.GoPackage, string(filepath.Separator)) {
currentFileOpt = " --go_opt=M" + base + "=./" + proto.GoPackage
} else {
currentFileOpt = " --go_opt=M" + base + "=../" + proto.GoPackage
}
} else {
currentFileOpt = " --go_opt=M" + base + "=../" + proto.GoPackage
currentFileOpt = " --go_opt=M" + base + "=."
}

if !optSet.Contains(currentFileOpt) {
cw.WriteString(currentFileOpt)
}
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/rpc/generator/genserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
package server
import (
"context"
{{if .notStream}}"context"{{end}}
{{.imports}}
)
Expand Down

0 comments on commit c8428a7

Please sign in to comment.