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:
@@ -27,17 +27,28 @@ func (l *{{.logicName}}) {{.method}} ({{if .hasReq}}in {{.request}}{{if .stream}
|
||||
var logicTemplate string
|
||||
|
||||
// GenLogic generates the logic file of the rpc service, which corresponds to the RPC definition items in proto.
|
||||
func (g *Generator) GenLogic(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
|
||||
func (g *Generator) GenLogic(ctx DirContext, proto parser.Proto, cfg *conf.Config,
|
||||
c *ZRpcContext) error {
|
||||
if !c.Multiple {
|
||||
return g.genLogicInCompatibility(ctx, proto, cfg)
|
||||
}
|
||||
|
||||
return g.genLogicGroup(ctx, proto, cfg)
|
||||
}
|
||||
|
||||
func (g *Generator) genLogicInCompatibility(ctx DirContext, proto parser.Proto,
|
||||
cfg *conf.Config) error {
|
||||
dir := ctx.GetLogic()
|
||||
service := proto.Service.Service.Name
|
||||
for _, rpc := range proto.Service.RPC {
|
||||
service := proto.Service[0].Service.Name
|
||||
for _, rpc := range proto.Service[0].RPC {
|
||||
logicName := fmt.Sprintf("%sLogic", stringx.From(rpc.Name).ToCamel())
|
||||
logicFilename, err := format.FileNamingFormat(cfg.NamingFormat, rpc.Name+"_logic")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
filename := filepath.Join(dir.Filename, logicFilename+".go")
|
||||
functions, err := g.genLogicFunction(service, proto.PbPackage, rpc)
|
||||
functions, err := g.genLogicFunction(service, proto.PbPackage, logicName, rpc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -50,9 +61,10 @@ func (g *Generator) GenLogic(ctx DirContext, proto parser.Proto, cfg *conf.Confi
|
||||
return err
|
||||
}
|
||||
err = util.With("logic").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
|
||||
"logicName": fmt.Sprintf("%sLogic", stringx.From(rpc.Name).ToCamel()),
|
||||
"functions": functions,
|
||||
"imports": strings.Join(imports.KeysStr(), pathx.NL),
|
||||
"logicName": fmt.Sprintf("%sLogic", stringx.From(rpc.Name).ToCamel()),
|
||||
"functions": functions,
|
||||
"packageName": "logic",
|
||||
"imports": strings.Join(imports.KeysStr(), pathx.NL),
|
||||
}, filename, false)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -61,16 +73,72 @@ func (g *Generator) GenLogic(ctx DirContext, proto parser.Proto, cfg *conf.Confi
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Generator) genLogicFunction(serviceName, goPackage string, rpc *parser.RPC) (string, error) {
|
||||
func (g *Generator) genLogicGroup(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
|
||||
dir := ctx.GetLogic()
|
||||
for _, item := range proto.Service {
|
||||
serviceName := item.Name
|
||||
for _, rpc := range item.RPC {
|
||||
var (
|
||||
err error
|
||||
filename string
|
||||
logicName string
|
||||
logicFilename string
|
||||
packageName string
|
||||
)
|
||||
|
||||
logicName = fmt.Sprintf("%sLogic", stringx.From(rpc.Name).ToCamel())
|
||||
childPkg, err := dir.GetChildPackage(serviceName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
serviceDir := filepath.Base(childPkg)
|
||||
nameJoin := fmt.Sprintf("%s_logic", serviceName)
|
||||
packageName = strings.ToLower(stringx.From(nameJoin).ToCamel())
|
||||
logicFilename, err = format.FileNamingFormat(cfg.NamingFormat, rpc.Name+"_logic")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
filename = filepath.Join(dir.Filename, serviceDir, logicFilename+".go")
|
||||
functions, err := g.genLogicFunction(serviceName, proto.PbPackage, logicName, rpc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
imports := collection.NewSet()
|
||||
imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetSvc().Package))
|
||||
imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetPb().Package))
|
||||
text, err := pathx.LoadTemplate(category, logicTemplateFileFile, logicTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = util.With("logic").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
|
||||
"logicName": logicName,
|
||||
"functions": functions,
|
||||
"packageName": packageName,
|
||||
"imports": strings.Join(imports.KeysStr(), pathx.NL),
|
||||
}, filename, false); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Generator) genLogicFunction(serviceName, goPackage, logicName string,
|
||||
rpc *parser.RPC) (string,
|
||||
error) {
|
||||
functions := make([]string, 0)
|
||||
text, err := pathx.LoadTemplate(category, logicFuncTemplateFileFile, logicFunctionTemplate)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
logicName := stringx.From(rpc.Name + "_logic").ToCamel()
|
||||
comment := parser.GetComment(rpc.Doc())
|
||||
streamServer := fmt.Sprintf("%s.%s_%s%s", goPackage, parser.CamelCase(serviceName), parser.CamelCase(rpc.Name), "Server")
|
||||
streamServer := fmt.Sprintf("%s.%s_%s%s", goPackage, parser.CamelCase(serviceName),
|
||||
parser.CamelCase(rpc.Name), "Server")
|
||||
buffer, err := util.With("fun").Parse(text).Execute(map[string]interface{}{
|
||||
"logicName": logicName,
|
||||
"method": parser.CamelCase(rpc.Name),
|
||||
|
||||
Reference in New Issue
Block a user