feat: Support for multiple rpc service generation and rpc grouping (#1972)
* Add group & compatible flag * Add group & compatible flag * Support for multiple rpc service generation and rpc grouping * Support for multiple rpc service generation and rpc grouping * Format code * Format code * Add comments * Fix unit test * Refactor function name * Add example & Update grpc readme * go mod tidy * update mod * update mod
This commit is contained in:
@@ -11,14 +11,20 @@ import (
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util/format"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util/stringx"
|
||||
)
|
||||
|
||||
//go:embed main.tpl
|
||||
var mainTemplate string
|
||||
|
||||
type MainServiceTemplateData struct {
|
||||
Service string
|
||||
ServerPkg string
|
||||
Pkg string
|
||||
}
|
||||
|
||||
// GenMain generates the main file of the rpc service, which is an rpc service program call entry
|
||||
func (g *Generator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
|
||||
func (g *Generator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf.Config,
|
||||
c *ZRpcContext) error {
|
||||
mainFilename, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetServiceName().Source())
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -28,9 +34,35 @@ func (g *Generator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf.Config
|
||||
imports := make([]string, 0)
|
||||
pbImport := fmt.Sprintf(`"%v"`, ctx.GetPb().Package)
|
||||
svcImport := fmt.Sprintf(`"%v"`, ctx.GetSvc().Package)
|
||||
remoteImport := fmt.Sprintf(`"%v"`, ctx.GetServer().Package)
|
||||
configImport := fmt.Sprintf(`"%v"`, ctx.GetConfig().Package)
|
||||
imports = append(imports, configImport, pbImport, remoteImport, svcImport)
|
||||
imports = append(imports, configImport, pbImport, svcImport)
|
||||
|
||||
var serviceNames []MainServiceTemplateData
|
||||
for _, e := range proto.Service {
|
||||
var (
|
||||
remoteImport string
|
||||
serverPkg string
|
||||
)
|
||||
if !c.Multiple {
|
||||
serverPkg = "server"
|
||||
remoteImport = fmt.Sprintf(`"%v"`, ctx.GetServer().Package)
|
||||
} else {
|
||||
childPkg, err := ctx.GetServer().GetChildPackage(e.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
serverPkg = filepath.Base(childPkg + "Server")
|
||||
remoteImport = fmt.Sprintf(`%s "%v"`, serverPkg, childPkg)
|
||||
}
|
||||
imports = append(imports, remoteImport)
|
||||
serviceNames = append(serviceNames, MainServiceTemplateData{
|
||||
Service: parser.CamelCase(e.Name),
|
||||
ServerPkg: serverPkg,
|
||||
Pkg: proto.PbPackage,
|
||||
})
|
||||
}
|
||||
|
||||
text, err := pathx.LoadTemplate(category, mainTemplateFile, mainTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -42,10 +74,9 @@ func (g *Generator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf.Config
|
||||
}
|
||||
|
||||
return util.With("main").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
|
||||
"serviceName": etcFileName,
|
||||
"imports": strings.Join(imports, pathx.NL),
|
||||
"pkg": proto.PbPackage,
|
||||
"serviceNew": stringx.From(proto.Service.Name).ToCamel(),
|
||||
"service": parser.CamelCase(proto.Service.Name),
|
||||
"serviceName": etcFileName,
|
||||
"imports": strings.Join(imports, pathx.NL),
|
||||
"pkg": proto.PbPackage,
|
||||
"serviceNames": serviceNames,
|
||||
}, fileName, false)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user