1 Star 0 Fork 0

carlmax_my/console-core-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 3.20 KB
一键复制 编辑 原始数据 按行查看 历史
carlmax_my 提交于 2024-12-02 21:32 . init project
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
})
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/carlmax_my/console-core-go.git
git@gitee.com:carlmax_my/console-core-go.git
carlmax_my
console-core-go
console-core-go
v0.0.19

搜索帮助

0d507c66 1850385 C8b1a773 1850385