feat: update route generate

This commit is contained in:
Rogee
2025-01-15 10:44:07 +08:00
parent 915e7cb2f7
commit 1e03cf6038
3 changed files with 23 additions and 13 deletions

View File

@@ -67,31 +67,29 @@ func Render(path string, routes []RouteDefinition) error {
Action: action.Name, Action: action.Name,
Func: funcName, Func: funcName,
Params: lo.FilterMap(action.Params, func(item ParamDefinition, _ int) (string, bool) { Params: lo.FilterMap(action.Params, func(item ParamDefinition, _ int) (string, bool) {
key := item.Name
if item.Key != "" {
key = item.Key
}
switch item.Position { switch item.Position {
case PositionQuery: case PositionQuery:
return fmt.Sprintf(`Query%s[%s]("%s")`, isScalarType(item.Type), item.Type, item.Name), true return fmt.Sprintf(`Query%s[%s]("%s")`, isScalarType(item.Type), item.Type, key), true
case PositionHeader: case PositionHeader:
return fmt.Sprintf(`Header[%s]("%s")`, item.Type, item.Name), true return fmt.Sprintf(`Header[%s]("%s")`, item.Type, key), true
case PositionFile:
return fmt.Sprintf(`File[multipart.FileHeader]("%s")`, key), true
case PositionCookie: case PositionCookie:
key := item.Name
if item.Key != "" {
key = item.Key
}
if item.Type == "string" { if item.Type == "string" {
return fmt.Sprintf(`CookieParam("%s")`, key), true return fmt.Sprintf(`CookieParam("%s")`, key), true
} }
return fmt.Sprintf(`Cookie[%s]("%s")`, item.Type, key), true return fmt.Sprintf(`Cookie[%s]("%s")`, item.Type, key), true
case PositionBody: case PositionBody:
return fmt.Sprintf(`Body[%s]("%s")`, item.Type, item.Name), true return fmt.Sprintf(`Body[%s]("%s")`, item.Type, key), true
case PositionPath: case PositionPath:
return fmt.Sprintf(`Path%s[%s]("%s")`, isScalarType(item.Type), item.Type, item.Name), true return fmt.Sprintf(`Path%s[%s]("%s")`, isScalarType(item.Type), item.Type, key), true
case PositionLocal: case PositionLocal:
key := item.Name
if item.Key != "" {
key = item.Key
}
return fmt.Sprintf(`Local[%s]("%s")`, item.Type, key), true return fmt.Sprintf(`Local[%s]("%s")`, item.Type, key), true
} }
return "", false return "", false

View File

@@ -53,6 +53,8 @@ func positionFromString(v string) Position {
return PositionCookie return PositionCookie
case "local": case "local":
return PositionLocal return PositionLocal
case "file":
return PositionFile
} }
panic("invalid position: " + v) panic("invalid position: " + v)
} }
@@ -64,6 +66,7 @@ const (
PositionHeader Position = "header" PositionHeader Position = "header"
PositionCookie Position = "cookie" PositionCookie Position = "cookie"
PositionLocal Position = "local" PositionLocal Position = "local"
PositionFile Position = "file"
) )
func ParseFile(file string) []RouteDefinition { func ParseFile(file string) []RouteDefinition {

View File

@@ -1,10 +1,19 @@
package f package f
import ( import (
"mime/multipart"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
func File[T any](key string) func(fiber.Ctx) (*multipart.FileHeader, error) {
return func(ctx fiber.Ctx) (*multipart.FileHeader, error) {
_ = new(T)
return ctx.FormFile(key)
}
}
func Local[T any](key string) func(fiber.Ctx) (T, error) { func Local[T any](key string) func(fiber.Ctx) (T, error) {
return func(ctx fiber.Ctx) (T, error) { return func(ctx fiber.Ctx) (T, error) {
v := fiber.Locals[T](ctx, key) v := fiber.Locals[T](ctx, key)