feat: 重构路由渲染逻辑,添加构建渲染数据和模板渲染功能,优化代码结构
This commit is contained in:
@@ -1,28 +1,23 @@
|
||||
package route
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"text/template"
|
||||
_ "embed"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/Masterminds/sprig/v3"
|
||||
"github.com/iancoleman/strcase"
|
||||
"github.com/samber/lo"
|
||||
"go.ipao.vip/atomctl/v2/pkg/utils/gomod"
|
||||
"go.ipao.vip/atomctl/v2/pkg/utils/gomod"
|
||||
)
|
||||
|
||||
//go:embed router.go.tpl
|
||||
var routeTpl string
|
||||
|
||||
type RenderData struct {
|
||||
PackageName string
|
||||
ProjectPackage string
|
||||
Imports []string
|
||||
Controllers []string
|
||||
Routes map[string][]Router
|
||||
PackageName string
|
||||
ProjectPackage string
|
||||
Imports []string
|
||||
Controllers []string
|
||||
Routes map[string][]Router
|
||||
RouteGroups []string
|
||||
}
|
||||
|
||||
type Router struct {
|
||||
@@ -35,95 +30,24 @@ type Router struct {
|
||||
}
|
||||
|
||||
func Render(path string, routes []RouteDefinition) error {
|
||||
routePath := filepath.Join(path, "routes.gen.go")
|
||||
routePath := filepath.Join(path, "routes.gen.go")
|
||||
|
||||
tmpl, err := template.New("route").Funcs(sprig.FuncMap()).Parse(routeTpl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := buildRenderData(RenderBuildOpts{
|
||||
PackageName: filepath.Base(path),
|
||||
ProjectPackage: gomod.GetModuleName(),
|
||||
Routes: routes,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
renderData := RenderData{
|
||||
PackageName: filepath.Base(path),
|
||||
ProjectPackage: gomod.GetModuleName(),
|
||||
Routes: make(map[string][]Router),
|
||||
}
|
||||
out, err := renderTemplate(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// collect imports
|
||||
imports := []string{}
|
||||
controllers := []string{}
|
||||
for _, route := range routes {
|
||||
imports = append(imports, route.Imports...)
|
||||
controllers = append(controllers, fmt.Sprintf("%s *%s", strcase.ToLowerCamel(route.Name), route.Name))
|
||||
for _, action := range route.Actions {
|
||||
funcName := fmt.Sprintf("Func%d", len(action.Params))
|
||||
if action.HasData {
|
||||
funcName = "Data" + funcName
|
||||
}
|
||||
|
||||
renderData.Routes[route.Name] = append(renderData.Routes[route.Name], Router{
|
||||
Method: strcase.ToCamel(action.Method),
|
||||
Route: action.Route,
|
||||
Controller: strcase.ToLowerCamel(route.Name),
|
||||
Action: action.Name,
|
||||
Func: funcName,
|
||||
Params: lo.FilterMap(action.Params, func(item ParamDefinition, _ int) (string, bool) {
|
||||
key := item.Name
|
||||
if item.Key != "" {
|
||||
key = item.Key
|
||||
}
|
||||
|
||||
switch item.Position {
|
||||
case PositionQuery:
|
||||
return fmt.Sprintf(`Query%s[%s]("%s")`, isScalarType(item.Type), item.Type, key), true
|
||||
case PositionHeader:
|
||||
return fmt.Sprintf(`Header[%s]("%s")`, item.Type, key), true
|
||||
case PositionFile:
|
||||
return fmt.Sprintf(`File[multipart.FileHeader]("%s")`, key), true
|
||||
case PositionCookie:
|
||||
if item.Type == "string" {
|
||||
return fmt.Sprintf(`CookieParam("%s")`, key), true
|
||||
}
|
||||
|
||||
return fmt.Sprintf(`Cookie[%s]("%s")`, item.Type, key), true
|
||||
case PositionBody:
|
||||
return fmt.Sprintf(`Body[%s]("%s")`, item.Type, key), true
|
||||
case PositionPath:
|
||||
return fmt.Sprintf(`Path%s[%s]("%s")`, isScalarType(item.Type), item.Type, key), true
|
||||
case PositionLocal:
|
||||
return fmt.Sprintf(`Local[%s]("%s")`, item.Type, key), true
|
||||
}
|
||||
return "", false
|
||||
}),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
renderData.Imports = lo.Uniq(imports)
|
||||
renderData.Controllers = lo.Uniq(controllers)
|
||||
|
||||
var buf bytes.Buffer
|
||||
err = tmpl.Execute(&buf, renderData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f, err := os.OpenFile(routePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = f.Write(buf.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func isScalarType(t string) string {
|
||||
switch t {
|
||||
case "string", "int", "int32", "int64", "float32", "float64", "bool":
|
||||
return "Param"
|
||||
}
|
||||
return ""
|
||||
if err := os.WriteFile(routePath, out, 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user