diff --git a/pkg/ast/route/render.go b/pkg/ast/route/render.go index e6640e9..9660896 100644 --- a/pkg/ast/route/render.go +++ b/pkg/ast/route/render.go @@ -67,31 +67,29 @@ func Render(path string, routes []RouteDefinition) error { 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, item.Name), true + return fmt.Sprintf(`Query%s[%s]("%s")`, isScalarType(item.Type), item.Type, key), true 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: - key := item.Name - if item.Key != "" { - key = item.Key - } - 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, item.Name), true + return fmt.Sprintf(`Body[%s]("%s")`, item.Type, key), true 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: - key := item.Name - if item.Key != "" { - key = item.Key - } return fmt.Sprintf(`Local[%s]("%s")`, item.Type, key), true } return "", false diff --git a/pkg/ast/route/route.go b/pkg/ast/route/route.go index d11ff75..a1dd31f 100644 --- a/pkg/ast/route/route.go +++ b/pkg/ast/route/route.go @@ -53,6 +53,8 @@ func positionFromString(v string) Position { return PositionCookie case "local": return PositionLocal + case "file": + return PositionFile } panic("invalid position: " + v) } @@ -64,6 +66,7 @@ const ( PositionHeader Position = "header" PositionCookie Position = "cookie" PositionLocal Position = "local" + PositionFile Position = "file" ) func ParseFile(file string) []RouteDefinition { diff --git a/templates/project/pkg/f/bind.go.tpl b/templates/project/pkg/f/bind.go.tpl index 308c13f..ffb7880 100644 --- a/templates/project/pkg/f/bind.go.tpl +++ b/templates/project/pkg/f/bind.go.tpl @@ -1,10 +1,19 @@ package f import ( + "mime/multipart" + "github.com/gofiber/fiber/v3" "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) { return func(ctx fiber.Ctx) (T, error) { v := fiber.Locals[T](ctx, key)