From 9c910b6ede0955d8551d61dd245e339884ae7cf8 Mon Sep 17 00:00:00 2001 From: Rogee Date: Thu, 11 Sep 2025 19:22:47 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=BB=9F=E4=B8=80=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=A0=BC=E5=BC=8F=EF=BC=8C=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/ast/route/builder.go | 193 ++++++++++++++++++++------------------- 1 file changed, 97 insertions(+), 96 deletions(-) diff --git a/pkg/ast/route/builder.go b/pkg/ast/route/builder.go index 31cc4d2..39836ba 100644 --- a/pkg/ast/route/builder.go +++ b/pkg/ast/route/builder.go @@ -1,122 +1,123 @@ package route import ( - "fmt" - "sort" + "fmt" + "sort" - "github.com/iancoleman/strcase" - "github.com/samber/lo" + "github.com/iancoleman/strcase" + "github.com/samber/lo" ) type RenderBuildOpts struct { - PackageName string - ProjectPackage string - Routes []RouteDefinition + PackageName string + ProjectPackage string + Routes []RouteDefinition } func buildRenderData(opts RenderBuildOpts) (RenderData, error) { - rd := RenderData{ - PackageName: opts.PackageName, - ProjectPackage: opts.ProjectPackage, - Imports: []string{}, - Controllers: []string{}, - Routes: make(map[string][]Router), - RouteGroups: []string{}, - } + rd := RenderData{ + PackageName: opts.PackageName, + ProjectPackage: opts.ProjectPackage, + Imports: []string{}, + Controllers: []string{}, + Routes: make(map[string][]Router), + RouteGroups: []string{}, + } - imports := []string{} - controllers := []string{} + imports := []string{} + controllers := []string{} - for _, route := range opts.Routes { - imports = append(imports, route.Imports...) - controllers = append(controllers, fmt.Sprintf("%s *%s", strcase.ToLowerCamel(route.Name), route.Name)) + for _, route := range opts.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 - } + for _, action := range route.Actions { + funcName := fmt.Sprintf("Func%d", len(action.Params)) + if action.HasData { + funcName = "Data" + funcName + } - params := lo.FilterMap(action.Params, func(item ParamDefinition, _ int) (string, bool) { - tok := buildParamToken(item) - if tok == "" { - return "", false - } - return tok, true - }) + params := lo.FilterMap(action.Params, func(item ParamDefinition, _ int) (string, bool) { + tok := buildParamToken(item) + if tok == "" { + return "", false + } + return tok, true + }) - rd.Routes[route.Name] = append(rd.Routes[route.Name], Router{ - Method: strcase.ToCamel(action.Method), - Route: action.Route, - Controller: strcase.ToLowerCamel(route.Name), - Action: action.Name, - Func: funcName, - Params: params, - }) - } - } + rd.Routes[route.Name] = append(rd.Routes[route.Name], Router{ + Method: strcase.ToCamel(action.Method), + Route: action.Route, + Controller: strcase.ToLowerCamel(route.Name), + Action: action.Name, + Func: funcName, + Params: params, + }) + } + } - // de-dup and sort imports/controllers for stable output - rd.Imports = lo.Uniq(imports) - sort.Strings(rd.Imports) - rd.Controllers = lo.Uniq(controllers) - sort.Strings(rd.Controllers) + // de-dup and sort imports/controllers for stable output + rd.Imports = lo.Uniq(imports) + sort.Strings(rd.Imports) + rd.Controllers = lo.Uniq(controllers) + sort.Strings(rd.Controllers) - // stable order for route groups and entries - for k := range rd.Routes { - rd.RouteGroups = append(rd.RouteGroups, k) - } - sort.Strings(rd.RouteGroups) - for _, k := range rd.RouteGroups { - items := rd.Routes[k] - sort.Slice(items, func(i, j int) bool { - if items[i].Method != items[j].Method { - return items[i].Method < items[j].Method - } - if items[i].Route != items[j].Route { - return items[i].Route < items[j].Route - } - return items[i].Action < items[j].Action - }) - rd.Routes[k] = items - } + // stable order for route groups and entries + for k := range rd.Routes { + rd.RouteGroups = append(rd.RouteGroups, k) + } + sort.Strings(rd.RouteGroups) + for _, k := range rd.RouteGroups { + items := rd.Routes[k] + sort.Slice(items, func(i, j int) bool { + if items[i].Method != items[j].Method { + return items[i].Method < items[j].Method + } + if items[i].Route != items[j].Route { + return items[i].Route < items[j].Route + } + return items[i].Action < items[j].Action + }) + rd.Routes[k] = items + } - return rd, nil + return rd, nil } func buildParamToken(item ParamDefinition) string { - key := item.Name - if item.Key != "" { - key = item.Key - } + key := item.Name + if item.Key != "" { + key = item.Key + } - switch item.Position { - case PositionQuery: - return fmt.Sprintf(`Query%s[%s]("%s")`, scalarSuffix(item.Type), item.Type, key) - case PositionHeader: - return fmt.Sprintf(`Header[%s]("%s")`, item.Type, key) - case PositionFile: - return fmt.Sprintf(`File[multipart.FileHeader]("%s")`, key) - case PositionCookie: - if item.Type == "string" { - return fmt.Sprintf(`CookieParam("%s")`, key) - } - return fmt.Sprintf(`Cookie[%s]("%s")`, item.Type, key) - case PositionBody: - return fmt.Sprintf(`Body[%s]("%s")`, item.Type, key) - case PositionPath: - return fmt.Sprintf(`Path%s[%s]("%s")`, scalarSuffix(item.Type), item.Type, key) - case PositionLocal: - return fmt.Sprintf(`Local[%s]("%s")`, item.Type, key) - } - return "" + switch item.Position { + case PositionQuery: + return fmt.Sprintf(`Query%s[%s]("%s")`, scalarSuffix(item.Type), item.Type, key) + case PositionHeader: + return fmt.Sprintf(`Header[%s]("%s")`, item.Type, key) + case PositionFile: + return fmt.Sprintf(`File[multipart.FileHeader]("%s")`, key) + case PositionCookie: + if item.Type == "string" { + return fmt.Sprintf(`CookieParam("%s")`, key) + } + return fmt.Sprintf(`Cookie[%s]("%s")`, item.Type, key) + case PositionBody: + return fmt.Sprintf(`Body[%s]("%s")`, item.Type, key) + case PositionPath: + return fmt.Sprintf(`Path%s[%s]("%s")`, scalarSuffix(item.Type), item.Type, key) + case PositionLocal: + return fmt.Sprintf(`Local[%s]("%s")`, item.Type, key) + } + return "" } func scalarSuffix(t string) string { - switch t { - case "string", "int", "int32", "int64", "float32", "float64", "bool": - return "Param" - } - return "" + switch t { + case "string", "int", "int8", "int16", "int32", "int64", + "uint", "uint8", "uint16", "uint32", "uint64", + "float32", "float64", "bool": + return "Param" + } + return "" } -