feat: add gen api @doc comment to logic handler routes (#3790)

Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
This commit is contained in:
chentong
2024-03-30 19:09:54 +08:00
committed by GitHub
parent cf987295df
commit 8c0bb27136
7 changed files with 54 additions and 41 deletions

View File

@@ -18,19 +18,6 @@ const defaultLogicPackage = "logic"
//go:embed handler.tpl
var handlerTemplate string
type handlerInfo struct {
PkgName string
ImportPackages string
ImportHttpxPackage string
HandlerName string
RequestType string
LogicName string
LogicType string
Call string
HasResp bool
HasRequest bool
}
func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route spec.Route) error {
handler := getHandlerName(route)
handlerPath := getHandlerFolderPath(group, route)
@@ -40,23 +27,6 @@ func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route
handler = strings.Title(handler)
logicName = pkgName
}
return doGenToFile(dir, handler, cfg, group, route, handlerInfo{
PkgName: pkgName,
ImportPackages: genHandlerImports(group, route, rootPkg),
HandlerName: handler,
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,
})
}
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
@@ -70,7 +40,19 @@ func doGenToFile(dir, handler string, cfg *config.Config, group spec.Group,
category: category,
templateFile: handlerTemplateFile,
builtinTemplate: handlerTemplate,
data: handleObj,
data: map[string]any{
"PkgName": pkgName,
"ImportPackages": genHandlerImports(group, route, rootPkg),
"HandlerName": handler,
"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,
"HasDoc": len(route.JoinedDoc()) > 0,
"Doc": getDoc(route.JoinedDoc()),
},
})
}