fix: param
This commit is contained in:
@@ -68,18 +68,16 @@ func Render(path string, routes []RouteDefinition) error {
|
|||||||
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) {
|
||||||
switch item.Position {
|
switch item.Position {
|
||||||
case PositionURI:
|
|
||||||
return fmt.Sprintf(`URI[%s]("%s")`, item.Type, item.Name), true
|
|
||||||
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, item.Name), true
|
||||||
case PositionHeader:
|
case PositionHeader:
|
||||||
return fmt.Sprintf(`Header[%s]("%s")`, item.Type, item.Name), true
|
return fmt.Sprintf(`Header[%s]("%s")`, item.Type, item.Name), true
|
||||||
case PositionCookie:
|
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:
|
case PositionBody:
|
||||||
return fmt.Sprintf(`Body[%s]("%s")`, item.Type, item.Name), true
|
return fmt.Sprintf(`Body[%s]("%s")`, item.Type, item.Name), true
|
||||||
case PositionPath:
|
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:
|
case PositionLocal:
|
||||||
key := item.Name
|
key := item.Name
|
||||||
if item.Key != "" {
|
if item.Key != "" {
|
||||||
|
|||||||
@@ -43,8 +43,6 @@ func positionFromString(v string) Position {
|
|||||||
switch v {
|
switch v {
|
||||||
case "path":
|
case "path":
|
||||||
return PositionPath
|
return PositionPath
|
||||||
case "uri":
|
|
||||||
return PositionURI
|
|
||||||
case "query":
|
case "query":
|
||||||
return PositionQuery
|
return PositionQuery
|
||||||
case "body":
|
case "body":
|
||||||
@@ -61,7 +59,6 @@ func positionFromString(v string) Position {
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
PositionPath Position = "path"
|
PositionPath Position = "path"
|
||||||
PositionURI Position = "uri"
|
|
||||||
PositionQuery Position = "query"
|
PositionQuery Position = "query"
|
||||||
PositionBody Position = "body"
|
PositionBody Position = "body"
|
||||||
PositionHeader Position = "header"
|
PositionHeader Position = "header"
|
||||||
|
|||||||
@@ -5,6 +5,13 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"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) {
|
func Path[T fiber.GenericType](key string) func(fiber.Ctx) (T, error) {
|
||||||
return func(ctx fiber.Ctx) (T, error) {
|
return func(ctx fiber.Ctx) (T, error) {
|
||||||
v := fiber.Params[T](ctx, key)
|
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) {
|
func PathParam[T fiber.GenericType](name string) func(fiber.Ctx) (T, error) {
|
||||||
return func(ctx fiber.Ctx) (*T, error) {
|
return func(ctx fiber.Ctx) (T, error) {
|
||||||
p := new(T)
|
v := fiber.Params[T](ctx, name)
|
||||||
if err := ctx.Bind().URI(p); err != nil {
|
return v, nil
|
||||||
return nil, errors.Wrapf(err, "uri: %s", name)
|
|
||||||
}
|
|
||||||
|
|
||||||
return p, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,3 +66,20 @@ func Header[T any](name string) func(fiber.Ctx) (*T, error) {
|
|||||||
return p, nil
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user