support api templates
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/templatex"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||
)
|
||||
|
||||
@@ -47,7 +48,12 @@ func genConfig(dir string, api *spec.ApiSpec) error {
|
||||
}
|
||||
|
||||
var authImportStr = fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceUrl)
|
||||
t := template.Must(template.New("configTemplate").Parse(configTemplate))
|
||||
text, err := templatex.LoadTemplate(category, configTemplateFile, configTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t := template.Must(template.New("configTemplate").Parse(text))
|
||||
buffer := new(bytes.Buffer)
|
||||
err = t.Execute(buffer, map[string]string{
|
||||
"authImport": authImportStr,
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/templatex"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -39,7 +40,12 @@ func genEtc(dir string, api *spec.ApiSpec) error {
|
||||
port = strconv.Itoa(defaultPort)
|
||||
}
|
||||
|
||||
t := template.Must(template.New("etcTemplate").Parse(etcTemplate))
|
||||
text, err := templatex.LoadTemplate(category, etcTemplateFile, etcTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t := template.Must(template.New("etcTemplate").Parse(text))
|
||||
buffer := new(bytes.Buffer)
|
||||
err = t.Execute(buffer, map[string]string{
|
||||
"serviceName": service.Name,
|
||||
|
||||
@@ -9,108 +9,54 @@ import (
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/templatex"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||
)
|
||||
|
||||
const (
|
||||
handlerTemplate = `package handler
|
||||
const handlerTemplate = `package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
{{.importPackages}}
|
||||
{{.ImportPackages}}
|
||||
)
|
||||
|
||||
func {{.handlerName}}(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
func {{.HandlerName}}(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
{{.handlerBody}}
|
||||
}
|
||||
}
|
||||
`
|
||||
handlerBodyTemplate = `{{.parseRequest}}
|
||||
{{.processBody}}
|
||||
`
|
||||
parseRequestTemplate = `var req {{.requestType}}
|
||||
var req types.{{.RequestType}}
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.Error(w, err)
|
||||
return
|
||||
}
|
||||
`
|
||||
hasRespTemplate = `
|
||||
l := logic.{{.logic}}(r.Context(), ctx)
|
||||
{{.logicResponse}} l.{{.callee}}({{.req}})
|
||||
|
||||
l := logic.New{{.LogicType}}(r.Context(), ctx)
|
||||
{{if .HasResp}}resp, {{end}}err := l.{{.Call}}(req)
|
||||
if err != nil {
|
||||
httpx.Error(w, err)
|
||||
} else {
|
||||
{{.respWriter}}
|
||||
{{if .HasResp}}httpx.OkJson(w, resp){{else}}httpx.Ok(w){{end}}
|
||||
}
|
||||
`
|
||||
)
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
type Handler struct {
|
||||
ImportPackages string
|
||||
HandlerName string
|
||||
RequestType string
|
||||
LogicType string
|
||||
Call string
|
||||
HasResp bool
|
||||
}
|
||||
|
||||
func genHandler(dir string, group spec.Group, route spec.Route) error {
|
||||
handler, ok := apiutil.GetAnnotationValue(route.Annotations, "server", "handler")
|
||||
if !ok {
|
||||
return fmt.Errorf("missing handler annotation for %q", route.Path)
|
||||
}
|
||||
|
||||
handler = getHandlerName(handler)
|
||||
var reqBody string
|
||||
if len(route.RequestType.Name) > 0 {
|
||||
var bodyBuilder strings.Builder
|
||||
t := template.Must(template.New("parseRequest").Parse(parseRequestTemplate))
|
||||
if err := t.Execute(&bodyBuilder, map[string]string{
|
||||
"requestType": typesPacket + "." + util.Title(route.RequestType.Name),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
reqBody = bodyBuilder.String()
|
||||
}
|
||||
|
||||
var req = "req"
|
||||
if len(route.RequestType.Name) == 0 {
|
||||
req = ""
|
||||
}
|
||||
var logicResponse string
|
||||
var writeResponse string
|
||||
var respWriter = `httpx.WriteJson(w, http.StatusOK, resp)`
|
||||
if len(route.ResponseType.Name) > 0 {
|
||||
logicResponse = "resp, err :="
|
||||
writeResponse = "resp, err"
|
||||
} else {
|
||||
logicResponse = "err :="
|
||||
writeResponse = "nil, err"
|
||||
respWriter = `httpx.Ok(w)`
|
||||
}
|
||||
var logicBodyBuilder strings.Builder
|
||||
t := template.Must(template.New("hasRespTemplate").Parse(hasRespTemplate))
|
||||
if err := t.Execute(&logicBodyBuilder, map[string]string{
|
||||
"logic": "New" + strings.TrimSuffix(strings.Title(handler), "Handler") + "Logic",
|
||||
"callee": strings.Title(strings.TrimSuffix(handler, "Handler")),
|
||||
"req": req,
|
||||
"logicResponse": logicResponse,
|
||||
"writeResponse": writeResponse,
|
||||
"respWriter": respWriter,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
respBody := logicBodyBuilder.String()
|
||||
|
||||
if !strings.HasSuffix(handler, "Handler") {
|
||||
handler = handler + "Handler"
|
||||
}
|
||||
|
||||
var bodyBuilder strings.Builder
|
||||
bodyTemplate := template.Must(template.New("handlerBodyTemplate").Parse(handlerBodyTemplate))
|
||||
if err := bodyTemplate.Execute(&bodyBuilder, map[string]string{
|
||||
"parseRequest": reqBody,
|
||||
"processBody": respBody,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return doGenToFile(dir, handler, group, route, bodyBuilder)
|
||||
}
|
||||
|
||||
func doGenToFile(dir, handler string, group spec.Group, route spec.Route, bodyBuilder strings.Builder) error {
|
||||
if getHandlerFolderPath(group, route) != handlerDir {
|
||||
handler = strings.Title(handler)
|
||||
}
|
||||
@@ -118,6 +64,21 @@ func doGenToFile(dir, handler string, group spec.Group, route spec.Route, bodyBu
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return doGenToFile(dir, handler, group, route, Handler{
|
||||
ImportPackages: genHandlerImports(group, route, parentPkg),
|
||||
HandlerName: handler,
|
||||
RequestType: util.Title(route.RequestType.Name),
|
||||
LogicType: strings.TrimSuffix(strings.Title(handler), "Handler") + "Logic",
|
||||
Call: strings.Title(strings.TrimSuffix(handler, "Handler")),
|
||||
HasResp: len(route.ResponseType.Name) > 0,
|
||||
})
|
||||
}
|
||||
|
||||
func doGenToFile(dir, handler string, group spec.Group, route spec.Route, handleObj Handler) error {
|
||||
if getHandlerFolderPath(group, route) != handlerDir {
|
||||
handler = strings.Title(handler)
|
||||
}
|
||||
filename := strings.ToLower(handler)
|
||||
if strings.HasSuffix(filename, "handler") {
|
||||
filename = filename + ".go"
|
||||
@@ -132,16 +93,18 @@ func doGenToFile(dir, handler string, group spec.Group, route spec.Route, bodyBu
|
||||
return nil
|
||||
}
|
||||
defer fp.Close()
|
||||
t := template.Must(template.New("handlerTemplate").Parse(handlerTemplate))
|
||||
|
||||
text, err := templatex.LoadTemplate(category, handlerTemplateFile, handlerTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
buffer := new(bytes.Buffer)
|
||||
err = t.Execute(buffer, map[string]string{
|
||||
"importPackages": genHandlerImports(group, route, parentPkg),
|
||||
"handlerName": handler,
|
||||
"handlerBody": strings.TrimSpace(bodyBuilder.String()),
|
||||
})
|
||||
err = template.Must(template.New("handlerTemplate").Parse(text)).Execute(buffer, handleObj)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
formatCode := formatCode(buffer.String())
|
||||
_, err = fp.WriteString(formatCode)
|
||||
return err
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/templatex"
|
||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||
)
|
||||
@@ -93,7 +94,12 @@ func genLogicByRoute(dir string, group spec.Group, route spec.Route) error {
|
||||
requestString = "req " + "types." + strings.Title(route.RequestType.Name)
|
||||
}
|
||||
|
||||
t := template.Must(template.New("logicTemplate").Parse(logicTemplate))
|
||||
text, err := templatex.LoadTemplate(category, logicTemplateFile, logicTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t := template.Must(template.New("logicTemplate").Parse(text))
|
||||
buffer := new(bytes.Buffer)
|
||||
err = t.Execute(fp, map[string]string{
|
||||
"imports": imports,
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/templatex"
|
||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||
)
|
||||
@@ -60,7 +61,12 @@ func genMain(dir string, api *spec.ApiSpec) error {
|
||||
return err
|
||||
}
|
||||
|
||||
t := template.Must(template.New("mainTemplate").Parse(mainTemplate))
|
||||
text, err := templatex.LoadTemplate(category, mainTemplateFile, mainTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t := template.Must(template.New("mainTemplate").Parse(text))
|
||||
buffer := new(bytes.Buffer)
|
||||
err = t.Execute(buffer, map[string]string{
|
||||
"importPackages": genMainImports(parentPkg),
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/templatex"
|
||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
)
|
||||
|
||||
@@ -46,8 +47,14 @@ func genServiceContext(dir string, api *spec.ApiSpec) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
text, err := templatex.LoadTemplate(category, contextTemplateFile, contextTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var configImport = "\"" + ctlutil.JoinPackages(parentPkg, configDir) + "\""
|
||||
t := template.Must(template.New("contextTemplate").Parse(contextTemplate))
|
||||
t := template.Must(template.New("contextTemplate").Parse(text))
|
||||
buffer := new(bytes.Buffer)
|
||||
err = t.Execute(buffer, map[string]string{
|
||||
"configImport": configImport,
|
||||
|
||||
29
tools/goctl/api/gogen/template.go
Normal file
29
tools/goctl/api/gogen/template.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package gogen
|
||||
|
||||
import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/templatex"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
const (
|
||||
category = "api"
|
||||
configTemplateFile = "config.tpl"
|
||||
contextTemplateFile = "context.tpl"
|
||||
etcTemplateFile = "etc.tpl"
|
||||
handlerTemplateFile = "handler.tpl"
|
||||
logicTemplateFile = "logic.tpl"
|
||||
mainTemplateFile = "main.tpl"
|
||||
)
|
||||
|
||||
var templates = map[string]string{
|
||||
configTemplateFile: configTemplate,
|
||||
contextTemplateFile: contextTemplate,
|
||||
etcTemplateFile: etcTemplate,
|
||||
handlerTemplateFile: handlerTemplate,
|
||||
logicTemplateFile: logicTemplate,
|
||||
mainTemplateFile: mainTemplate,
|
||||
}
|
||||
|
||||
func GenTemplates(_ *cli.Context) error {
|
||||
return templatex.InitTemplates(category, templates)
|
||||
}
|
||||
Reference in New Issue
Block a user