diff --git a/pkg/ast/route/render.go b/pkg/ast/route/render.go index 3a2d74d..7fc0d5f 100644 --- a/pkg/ast/route/render.go +++ b/pkg/ast/route/render.go @@ -68,18 +68,16 @@ func Render(path string, routes []RouteDefinition) error { Func: funcName, Params: lo.FilterMap(action.Params, func(item ParamDefinition, _ int) (string, bool) { switch item.Position { - case PositionURI: - return fmt.Sprintf(`URI[%s]("%s")`, item.Type, item.Name), true case PositionQuery: return fmt.Sprintf(`Query%s[%s]("%s")`, isScalarType(item.Type), item.Type, item.Name), true case PositionHeader: return fmt.Sprintf(`Header[%s]("%s")`, item.Type, item.Name), true case PositionCookie: - return fmt.Sprintf(`Cookie[%s]("%s")`, item.Type, item.Name), true + return fmt.Sprintf(`Cookie%s[%s]("%s")`, isScalarType(item.Type), item.Type, item.Name), true case PositionBody: return fmt.Sprintf(`Body[%s]("%s")`, item.Type, item.Name), true case PositionPath: - return fmt.Sprintf(`Path[%s]("%s")`, item.Type, item.Name), true + return fmt.Sprintf(`Path%s[%s]("%s")`, isScalarType(item.Type), item.Type, item.Name), true case PositionLocal: key := item.Name if item.Key != "" { diff --git a/pkg/ast/route/route.go b/pkg/ast/route/route.go index 9df4a26..d11ff75 100644 --- a/pkg/ast/route/route.go +++ b/pkg/ast/route/route.go @@ -43,8 +43,6 @@ func positionFromString(v string) Position { switch v { case "path": return PositionPath - case "uri": - return PositionURI case "query": return PositionQuery case "body": @@ -61,7 +59,6 @@ func positionFromString(v string) Position { const ( PositionPath Position = "path" - PositionURI Position = "uri" PositionQuery Position = "query" PositionBody Position = "body" PositionHeader Position = "header" diff --git a/templates/project/pkg/f/bind.go.tpl b/templates/project/pkg/f/bind.go.tpl index 7a92fdd..308c13f 100644 --- a/templates/project/pkg/f/bind.go.tpl +++ b/templates/project/pkg/f/bind.go.tpl @@ -5,6 +5,13 @@ import ( "github.com/pkg/errors" ) +func Local[T any](key string) func(fiber.Ctx) (T, error) { + return func(ctx fiber.Ctx) (T, error) { + v := fiber.Locals[T](ctx, key) + return v, nil + } +} + func Path[T fiber.GenericType](key string) func(fiber.Ctx) (T, error) { return func(ctx fiber.Ctx) (T, error) { v := fiber.Params[T](ctx, key) @@ -12,14 +19,10 @@ func Path[T fiber.GenericType](key string) func(fiber.Ctx) (T, error) { } } -func URI[T any](name string) func(fiber.Ctx) (*T, error) { - return func(ctx fiber.Ctx) (*T, error) { - p := new(T) - if err := ctx.Bind().URI(p); err != nil { - return nil, errors.Wrapf(err, "uri: %s", name) - } - - return p, nil +func PathParam[T fiber.GenericType](name string) func(fiber.Ctx) (T, error) { + return func(ctx fiber.Ctx) (T, error) { + v := fiber.Params[T](ctx, name) + return v, nil } } @@ -63,3 +66,20 @@ func Header[T any](name string) func(fiber.Ctx) (*T, error) { return p, nil } } + +func Cookie[T any](name string) func(fiber.Ctx) (*T, error) { + return func(ctx fiber.Ctx) (*T, error) { + p := new(T) + if err := ctx.Bind().Cookie(p); err != nil { + return nil, errors.Wrapf(err, "cookie: %s", name) + } + + return p, nil + } +} + +func CookieParam(name string) func(fiber.Ctx) (string, error) { + return func(ctx fiber.Ctx) (string, error) { + return ctx.Cookies(name), nil + } +}