Code optimized (#523)

* optimized markdown generator

* optimized markdown generator

* optimized markdown generator

* add more comment

* add comment

* add comment

* add comments for rpc tool

* add comments for model tool

* add comments for model tool

* add comments for model tool

* add comments for config tool

* add comments for config tool

* add comments

* add comments

* add comments

* add comments

* add comment

* remove rpc main head info

* add comment

* optimized

Co-authored-by: anqiansong <anqiansong@xiaoheiban.cn>
This commit is contained in:
kingxt
2021-02-26 16:11:47 +08:00
committed by GitHub
parent ef146cf5ba
commit e6ef1fca12
104 changed files with 651 additions and 375 deletions

View File

@@ -9,10 +9,10 @@ import (
"github.com/urfave/cli"
)
// Rpc is to generate rpc service code from a proto file by specifying a proto file using flag src,
// 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 {
func RPC(c *cli.Context) error {
src := c.String("src")
out := c.String("dir")
style := c.String("style")
@@ -24,7 +24,7 @@ func Rpc(c *cli.Context) error {
return errors.New("missing -dir")
}
g, err := generator.NewDefaultRpcGenerator(style)
g, err := generator.NewDefaultRPCGenerator(style)
if err != nil {
return err
}
@@ -32,9 +32,9 @@ func Rpc(c *cli.Context) error {
return g.Generate(src, out, protoImportPath)
}
// RpcNew is to generate rpc greet service, this greet service can speed
// 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 {
func RPCNew(c *cli.Context) error {
rpcname := c.Args().First()
ext := filepath.Ext(rpcname)
if len(ext) > 0 {
@@ -54,7 +54,7 @@ func RpcNew(c *cli.Context) error {
return err
}
g, err := generator.NewDefaultRpcGenerator(style)
g, err := generator.NewDefaultRPCGenerator(style)
if err != nil {
return err
}
@@ -62,7 +62,8 @@ func RpcNew(c *cli.Context) error {
return g.Generate(src, filepath.Dir(src), nil)
}
func RpcTemplate(c *cli.Context) error {
// RPCTemplate is the entry for generate rpc template
func RPCTemplate(c *cli.Context) error {
protoFile := c.String("o")
if len(protoFile) == 0 {
return errors.New("missing -o")

View File

@@ -12,6 +12,9 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/vars"
)
// Run provides the execution of shell scripts in golang,
// which can support macOS, Windows, and Linux operating systems.
// Other operating systems are currently not supported
func Run(arg string, dir string, in ...*bytes.Buffer) (string, error) {
goos := runtime.GOOS
var cmd *exec.Cmd

View File

@@ -6,18 +6,22 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/util/console"
)
type defaultGenerator struct {
// DefaultGenerator defines the environment needs of rpc service generation
type DefaultGenerator struct {
log console.Console
}
func NewDefaultGenerator() *defaultGenerator {
// NewDefaultGenerator returns an instance of DefaultGenerator
func NewDefaultGenerator() *DefaultGenerator {
log := console.NewColorConsole()
return &defaultGenerator{
return &DefaultGenerator{
log: log,
}
}
func (g *defaultGenerator) Prepare() error {
// Prepare provides environment detection generated by rpc service,
// including go environment, protoc, whether protoc-gen-go is installed or not
func (g *DefaultGenerator) Prepare() error {
_, err := exec.LookPath("go")
if err != nil {
return err

View File

@@ -10,27 +10,33 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/util/ctx"
)
type RpcGenerator struct {
// RPCGenerator defines a generator and configure
type RPCGenerator struct {
g Generator
cfg *conf.Config
}
func NewDefaultRpcGenerator(style string) (*RpcGenerator, error) {
// NewDefaultRPCGenerator wraps Generator with configure
func NewDefaultRPCGenerator(style string) (*RPCGenerator, error) {
cfg, err := conf.NewConfig(style)
if err != nil {
return nil, err
}
return NewRpcGenerator(NewDefaultGenerator(), cfg), nil
return NewRPCGenerator(NewDefaultGenerator(), cfg), nil
}
func NewRpcGenerator(g Generator, cfg *conf.Config) *RpcGenerator {
return &RpcGenerator{
// NewRPCGenerator creates an instance for RPCGenerator
func NewRPCGenerator(g Generator, cfg *conf.Config) *RPCGenerator {
return &RPCGenerator{
g: g,
cfg: cfg,
}
}
func (g *RpcGenerator) Generate(src, target string, protoImportPath []string) error {
// Generate generates an rpc service, through the proto file,
// code storage directory, and proto import parameters to control
// the source file and target location of the rpc service that needs to be generated
func (g *RPCGenerator) Generate(src, target string, protoImportPath []string) error {
abs, err := filepath.Abs(target)
if err != nil {
return err

View File

@@ -27,7 +27,7 @@ func TestRpcGenerate(t *testing.T) {
return
}
projectName := stringx.Rand()
g := NewRpcGenerator(dispatcher, cfg)
g := NewRPCGenerator(dispatcher, cfg)
// case go path
src := filepath.Join(build.Default.GOPATH, "src")

View File

@@ -61,7 +61,9 @@ func (m *default{{.serviceName}}) {{.method}}(ctx context.Context,in *{{.pbReque
`
)
func (g *defaultGenerator) GenCall(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
// GenCall generates the rpc client code, which is the entry point for the rpc service call.
// It is a layer of encapsulation for the rpc client and shields the details in the pb.
func (g *DefaultGenerator) GenCall(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
dir := ctx.GetCall()
service := proto.Service
head := util.GetHead(proto.Name)
@@ -105,7 +107,7 @@ func (g *defaultGenerator) GenCall(ctx DirContext, proto parser.Proto, cfg *conf
return err
}
func (g *defaultGenerator) genFunction(goPackage string, service parser.Service) ([]string, error) {
func (g *DefaultGenerator) genFunction(goPackage string, service parser.Service) ([]string, error) {
functions := make([]string, 0)
for _, rpc := range service.RPC {
text, err := util.LoadTemplate(category, callFunctionTemplateFile, callFunctionTemplate)
@@ -133,7 +135,7 @@ func (g *defaultGenerator) genFunction(goPackage string, service parser.Service)
return functions, nil
}
func (g *defaultGenerator) getInterfaceFuncs(service parser.Service) ([]string, error) {
func (g *DefaultGenerator) getInterfaceFuncs(service parser.Service) ([]string, error) {
functions := make([]string, 0)
for _, rpc := range service.RPC {

View File

@@ -20,7 +20,11 @@ type Config struct {
}
`
func (g *defaultGenerator) GenConfig(ctx DirContext, _ parser.Proto, cfg *conf.Config) error {
// GenConfig generates the configuration structure definition file of the rpc service,
// which contains the zrpc.RpcServerConf configuration item by default.
// You can specify the naming style of the target file name through config.Config. For details,
// see https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/config.go
func (g *DefaultGenerator) GenConfig(ctx DirContext, _ parser.Proto, cfg *conf.Config) error {
dir := ctx.GetConfig()
configFilename, err := format.FileNamingFormat(cfg.NamingFormat, "config")
if err != nil {

View File

@@ -5,6 +5,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
)
// Generator defines a generator interface to describe how to generate rpc service
type Generator interface {
Prepare() error
GenMain(ctx DirContext, proto parser.Proto, cfg *conf.Config) error

View File

@@ -20,7 +20,9 @@ Etcd:
Key: {{.serviceName}}.rpc
`
func (g *defaultGenerator) GenEtc(ctx DirContext, _ parser.Proto, cfg *conf.Config) error {
// GenEtc generates the yaml configuration file of the rpc service,
// including host, port monitoring configuration items and etcd configuration
func (g *DefaultGenerator) GenEtc(ctx DirContext, _ parser.Proto, cfg *conf.Config) error {
dir := ctx.GetEtc()
etcFilename, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetServiceName().Source())
if err != nil {

View File

@@ -48,7 +48,8 @@ func (l *{{.logicName}}) {{.method}} (in {{.request}}) ({{.response}}, error) {
`
)
func (g *defaultGenerator) GenLogic(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
// GenLogic generates the logic file of the rpc service, which corresponds to the RPC definition items in proto.
func (g *DefaultGenerator) GenLogic(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
dir := ctx.GetLogic()
for _, rpc := range proto.Service.RPC {
logicFilename, err := format.FileNamingFormat(cfg.NamingFormat, rpc.Name+"_logic")
@@ -81,7 +82,7 @@ func (g *defaultGenerator) GenLogic(ctx DirContext, proto parser.Proto, cfg *con
return nil
}
func (g *defaultGenerator) genLogicFunction(goPackage string, rpc *parser.RPC) (string, error) {
func (g *DefaultGenerator) genLogicFunction(goPackage string, rpc *parser.RPC) (string, error) {
var functions = make([]string, 0)
text, err := util.LoadTemplate(category, logicFuncTemplateFileFile, logicFunctionTemplate)
if err != nil {

View File

@@ -12,9 +12,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)
const mainTemplate = `{{.head}}
package main
const mainTemplate = `package main
import (
"flag"
@@ -47,7 +45,8 @@ func main() {
}
`
func (g *defaultGenerator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
// GenMain generates the main file of the rpc service, which is an rpc service program call entry
func (g *DefaultGenerator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
mainFilename, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetServiceName().Source())
if err != nil {
return err
@@ -60,14 +59,12 @@ func (g *defaultGenerator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf
remoteImport := fmt.Sprintf(`"%v"`, ctx.GetServer().Package)
configImport := fmt.Sprintf(`"%v"`, ctx.GetConfig().Package)
imports = append(imports, configImport, pbImport, remoteImport, svcImport)
head := util.GetHead(proto.Name)
text, err := util.LoadTemplate(category, mainTemplateFile, mainTemplate)
if err != nil {
return err
}
return util.With("main").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
"head": head,
"serviceName": strings.ToLower(ctx.GetServiceName().ToCamel()),
"imports": strings.Join(imports, util.NL),
"pkg": proto.PbPackage,

View File

@@ -10,7 +10,9 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
)
func (g *defaultGenerator) GenPb(ctx DirContext, protoImportPath []string, proto parser.Proto, _ *conf.Config) error {
// GenPb generates the pb.go file, which is a layer of packaging for protoc to generate gprc,
// but the commands and flags in protoc are not completely joined in goctl. At present, proto_path(-I) is introduced
func (g *DefaultGenerator) GenPb(ctx DirContext, protoImportPath []string, proto parser.Proto, _ *conf.Config) error {
dir := ctx.GetPb()
cw := new(bytes.Buffer)
base := filepath.Dir(proto.Src)

View File

@@ -45,7 +45,8 @@ func (s *{{.server}}Server) {{.method}} (ctx context.Context, in {{.request}}) (
`
)
func (g *defaultGenerator) GenServer(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
// GenServer generates rpc server file, which is an implementation of rpc server
func (g *DefaultGenerator) GenServer(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
dir := ctx.GetServer()
logicImport := fmt.Sprintf(`"%v"`, ctx.GetLogic().Package)
svcImport := fmt.Sprintf(`"%v"`, ctx.GetSvc().Package)
@@ -81,7 +82,7 @@ func (g *defaultGenerator) GenServer(ctx DirContext, proto parser.Proto, cfg *co
return err
}
func (g *defaultGenerator) genFunctions(goPackage string, service parser.Service) ([]string, error) {
func (g *DefaultGenerator) genFunctions(goPackage string, service parser.Service) ([]string, error) {
var functionList []string
for _, rpc := range service.RPC {
text, err := util.LoadTemplate(category, serverFuncTemplateFile, functionTemplate)

View File

@@ -25,7 +25,9 @@ func NewServiceContext(c config.Config) *ServiceContext {
}
`
func (g *defaultGenerator) GenSvc(ctx DirContext, _ parser.Proto, cfg *conf.Config) error {
// GenSvc generates the servicecontext.go file, which is the resource dependency of a service,
// such as rpc dependency, model dependency, etc.
func (g *DefaultGenerator) GenSvc(ctx DirContext, _ parser.Proto, cfg *conf.Config) error {
dir := ctx.GetSvc()
svcFilename, err := format.FileNamingFormat(cfg.NamingFormat, "service_context")
if err != nil {

View File

@@ -23,6 +23,7 @@ const (
)
type (
// DirContext defines a rpc service directories context
DirContext interface {
GetCall() Dir
GetEtc() Dir
@@ -36,11 +37,13 @@ type (
GetServiceName() stringx.String
}
// Dir defines a directory
Dir struct {
Base string
Filename string
Package string
}
defaultDirContext struct {
inner map[string]Dir
serviceName stringx.String
@@ -159,6 +162,7 @@ func (d *defaultDirContext) GetServiceName() stringx.String {
return d.serviceName
}
// Valid returns true if the directory is valid
func (d *Dir) Valid() bool {
return len(d.Filename) > 0 && len(d.Package) > 0
}

View File

@@ -25,6 +25,7 @@ service {{.serviceName}} {
}
`
// ProtoTmpl returns an sample of a proto file
func ProtoTmpl(out string) error {
protoFilename := filepath.Base(out)
serviceName := stringx.From(strings.TrimSuffix(protoFilename, filepath.Ext(protoFilename)))

View File

@@ -38,10 +38,13 @@ var templates = map[string]string{
rpcTemplateFile: rpcTemplateText,
}
// GenTemplates is the entry for command goctl template,
// it will create the specified category
func GenTemplates(_ *cli.Context) error {
return util.InitTemplates(category, templates)
}
// RevertTemplate restores the deleted template files
func RevertTemplate(name string) error {
content, ok := templates[name]
if !ok {
@@ -50,10 +53,13 @@ func RevertTemplate(name string) error {
return util.CreateTemplate(category, name, content)
}
// Clean deletes all template files
func Clean() error {
return util.Clean(category)
}
// Update is used to update the template files, it will delete the existing old templates at first,
// and then create the latest template files
func Update() error {
err := Clean()
if err != nil {
@@ -63,6 +69,7 @@ func Update() error {
return util.InitTemplates(category, templates)
}
// Category returns a const string value for rpc template category
func Category() string {
return category
}

View File

@@ -2,6 +2,7 @@ package parser
import "github.com/emicklei/proto"
// GetComment returns content with prefix //
func GetComment(comment *proto.Comment) string {
if comment == nil {
return ""

View File

@@ -2,6 +2,7 @@ package parser
import "github.com/emicklei/proto"
// Import embeds proto.Import
type Import struct {
*proto.Import
}

View File

@@ -1,7 +1,8 @@
package parser
import pr "github.com/emicklei/proto"
import "github.com/emicklei/proto"
// Message embeds proto.Message
type Message struct {
*pr.Message
*proto.Message
}

View File

@@ -2,6 +2,7 @@ package parser
import "github.com/emicklei/proto"
// Option embeds proto.Option
type Option struct {
*proto.Option
}

View File

@@ -14,14 +14,18 @@ import (
)
type (
defaultProtoParser struct{}
// DefaultProtoParser types a empty struct
DefaultProtoParser struct{}
)
func NewDefaultProtoParser() *defaultProtoParser {
return &defaultProtoParser{}
// NewDefaultProtoParser creates a new instance
func NewDefaultProtoParser() *DefaultProtoParser {
return &DefaultProtoParser{}
}
func (p *defaultProtoParser) Parse(src string) (Proto, error) {
// Parse provides to parse the proto file into a golang structure,
// which is convenient for subsequent rpc generation and use
func (p *DefaultProtoParser) Parse(src string) (Proto, error) {
var ret Proto
abs, err := filepath.Abs(src)
@@ -101,7 +105,7 @@ func (p *defaultProtoParser) Parse(src string) (Proto, error) {
return ret, nil
}
// see google.golang.org/protobuf@v1.25.0/internal/strs/strings.go:71
// GoSanitized copy from protobuf, for more information, please see google.golang.org/protobuf@v1.25.0/internal/strs/strings.go:71
func GoSanitized(s string) string {
// Sanitize the input to the set of valid characters,
// which must be '_' or be in the Unicode L or N categories.
@@ -121,7 +125,7 @@ func GoSanitized(s string) string {
return s
}
// copy from github.com/golang/protobuf@v1.4.2/protoc-gen-go/generator/generator.go:2648
// CamelCase copy from protobuf, for more information, please see github.com/golang/protobuf@v1.4.2/protoc-gen-go/generator/generator.go:2648
func CamelCase(s string) string {
if s == "" {
return ""

View File

@@ -1,5 +1,6 @@
package parser
// Proto describes a proto file,
type Proto struct {
Src string
Name string

View File

@@ -2,6 +2,7 @@ package parser
import "github.com/emicklei/proto"
// RPC embeds proto.RPC
type RPC struct {
*proto.RPC
}

View File

@@ -2,6 +2,8 @@ package parser
import "github.com/emicklei/proto"
// Service describes the rpc service, which is the relevant
// content after the translation of the proto file
type Service struct {
*proto.Service
RPC []*RPC