You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/tools/goctl/api/gogen/genhandlers.go

180 lines
5.1 KiB
Go

4 years ago
package gogen
import (
"fmt"
"path"
"strings"
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
"github.com/tal-tech/go-zero/tools/goctl/config"
"github.com/tal-tech/go-zero/tools/goctl/internal/version"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/format"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/tal-tech/go-zero/tools/goctl/vars"
4 years ago
)
const (
defaultLogicPackage = "logic"
handlerTemplate = `package {{.PkgName}}
4 years ago
import (
"net/http"
{{if .After1_1_10}}"github.com/zeromicro/go-zero/rest/httpx"{{end}}
{{.ImportPackages}}
4 years ago
)
func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
4 years ago
return func(w http.ResponseWriter, r *http.Request) {
{{if .HasRequest}}var req types.{{.RequestType}}
4 years ago
if err := httpx.Parse(r, &req); err != nil {
httpx.Error(w, err)
4 years ago
return
}
{{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx)
{{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}req{{end}})
if err != nil {
httpx.Error(w, err)
} else {
{{if .HasResp}}httpx.OkJson(w, resp){{else}}httpx.Ok(w){{end}}
}
}
}
`
)
type handlerInfo struct {
PkgName string
ImportPackages string
HandlerName string
RequestType string
LogicName string
LogicType string
Call string
HasResp bool
HasRequest bool
After1_1_10 bool
}
4 years ago
func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route spec.Route) error {
handler := getHandlerName(route)
handlerPath := getHandlerFolderPath(group, route)
pkgName := handlerPath[strings.LastIndex(handlerPath, "/")+1:]
logicName := defaultLogicPackage
if handlerPath != handlerDir {
handler = strings.Title(handler)
logicName = pkgName
}
parentPkg, err := getParentPackage(dir)
if err != nil {
return err
4 years ago
}
goctlVersion := version.GetGoctlVersion()
// todo(anqiansong): This will be removed after a certain number of production versions of goctl (probably 5)
after1_1_10 := version.IsVersionGreaterThan(goctlVersion, "1.1.10")
return doGenToFile(dir, handler, cfg, group, route, handlerInfo{
PkgName: pkgName,
ImportPackages: genHandlerImports(group, route, parentPkg),
HandlerName: handler,
After1_1_10: after1_1_10,
RequestType: util.Title(route.RequestTypeName()),
LogicName: logicName,
LogicType: strings.Title(getLogicName(route)),
Call: strings.Title(strings.TrimSuffix(handler, "Handler")),
HasResp: len(route.ResponseTypeName()) > 0,
HasRequest: len(route.RequestTypeName()) > 0,
})
4 years ago
}
func doGenToFile(dir, handler string, cfg *config.Config, group spec.Group,
route spec.Route, handleObj handlerInfo) error {
filename, err := format.FileNamingFormat(cfg.NamingFormat, handler)
if err != nil {
return err
4 years ago
}
return genFile(fileGenConfig{
dir: dir,
subdir: getHandlerFolderPath(group, route),
filename: filename + ".go",
templateName: "handlerTemplate",
category: category,
templateFile: handlerTemplateFile,
builtinTemplate: handlerTemplate,
data: handleObj,
})
4 years ago
}
func genHandlers(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error {
4 years ago
for _, group := range api.Service.Groups {
for _, route := range group.Routes {
if err := genHandler(dir, rootPkg, cfg, group, route); err != nil {
4 years ago
return err
}
}
}
return nil
}
func genHandlerImports(group spec.Group, route spec.Route, parentPkg string) string {
var imports []string
imports = append(imports, fmt.Sprintf("\"%s\"",
pathx.JoinPackages(parentPkg, getLogicFolderPath(group, route))))
imports = append(imports, fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, contextDir)))
if len(route.RequestTypeName()) > 0 {
imports = append(imports, fmt.Sprintf("\"%s\"\n", pathx.JoinPackages(parentPkg, typesDir)))
4 years ago
}
currentVersion := version.GetGoctlVersion()
// todo(anqiansong): This will be removed after a certain number of production versions of goctl (probably 5)
if !version.IsVersionGreaterThan(currentVersion, "1.1.10") {
imports = append(imports, fmt.Sprintf("\"%s/rest/httpx\"", vars.ProjectOpenSourceURL))
}
4 years ago
return strings.Join(imports, "\n\t")
}
func getHandlerBaseName(route spec.Route) (string, error) {
handler := route.Handler
handler = strings.TrimSpace(handler)
handler = strings.TrimSuffix(handler, "handler")
handler = strings.TrimSuffix(handler, "Handler")
return handler, nil
4 years ago
}
func getHandlerFolderPath(group spec.Group, route spec.Route) string {
folder := route.GetAnnotation(groupProperty)
if len(folder) == 0 {
folder = group.GetAnnotation(groupProperty)
if len(folder) == 0 {
4 years ago
return handlerDir
}
}
4 years ago
folder = strings.TrimPrefix(folder, "/")
folder = strings.TrimSuffix(folder, "/")
return path.Join(handlerDir, folder)
}
func getHandlerName(route spec.Route) string {
handler, err := getHandlerBaseName(route)
if err != nil {
panic(err)
}
return handler + "Handler"
}
func getLogicName(route spec.Route) string {
handler, err := getHandlerBaseName(route)
if err != nil {
panic(err)
}
return handler + "Logic"
4 years ago
}