123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package generator
- import (
- _ "embed"
- "fmt"
- "path/filepath"
- "strings"
- "github.com/zeromicro/go-zero/core/collection"
- conf "github.com/zeromicro/go-zero/tools/goctl/config"
- "github.com/zeromicro/go-zero/tools/goctl/rpc/parser"
- "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"
- )
- const logicFunctionTemplate = `{{if .hasComment}}{{.comment}}{{end}}
- func (l *{{.logicName}}) {{.method}} ({{if .hasReq}}in {{.request}}{{if .stream}},stream {{.streamBody}}{{end}}{{else}}stream {{.streamBody}}{{end}}) ({{if .hasReply}}{{.response}},{{end}} error) {
- // todo: add your logic here and delete this line
-
- return {{if .hasReply}}&{{.responseType}}{},{{end}} nil
- }
- `
- //go:embed logic.tpl
- 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 {
- dir := ctx.GetLogic()
- service := proto.Service.Service.Name
- for _, rpc := range proto.Service.RPC {
- 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)
- 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
- }
- 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),
- }, filename, false)
- if err != nil {
- return err
- }
- }
- return nil
- }
- func (g *Generator) genLogicFunction(serviceName, goPackage 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")
- buffer, err := util.With("fun").Parse(text).Execute(map[string]interface{}{
- "logicName": logicName,
- "method": parser.CamelCase(rpc.Name),
- "hasReq": !rpc.StreamsRequest,
- "request": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.RequestType)),
- "hasReply": !rpc.StreamsRequest && !rpc.StreamsReturns,
- "response": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.ReturnsType)),
- "responseType": fmt.Sprintf("%s.%s", goPackage, parser.CamelCase(rpc.ReturnsType)),
- "stream": rpc.StreamsRequest || rpc.StreamsReturns,
- "streamBody": streamServer,
- "hasComment": len(comment) > 0,
- "comment": comment,
- })
- if err != nil {
- return "", err
- }
- functions = append(functions, buffer.String())
- return strings.Join(functions, pathx.NL), nil
- }
|