代码拉取完成,页面将自动刷新
package main
import (
"flag"
"fmt"
"go/token"
"log"
"os"
"strings"
"gitee.com/carlmax_my/console-core-go/pkg/util"
"github.com/dave/dst"
"github.com/dave/dst/decorator"
)
var handlerName string
func init() {
handler := flag.String("handler", "", "请输入需要生成的 handler 名称\n")
flag.Parse()
handlerName = strings.ToLower(*handler)
}
func main() {
fs := token.NewFileSet()
filePath := fmt.Sprintf("./internal/api/%s", handlerName)
parsedFile, err := decorator.ParseFile(fs, filePath+"/handler.go", nil, 0)
if err != nil {
log.Fatalf("parsing package: %s: %s\n", filePath, err)
}
files, _ := os.ReadDir(filePath)
if len(files) > 1 {
log.Fatalf("Please ensure in dir %s, have and only have handler.go one file", filePath)
}
dst.Inspect(parsedFile, func(n dst.Node) bool {
decl, ok := n.(*dst.GenDecl)
if !ok || decl.Tok != token.TYPE {
return true
}
for _, spec := range decl.Specs {
typeSpec, _ok := spec.(*dst.TypeSpec)
if !_ok {
continue
}
var interfaceType *dst.InterfaceType
if interfaceType, ok = typeSpec.Type.(*dst.InterfaceType); !ok {
continue
}
for _, v := range interfaceType.Methods.List {
if len(v.Names) > 0 {
if v.Names[0].String() == "i" {
continue
}
filepath := "./internal/api/" + handlerName
filename := fmt.Sprintf("%s/func_%s.go", filepath, strings.ToLower(v.Names[0].String()))
funcFile, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0766)
if err != nil {
log.Printf("create and open func file error %v\n", err.Error())
continue
}
if funcFile == nil {
log.Printf("func file is nil \n")
continue
}
log.Println(" └── file : ", filename)
rawName := v.Names[0].String()
structName := util.ToLowerFirst(rawName)
funcContent := fmt.Sprintf("package %s\n\n", handlerName)
funcContent += "import (\n"
funcContent += `"gitee.com/carlmax_my/console-core-go/pkg/core"`
funcContent += "\n)\n\n"
funcContent += fmt.Sprintf("\n\ntype %sRequest struct {}\n\n", structName)
funcContent += fmt.Sprintf("type %sResponse struct {}\n\n", structName)
// 首行注释
funcContent += fmt.Sprintf("%s\n", v.Decorations().Start.All()[0])
nameArr := strings.Split(v.Decorations().Start.All()[0], rawName)
funcContent += fmt.Sprintf("// @Summary%s \n", nameArr[1])
funcContent += fmt.Sprintf("// @Description%s \n", nameArr[1])
// Tags
funcContent += fmt.Sprintf("%s \n", v.Decorations().Start.All()[1])
funcContent += fmt.Sprintf("// @Accept json \n")
funcContent += fmt.Sprintf("// @Produce json \n")
funcContent += fmt.Sprintf("// @Param Request body %sRequest true \"RequestInfo\" \n", structName)
funcContent += fmt.Sprintf("// @Success 200 {object} %sResponse \n", structName)
funcContent += fmt.Sprintf("// @Failure 400 {object} api.Failure \n")
// Router
funcContent += fmt.Sprintf("%s \n", v.Decorations().Start.All()[2])
funcContent += fmt.Sprintf("func (h *handler) %s() core.HandlerFunc { \n return func(ctx core.Context) {\n\n}}", v.Names[0].String())
funcFile.WriteString(funcContent)
funcFile.Close()
}
}
}
return true
})
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。