Feature rpc protoc (#1251)

* code generation by protoc

* generate pb by protoc direct

* support: grpc code generation by protoc directly

* format code

* check --go_out & --go-grpc_out

* fix typo

* Update version

* fix typo

* optimize: remove deprecated unit test

* format code

Co-authored-by: anqiansong <anqiansong@bytedance.com>
This commit is contained in:
anqiansong
2022-01-11 20:34:25 +08:00
committed by GitHub
parent 2203809e5e
commit 9b592b3dee
13 changed files with 495 additions and 79 deletions

View File

@@ -8,15 +8,20 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/rpc/generator"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/console"
"github.com/tal-tech/go-zero/tools/goctl/util/env"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
)
// Deprecated: use ZRPC instead.
// RPC is to generate rpc service code from a proto file by specifying a proto file using flag src,
// you can specify a target folder for code generation, when the proto file has import, you can specify
// the import search directory through the proto_path command, for specific usage, please refer to protoc -h
func RPC(c *cli.Context) error {
console.Warning("deprecated: use %q instead, for the details see %q",
"goctl rpc protoc", "goctl rpc protoc --help")
if err := prepare(); err != nil {
return err
}
@@ -73,6 +78,9 @@ func prepare() error {
// RPCNew is to generate rpc greet service, this greet service can speed
// up your understanding of the zrpc service structure
func RPCNew(c *cli.Context) error {
console.Warning("deprecated: it will be removed in the feature, zrpc code generation please use %q instead",
"goctl rpc protoc")
rpcname := c.Args().First()
ext := filepath.Ext(rpcname)
if len(ext) > 0 {

238
tools/goctl/rpc/cli/zrpc.go Normal file
View File

@@ -0,0 +1,238 @@
package cli
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/emicklei/proto"
"github.com/tal-tech/go-zero/tools/goctl/rpc/generator"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
)
var (
errInvalidGrpcOutput = errors.New("ZRPC: missing grpc output")
errInvalidZrpcOutput = errors.New("ZRPC: missing zrpc output, please use --zrpc_out to specify the output")
errInvalidInput = errors.New("ZRPC: missing source")
errMultiInput = errors.New("ZRPC: only one source is expected")
)
const (
optImport = "import"
optSourceRelative = "source_relative"
)
// ZRPC generates grpc code directly by protoc and generates
// zrpc code by goctl.
func ZRPC(c *cli.Context) error {
args := c.Parent().Args()
protocArgs := removeGoctlFlag(args)
pwd, err := os.Getwd()
if err != nil {
return err
}
source, err := getSourceProto(c.Args(), pwd)
if err != nil {
return err
}
src := filepath.Dir(source)
goPackage, protoPkg, err := getGoPackage(source)
if err != nil {
return err
}
grpcOut := c.String("go-grpc_out")
goOut := c.String("go_out")
goOpt := c.String("go_opt")
grpcOpt := c.String("go-grpc_opt")
zrpcOut := c.String("zrpc_out")
style := c.String("style")
home := c.String("home")
remote := c.String("remote")
if len(remote) > 0 {
repo, _ := util.CloneIntoGitHome(remote)
if len(repo) > 0 {
home = repo
}
}
if len(home) > 0 {
pathx.RegisterGoctlHome(home)
}
if len(goOut) == 0 {
return errInvalidGrpcOutput
}
if len(zrpcOut) == 0 {
return errInvalidZrpcOutput
}
if !filepath.IsAbs(zrpcOut) {
zrpcOut = filepath.Join(pwd, zrpcOut)
}
goOut = removePluginFlag(goOut)
goOut, err = parseOutOut(src, goOut, goOpt, goPackage)
if err != nil {
return err
}
var isGoolePlugin = len(grpcOut) > 0
// If grpcOut is not empty means that user generates grpc code by
// https://google.golang.org/protobuf/cmd/protoc-gen-go and
// https://google.golang.org/grpc/cmd/protoc-gen-go-grpc,
// for details please see https://grpc.io/docs/languages/go/quickstart/
if isGoolePlugin {
grpcOut, err = parseOutOut(src, grpcOut, grpcOpt, goPackage)
if err != nil {
return err
}
} else {
// Else it means that user generates grpc code by
// https://github.com/golang/protobuf/tree/master/protoc-gen-go
grpcOut = goOut
}
goOut, err = filepath.Abs(goOut)
if err != nil {
return err
}
grpcOut, err = filepath.Abs(grpcOut)
if err != nil {
return err
}
zrpcOut, err = filepath.Abs(zrpcOut)
if err != nil {
return err
}
if isGoolePlugin && grpcOut != goOut {
return fmt.Errorf("the --go_out and --go-grpc_out must be the same")
}
if goOut == zrpcOut || grpcOut == zrpcOut {
recommendName := goPackage
if len(recommendName) == 0 {
recommendName = protoPkg
}
return fmt.Errorf("the zrpc and grpc output can not be the same, it is recommended to output grpc to the %q",
filepath.Join(goOut, recommendName))
}
var ctx generator.ZRpcContext
ctx.Src = source
ctx.ProtoGenGoDir = goOut
ctx.ProtoGenGrpcDir = grpcOut
ctx.Output = zrpcOut
ctx.ProtocCmd = strings.Join(protocArgs, " ")
g, err := generator.NewDefaultRPCGenerator(style, generator.WithZRpcContext(&ctx))
if err != nil {
return err
}
return g.Generate(source, zrpcOut, nil)
}
// parseOutOut calculates the output place to grpc code, about to calculate logic for details
// please see https://developers.google.com/protocol-buffers/docs/reference/go-generated#invocation.
func parseOutOut(sourceDir, grpcOut, grpcOpt, goPackage string) (string, error) {
if !filepath.IsAbs(grpcOut) {
grpcOut = filepath.Join(sourceDir, grpcOut)
}
switch grpcOpt {
case "", optImport:
grpcOut = filepath.Join(grpcOut, goPackage)
case optSourceRelative:
grpcOut = filepath.Join(grpcOut)
default:
return "", fmt.Errorf("parseAndSetGrpcOut: unknown path type %q: want %q or %q",
grpcOpt, optImport, optSourceRelative)
}
return grpcOut, nil
}
func getGoPackage(source string) (string, string, error) {
r, err := os.Open(source)
if err != nil {
return "", "", err
}
defer func() {
_ = r.Close()
}()
parser := proto.NewParser(r)
set, err := parser.Parse()
if err != nil {
return "", "", err
}
var goPackage, protoPkg string
proto.Walk(set, proto.WithOption(func(option *proto.Option) {
if option.Name == "go_package" {
goPackage = option.Constant.Source
}
}), proto.WithPackage(func(p *proto.Package) {
protoPkg = p.Name
}))
return goPackage, protoPkg, nil
}
func removeGoctlFlag(args []string) []string {
var ret []string
var step int
for step < len(args) {
arg := args[step]
switch {
case arg == "--style", arg == "--home", arg == "--zrpc_out":
step += 2
continue
case strings.HasPrefix(arg, "--style="),
strings.HasPrefix(arg, "--home="),
strings.HasPrefix(arg, "--zrpc_out="):
step += 1
continue
}
step += 1
ret = append(ret, arg)
}
return ret
}
func getSourceProto(args []string, pwd string) (string, error) {
var source []string
for _, p := range args {
if strings.HasSuffix(p, ".proto") {
source = append(source, p)
}
}
switch len(source) {
case 0:
return "", errInvalidInput
case 1:
isAbs := filepath.IsAbs(source[0])
if isAbs {
return source[0], nil
}
abs := filepath.Join(pwd, source[0])
return abs, nil
default:
return "", errMultiInput
}
}
func removePluginFlag(goOut string) string {
goOut = strings.ReplaceAll(goOut, "plugins=", "")
index := strings.LastIndex(goOut, ":")
if index < 0 {
return goOut
}
return goOut[index+1:]
}

View File

@@ -0,0 +1,113 @@
package cli
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/tools/goctl/util/console"
)
type test struct {
source []string
expected string
expectedErr error
}
func Test_GetSourceProto(t *testing.T) {
pwd, err := os.Getwd()
if err != nil {
console.Error(err.Error())
return
}
var testData = []test{
{
source: []string{"a.proto"},
expected: filepath.Join(pwd, "a.proto"),
},
{
source: []string{"/foo/bar/a.proto"},
expected: "/foo/bar/a.proto",
},
{
source: []string{"a.proto", "b.proto"},
expectedErr: errMultiInput,
},
{
source: []string{"", "--go_out=."},
expectedErr: errInvalidInput,
},
}
for _, d := range testData {
ret, err := getSourceProto(d.source, pwd)
if d.expectedErr != nil {
assert.Equal(t, d.expectedErr, err)
continue
}
assert.Equal(t, d.expected, ret)
}
}
func Test_RemoveGoctlFlag(t *testing.T) {
var testData = []test{
{
source: strings.Fields("protoc foo.proto --go_out=. --go_opt=bar --zrpc_out=. --style go-zero --home=foo"),
expected: "protoc foo.proto --go_out=. --go_opt=bar",
},
{
source: strings.Fields("foo bar foo.proto"),
expected: "foo bar foo.proto",
},
{
source: strings.Fields("protoc foo.proto --go_out . --style=go_zero --home ."),
expected: "protoc foo.proto --go_out .",
},
{
source: strings.Fields(`protoc foo.proto --go_out . --style="go_zero" --home="."`),
expected: "protoc foo.proto --go_out .",
},
{
source: strings.Fields(`protoc foo.proto --go_opt=. --zrpc_out . --style=goZero --home=bar`),
expected: "protoc foo.proto --go_opt=.",
},
{
source: strings.Fields(`protoc foo.proto --go_opt=. --zrpc_out="bar" --style=goZero --home=bar`),
expected: "protoc foo.proto --go_opt=.",
},
}
for _, e := range testData {
cmd := strings.Join(removeGoctlFlag(e.source), " ")
assert.Equal(t, e.expected, cmd)
}
}
func Test_RemovePluginFlag(t *testing.T) {
var testData = []test{
{
source: strings.Fields("plugins=grpc:."),
expected: ".",
},
{
source: strings.Fields("plugins=g1,g2:."),
expected: ".",
},
{
source: strings.Fields("g1,g2:."),
expected: ".",
},
{
source: strings.Fields("plugins=g1,g2:foo"),
expected: "foo",
},
}
for _, e := range testData {
data := removePluginFlag(e.source[0])
assert.Equal(t, e.expected, data)
}
}