forked from cloudwego/hertz
Merge pull request #116 from FGYFFFF/feat/update_for_template
feat: update for tempalte
This commit is contained in:
commit
583c948003
|
@ -111,7 +111,7 @@ func (pkgGen *HttpPackageGenerator) updateHandler(handler interface{}, handlerTp
|
|||
return err
|
||||
}
|
||||
|
||||
hertzImport := regexp.MustCompile(`"github.com/cloudwego/hertz/pkg/app"\n`)
|
||||
hertzImport := regexp.MustCompile(`import \(\n`)
|
||||
// insert new imports
|
||||
for alias, model := range handler.(Handler).Imports {
|
||||
if bytes.Contains(file, []byte(model.Package)) {
|
||||
|
@ -120,7 +120,7 @@ func (pkgGen *HttpPackageGenerator) updateHandler(handler interface{}, handlerTp
|
|||
|
||||
subIndexImport := hertzImport.FindSubmatchIndex(file)
|
||||
if len(subIndexImport) != 2 || subIndexImport[0] < 1 {
|
||||
return fmt.Errorf("\"github.com/cloudwego/hertz/pkg/app\" not found in %s", string(file))
|
||||
return fmt.Errorf("\"import (\" not found in %s", string(file))
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(nil)
|
||||
|
@ -132,35 +132,36 @@ func (pkgGen *HttpPackageGenerator) updateHandler(handler interface{}, handlerTp
|
|||
|
||||
// insert new handler
|
||||
for _, method := range handler.(Handler).Methods {
|
||||
if bytes.Contains(file, []byte(fmt.Sprintf("func %s(ctx context.Context, c *app.RequestContext)", method.Name))) {
|
||||
if bytes.Contains(file, []byte(fmt.Sprintf("func %s(", method.Name))) {
|
||||
continue
|
||||
}
|
||||
|
||||
handlerFunc := fmt.Sprintf("%s\n"+
|
||||
"func %s(ctx context.Context, c *app.RequestContext) { \n"+
|
||||
"\tvar err error\n"+
|
||||
"\tvar req %s\n"+
|
||||
"\terr = c.BindAndValidate(&req)\n"+
|
||||
"\tif err != nil {\n"+
|
||||
"\t\tc.String(400, err.Error())\n"+
|
||||
"\t\treturn\n"+
|
||||
"\t}\n\n"+
|
||||
"\tresp := new(%s)\n\n"+
|
||||
"\tc.%s(200, resp)\n"+
|
||||
"}\n", method.Comment, method.Name, method.RequestTypeName, method.ReturnTypeName, method.Serializer)
|
||||
|
||||
if len(method.Name) == 0 {
|
||||
handlerFunc = fmt.Sprintf("%s\n"+
|
||||
"func %s(ctx context.Context, c *app.RequestContext) { \n"+
|
||||
"\tvar err error\n"+
|
||||
"\tresp := new(%s)\n\n"+
|
||||
"\tc.%s(200, resp)\n"+
|
||||
"}\n", method.Comment, method.Name, method.ReturnTypeName, method.Serializer)
|
||||
// Generate additional handlers using templates
|
||||
handlerSingleTpl := pkgGen.tpls[handlerSingleTplName]
|
||||
if handlerSingleTpl == nil {
|
||||
return fmt.Errorf("tpl %s not found", handlerSingleTplName)
|
||||
}
|
||||
data := make(map[string]string, 5)
|
||||
data["Comment"] = method.Comment
|
||||
data["Name"] = method.Name
|
||||
data["RequestTypeName"] = method.RequestTypeName
|
||||
data["ReturnTypeName"] = method.ReturnTypeName
|
||||
data["Serializer"] = method.Serializer
|
||||
handlerFunc := bytes.NewBuffer(nil)
|
||||
err = handlerSingleTpl.Execute(handlerFunc, data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("execute template \"%s\" failed, %v", handlerSingleTplName, err)
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.Write(file)
|
||||
buf.Write([]byte(handlerFunc))
|
||||
_, err = buf.Write(file)
|
||||
if err != nil {
|
||||
return fmt.Errorf("write handler \"%s\" failed, %v", method.Name, err)
|
||||
}
|
||||
_, err = buf.Write(handlerFunc.Bytes())
|
||||
if err != nil {
|
||||
return fmt.Errorf("write handler \"%s\" failed, %v", method.Name, err)
|
||||
}
|
||||
file = buf.Bytes()
|
||||
}
|
||||
|
||||
|
|
|
@ -127,7 +127,7 @@ dumped_hertz_remote_config.json
|
|||
Delims: [2]string{"{{", "}}"},
|
||||
Body: `// Code generated by hz. DO NOT EDIT.
|
||||
|
||||
hz version: v{{.hzVersion}}`,
|
||||
hz version: {{.hzVersion}}`,
|
||||
},
|
||||
{
|
||||
Path: defaultHandlerDir + sp + "ping.go",
|
||||
|
|
|
@ -17,20 +17,36 @@
|
|||
package generator
|
||||
|
||||
var (
|
||||
routerTplName = "router.go"
|
||||
middlewareTplName = "middleware.go"
|
||||
handlerTplName = "handler.go"
|
||||
modelTplName = "model.go"
|
||||
registerTplName = "register.go"
|
||||
clientTplName = "client.go"
|
||||
routerTplName = "router.go"
|
||||
middlewareTplName = "middleware.go"
|
||||
middlewareSingleTplName = "middleware_single.go"
|
||||
handlerTplName = "handler.go"
|
||||
handlerSingleTplName = "handler_single.go"
|
||||
modelTplName = "model.go"
|
||||
registerTplName = "register.go"
|
||||
clientTplName = "client.go"
|
||||
|
||||
insertPointNew = "//INSERT_POINT: DO NOT DELETE THIS LINE!"
|
||||
insertPointPatternNew = `//INSERT_POINT\: DO NOT DELETE THIS LINE\!`
|
||||
insertPointPatternOld = `func register\(r \*(hertz|server)\.Hertz\) \{\n`
|
||||
)
|
||||
|
||||
var templateNameSet = map[string]string{
|
||||
routerTplName: routerTplName,
|
||||
middlewareTplName: middlewareTplName,
|
||||
middlewareSingleTplName: middlewareSingleTplName,
|
||||
handlerTplName: handlerTplName,
|
||||
handlerSingleTplName: handlerSingleTplName,
|
||||
modelTplName: modelTplName,
|
||||
registerTplName: registerTplName,
|
||||
clientTplName: clientTplName,
|
||||
}
|
||||
|
||||
func IsDefaultTpl(name string) bool {
|
||||
return name == routerTplName || name == handlerTplName || name == modelTplName || name == registerTplName || name == middlewareTplName || name == clientTplName
|
||||
if _, exist := templateNameSet[name]; exist {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
var defaultPkgConfig = TemplateConfig{
|
||||
|
@ -193,5 +209,36 @@ func New{{.ServiceName}}Client(opt ...config.ClientOption) (*{{.ServiceName}}Cli
|
|||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
Path: defaultHandlerDir + sp + handlerSingleTplName,
|
||||
Delims: [2]string{"{{", "}}"},
|
||||
Body: `
|
||||
{{.Comment}}
|
||||
func {{.Name}}(ctx context.Context, c *app.RequestContext) {
|
||||
var err error
|
||||
{{if ne .RequestTypeName "" -}}
|
||||
var req {{.RequestTypeName}}
|
||||
err = c.BindAndValidate(&req)
|
||||
if err != nil {
|
||||
c.String(400, err.Error())
|
||||
return
|
||||
}
|
||||
{{end}}
|
||||
resp := new({{.ReturnTypeName}})
|
||||
|
||||
c.{{.Serializer}}(200, resp)
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
Path: defaultRouterDir + sp + middlewareSingleTplName,
|
||||
Delims: [2]string{"{{", "}}"},
|
||||
Body: `
|
||||
func {{.MiddleWare}}Mw() []app.HandlerFunc {
|
||||
// your code...
|
||||
return nil
|
||||
}
|
||||
`,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
|
@ -246,7 +246,7 @@ func (pkgGen *HttpPackageGenerator) updateRegister(pkg, rDir, pkgName string) er
|
|||
}
|
||||
|
||||
insertImport := register.PkgAlias + " " + "\"" + register.Pkg + "\"\n"
|
||||
if !bytes.Contains(file, []byte(insertImport)) {
|
||||
if !bytes.Contains(file, []byte(register.Pkg)) {
|
||||
|
||||
subIndexImport := regImport.FindSubmatchIndex(file)
|
||||
if len(subIndexImport) != 2 || subIndexImport[0] < 1 {
|
||||
|
@ -267,7 +267,7 @@ func (pkgGen *HttpPackageGenerator) updateRegister(pkg, rDir, pkgName string) er
|
|||
|
||||
subIndexReg := regRegisterV3.FindSubmatchIndex(file)
|
||||
if len(subIndexReg) != 2 || subIndexReg[0] < 1 {
|
||||
return fmt.Errorf("wrong format %s: insert-point '%s' not found", string(file), insertPointPatternOld)
|
||||
return fmt.Errorf("wrong format %s: insert-point '%s' not found", string(file), insertPointPatternNew)
|
||||
}
|
||||
|
||||
bufReg := bytes.NewBuffer(nil)
|
||||
|
@ -331,9 +331,27 @@ func (pkgGen *HttpPackageGenerator) updateMiddlewareReg(router interface{}, midd
|
|||
if bytes.Contains(file, []byte(mw+"Mw")) {
|
||||
continue
|
||||
}
|
||||
middlewareSingleTpl := pkgGen.tpls[middlewareSingleTplName]
|
||||
if middlewareSingleTpl == nil {
|
||||
return fmt.Errorf("tpl %s not found", middlewareSingleTplName)
|
||||
}
|
||||
data := make(map[string]string, 1)
|
||||
data["MiddleWare"] = mw
|
||||
middlewareFunc := bytes.NewBuffer(nil)
|
||||
err = middlewareSingleTpl.Execute(middlewareFunc, data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("execute template \"%s\" failed, %v", middlewareSingleTplName, err)
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(nil)
|
||||
buf.Write(file)
|
||||
buf.Write([]byte("func " + mw + "Mw() []app.HandlerFunc {\n\t// your code...\n\treturn nil\n}\n"))
|
||||
_, err = buf.Write(file)
|
||||
if err != nil {
|
||||
return fmt.Errorf("write middleware \"%s\" failed, %v", mw, err)
|
||||
}
|
||||
_, err = buf.Write(middlewareFunc.Bytes())
|
||||
if err != nil {
|
||||
return fmt.Errorf("write middleware \"%s\" failed, %v", mw, err)
|
||||
}
|
||||
file = buf.Bytes()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue