refactor: 统一代码格式,优化代码可读性

This commit is contained in:
Rogee
2025-09-11 19:22:47 +08:00
parent 6973c85730
commit 9c910b6ede

View File

@@ -1,122 +1,123 @@
package route package route
import ( import (
"fmt" "fmt"
"sort" "sort"
"github.com/iancoleman/strcase" "github.com/iancoleman/strcase"
"github.com/samber/lo" "github.com/samber/lo"
) )
type RenderBuildOpts struct { type RenderBuildOpts struct {
PackageName string PackageName string
ProjectPackage string ProjectPackage string
Routes []RouteDefinition Routes []RouteDefinition
} }
func buildRenderData(opts RenderBuildOpts) (RenderData, error) { func buildRenderData(opts RenderBuildOpts) (RenderData, error) {
rd := RenderData{ rd := RenderData{
PackageName: opts.PackageName, PackageName: opts.PackageName,
ProjectPackage: opts.ProjectPackage, ProjectPackage: opts.ProjectPackage,
Imports: []string{}, Imports: []string{},
Controllers: []string{}, Controllers: []string{},
Routes: make(map[string][]Router), Routes: make(map[string][]Router),
RouteGroups: []string{}, RouteGroups: []string{},
} }
imports := []string{} imports := []string{}
controllers := []string{} controllers := []string{}
for _, route := range opts.Routes { for _, route := range opts.Routes {
imports = append(imports, route.Imports...) imports = append(imports, route.Imports...)
controllers = append(controllers, fmt.Sprintf("%s *%s", strcase.ToLowerCamel(route.Name), route.Name)) controllers = append(controllers, fmt.Sprintf("%s *%s", strcase.ToLowerCamel(route.Name), route.Name))
for _, action := range route.Actions { for _, action := range route.Actions {
funcName := fmt.Sprintf("Func%d", len(action.Params)) funcName := fmt.Sprintf("Func%d", len(action.Params))
if action.HasData { if action.HasData {
funcName = "Data" + funcName funcName = "Data" + funcName
} }
params := lo.FilterMap(action.Params, func(item ParamDefinition, _ int) (string, bool) { params := lo.FilterMap(action.Params, func(item ParamDefinition, _ int) (string, bool) {
tok := buildParamToken(item) tok := buildParamToken(item)
if tok == "" { if tok == "" {
return "", false return "", false
} }
return tok, true return tok, true
}) })
rd.Routes[route.Name] = append(rd.Routes[route.Name], Router{ rd.Routes[route.Name] = append(rd.Routes[route.Name], Router{
Method: strcase.ToCamel(action.Method), Method: strcase.ToCamel(action.Method),
Route: action.Route, Route: action.Route,
Controller: strcase.ToLowerCamel(route.Name), Controller: strcase.ToLowerCamel(route.Name),
Action: action.Name, Action: action.Name,
Func: funcName, Func: funcName,
Params: params, Params: params,
}) })
} }
} }
// de-dup and sort imports/controllers for stable output // de-dup and sort imports/controllers for stable output
rd.Imports = lo.Uniq(imports) rd.Imports = lo.Uniq(imports)
sort.Strings(rd.Imports) sort.Strings(rd.Imports)
rd.Controllers = lo.Uniq(controllers) rd.Controllers = lo.Uniq(controllers)
sort.Strings(rd.Controllers) sort.Strings(rd.Controllers)
// stable order for route groups and entries // stable order for route groups and entries
for k := range rd.Routes { for k := range rd.Routes {
rd.RouteGroups = append(rd.RouteGroups, k) rd.RouteGroups = append(rd.RouteGroups, k)
} }
sort.Strings(rd.RouteGroups) sort.Strings(rd.RouteGroups)
for _, k := range rd.RouteGroups { for _, k := range rd.RouteGroups {
items := rd.Routes[k] items := rd.Routes[k]
sort.Slice(items, func(i, j int) bool { sort.Slice(items, func(i, j int) bool {
if items[i].Method != items[j].Method { if items[i].Method != items[j].Method {
return items[i].Method < items[j].Method return items[i].Method < items[j].Method
} }
if items[i].Route != items[j].Route { if items[i].Route != items[j].Route {
return items[i].Route < items[j].Route return items[i].Route < items[j].Route
} }
return items[i].Action < items[j].Action return items[i].Action < items[j].Action
}) })
rd.Routes[k] = items rd.Routes[k] = items
} }
return rd, nil return rd, nil
} }
func buildParamToken(item ParamDefinition) string { func buildParamToken(item ParamDefinition) string {
key := item.Name key := item.Name
if item.Key != "" { if item.Key != "" {
key = item.Key key = item.Key
} }
switch item.Position { switch item.Position {
case PositionQuery: case PositionQuery:
return fmt.Sprintf(`Query%s[%s]("%s")`, scalarSuffix(item.Type), item.Type, key) return fmt.Sprintf(`Query%s[%s]("%s")`, scalarSuffix(item.Type), item.Type, key)
case PositionHeader: case PositionHeader:
return fmt.Sprintf(`Header[%s]("%s")`, item.Type, key) return fmt.Sprintf(`Header[%s]("%s")`, item.Type, key)
case PositionFile: case PositionFile:
return fmt.Sprintf(`File[multipart.FileHeader]("%s")`, key) return fmt.Sprintf(`File[multipart.FileHeader]("%s")`, key)
case PositionCookie: case PositionCookie:
if item.Type == "string" { if item.Type == "string" {
return fmt.Sprintf(`CookieParam("%s")`, key) return fmt.Sprintf(`CookieParam("%s")`, key)
} }
return fmt.Sprintf(`Cookie[%s]("%s")`, item.Type, key) return fmt.Sprintf(`Cookie[%s]("%s")`, item.Type, key)
case PositionBody: case PositionBody:
return fmt.Sprintf(`Body[%s]("%s")`, item.Type, key) return fmt.Sprintf(`Body[%s]("%s")`, item.Type, key)
case PositionPath: case PositionPath:
return fmt.Sprintf(`Path%s[%s]("%s")`, scalarSuffix(item.Type), item.Type, key) return fmt.Sprintf(`Path%s[%s]("%s")`, scalarSuffix(item.Type), item.Type, key)
case PositionLocal: case PositionLocal:
return fmt.Sprintf(`Local[%s]("%s")`, item.Type, key) return fmt.Sprintf(`Local[%s]("%s")`, item.Type, key)
} }
return "" return ""
} }
func scalarSuffix(t string) string { func scalarSuffix(t string) string {
switch t { switch t {
case "string", "int", "int32", "int64", "float32", "float64", "bool": case "string", "int", "int8", "int16", "int32", "int64",
return "Param" "uint", "uint8", "uint16", "uint32", "uint64",
} "float32", "float64", "bool":
return "" return "Param"
}
return ""
} }