Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: goctl genhandler duplicate rest/httpx & template import custom #2152

Merged
merged 1 commit into from Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 20 additions & 19 deletions tools/goctl/api/gogen/genhandlers.go
Expand Up @@ -21,15 +21,16 @@ const defaultLogicPackage = "logic"
var handlerTemplate string

type handlerInfo struct {
PkgName string
ImportPackages string
HandlerName string
RequestType string
LogicName string
LogicType string
Call string
HasResp bool
HasRequest bool
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 {
Expand All @@ -47,15 +48,16 @@ func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route
}

return doGenToFile(dir, handler, cfg, group, route, handlerInfo{
PkgName: pkgName,
ImportPackages: genHandlerImports(group, route, parentPkg),
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,
PkgName: pkgName,
ImportPackages: genHandlerImports(group, route, parentPkg),
ImportHttpxPackage: fmt.Sprintf("\"%s/rest/httpx\"", vars.ProjectOpenSourceURL),
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,
})
}

Expand Down Expand Up @@ -99,7 +101,6 @@ func genHandlerImports(group spec.Group, route spec.Route, parentPkg string) str
if len(route.RequestTypeName()) > 0 {
imports = append(imports, fmt.Sprintf("\"%s\"\n", pathx.JoinPackages(parentPkg, typesDir)))
}
imports = append(imports, fmt.Sprintf("\"%s/rest/httpx\"", vars.ProjectOpenSourceURL))

return strings.Join(imports, "\n\t")
}
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/gogen/handler.tpl
Expand Up @@ -3,8 +3,8 @@ package {{.PkgName}}
import (
"net/http"

"github.com/zeromicro/go-zero/rest/httpx"
{{.ImportPackages}}
{{.ImportHttpxPackage}}
)

func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
Expand Down