Skip to content

Commit

Permalink
fix import error if generate multiple proto (zeromicro#3694)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzhaolei committed Nov 4, 2023
1 parent fd8ee0b commit df2799f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
14 changes: 9 additions & 5 deletions tools/goctl/rpc/generator/genpb.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ func (g *Generator) genPbDirect(ctx DirContext, c *ZRpcContext) error {
}

func (g *Generator) setPbDir(ctx DirContext, c *ZRpcContext) error {
pbDir, err := findPbFile(c.GoOutput, false)
pbDir, err := findPbFile(c.GoOutput, c.Src, false)
if err != nil {
return err
}
if len(pbDir) == 0 {
return fmt.Errorf("pg.go is not found under %q", c.GoOutput)
}
grpcDir, err := findPbFile(c.GrpcOutput, true)
grpcDir, err := findPbFile(c.GrpcOutput, c.Src, true)
if err != nil {
return err
}
Expand All @@ -62,7 +62,11 @@ const (
grpcSuffix = "_grpc.pb.go"
)

func findPbFile(current string, grpc bool) (string, error) {
func findPbFile(current string, src string, grpc bool) (string, error) {
protoName := strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
pbFile := protoName + "." + pbSuffix
grpcFile := protoName + grpcSuffix

fileSystem := os.DirFS(current)
var ret string
err := fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
Expand All @@ -71,11 +75,11 @@ func findPbFile(current string, grpc bool) (string, error) {
}
if strings.HasSuffix(path, pbSuffix) {
if grpc {
if strings.HasSuffix(path, grpcSuffix) {
if strings.HasSuffix(path, grpcFile) {
ret = path
return os.ErrExist
}
} else if !strings.HasSuffix(path, grpcSuffix) {
} else if strings.HasSuffix(path, pbFile) {
ret = path
return os.ErrExist
}
Expand Down
20 changes: 10 additions & 10 deletions tools/goctl/rpc/generator/genpb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ service Greeter {
t.Log(err)
return
}
pbDir, err := findPbFile(output, false)
pbDir, err := findPbFile(output, protoFile, false)
assert.Nil(t, err)
pbGo := filepath.Join(pbDir, "greet.pb.go")
assert.True(t, pathx.FileExists(pbGo))

grpcDir, err := findPbFile(output, true)
grpcDir, err := findPbFile(output, protoFile, true)
assert.Nil(t, err)
grpcGo := filepath.Join(grpcDir, "greet_grpc.pb.go")
assert.True(t, pathx.FileExists(grpcGo))
Expand All @@ -76,12 +76,12 @@ service Greeter {
t.Log(err)
return
}
pbDir, err := findPbFile(output, false)
pbDir, err := findPbFile(output, protoFile, false)
assert.Nil(t, err)
pbGo := filepath.Join(pbDir, "greet.pb.go")
assert.True(t, pathx.FileExists(pbGo))

grpcDir, err := findPbFile(output, true)
grpcDir, err := findPbFile(output, protoFile, true)
assert.Nil(t, err)
grpcGo := filepath.Join(grpcDir, "greet_grpc.pb.go")
assert.True(t, pathx.FileExists(grpcGo))
Expand All @@ -108,12 +108,12 @@ service Greeter {
t.Log(err)
return
}
pbDir, err := findPbFile(output, false)
pbDir, err := findPbFile(output, protoFile, false)
assert.Nil(t, err)
pbGo := filepath.Join(pbDir, "greet.pb.go")
assert.True(t, pathx.FileExists(pbGo))

grpcDir, err := findPbFile(output, true)
grpcDir, err := findPbFile(output, protoFile, true)
assert.Nil(t, err)
grpcGo := filepath.Join(grpcDir, "greet_grpc.pb.go")
assert.True(t, pathx.FileExists(grpcGo))
Expand All @@ -140,12 +140,12 @@ service Greeter {
t.Log(err)
return
}
pbDir, err := findPbFile(output, false)
pbDir, err := findPbFile(output, protoFile, false)
assert.Nil(t, err)
pbGo := filepath.Join(pbDir, "greet.pb.go")
assert.True(t, pathx.FileExists(pbGo))

grpcDir, err := findPbFile(output, true)
grpcDir, err := findPbFile(output, protoFile, true)
assert.Nil(t, err)
grpcGo := filepath.Join(grpcDir, "greet_grpc.pb.go")
assert.True(t, pathx.FileExists(grpcGo))
Expand Down Expand Up @@ -183,12 +183,12 @@ service Greeter {
t.Log(err)
return
}
pbDir, err := findPbFile(output, false)
pbDir, err := findPbFile(output, protoFile, false)
assert.Nil(t, err)
pbGo := filepath.Join(pbDir, "greet.pb.go")
assert.True(t, pathx.FileExists(pbGo))

grpcDir, err := findPbFile(output, true)
grpcDir, err := findPbFile(output, protoFile, true)
assert.Nil(t, err)
grpcGo := filepath.Join(grpcDir, "greet_grpc.pb.go")
assert.True(t, pathx.FileExists(grpcGo))
Expand Down

0 comments on commit df2799f

Please sign in to comment.