-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind.go
94 lines (79 loc) · 3.11 KB
/
bind.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package binding
import (
"github.com/gofiber/fiber/v3"
)
func NewReq[REQ any](c fiber.Ctx) (*REQ, error) {
req := new(REQ)
err := Bind(c, req)
if err != nil {
return nil, err
}
return req, nil
}
// BindJSON is a shortcut for c.MustBindWith(obj, binding.JSON).
func BindJSON(c fiber.Ctx, obj interface{}) error {
return MustBindWith(c, obj, JSON)
}
// BindXML is a shortcut for c.MustBindWith(obj, binding.BindXML).
func BindXML(c fiber.Ctx, obj interface{}) error {
return MustBindWith(c, obj, XML)
}
// BindQuery is a shortcut for c.MustBindWith(obj, binding.Query).
func BindQuery(c fiber.Ctx, obj interface{}) error {
return MustBindWith(c, obj, Query)
}
// BindYAML is a shortcut for c.MustBindWith(obj, binding.YAML).
func BindYAML(c fiber.Ctx, obj interface{}) error {
return MustBindWith(c, obj, YAML)
}
// MustBindWith binds the passed struct pointer using the specified binding engine.
// BindUri binds the passed struct pointer using binding.Uri.
// It will abort the request with HTTP 400 if any error occurs.
func BindUri(c fiber.Ctx, obj interface{}) error {
return ShouldBindUri(c, obj)
}
// MustBindWith binds the passed struct pointer using the specified binding engine.
// It will abort the request with HTTP 400 if any error occurs.
// See the binding package.
func MustBindWith(c fiber.Ctx, obj interface{}, b Binding) error {
return ShouldBindWith(c, obj, b)
}
// ShouldBind checks the Content-Type to select a binding engine automatically,
// Depending the "Content-Type" header different bindings are used:
//
// "application/json" --> JSON binding
// "application/xml" --> XML binding
//
// otherwise --> returns an error
// It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.
// It decodes the json payload into the struct specified as a pointer.
// Like c.GinBind() but this method does not set the response status code to 400 and abort if the json is not valid.
func ShouldBind(c fiber.Ctx, obj interface{}) error {
b := Default(c.Method(), c.Request().Header.ContentType())
return ShouldBindWith(c, obj, b)
}
// ShouldBindJSON is a shortcut for c.ShouldBindWith(obj, binding.JSON).
func ShouldBindJSON(c fiber.Ctx, obj interface{}) error {
return ShouldBindWith(c, obj, JSON)
}
// ShouldBindXML is a shortcut for c.ShouldBindWith(obj, binding.XML).
func ShouldBindXML(c fiber.Ctx, obj interface{}) error {
return ShouldBindWith(c, obj, XML)
}
// ShouldBindQuery is a shortcut for c.ShouldBindWith(obj, binding.Query).
func ShouldBindQuery(c fiber.Ctx, obj interface{}) error {
return ShouldBindWith(c, obj, Query)
}
// ShouldBindYAML is a shortcut for c.ShouldBindWith(obj, binding.YAML).
func ShouldBindYAML(c fiber.Ctx, obj interface{}) error {
return ShouldBindWith(c, obj, YAML)
}
// ShouldBindUri binds the passed struct pointer using the specified binding engine.
func ShouldBindUri(c fiber.Ctx, obj interface{}) error {
return Uri.Bind(c, obj)
}
// ShouldBindWith binds the passed struct pointer using the specified binding engine.
// See the binding package.
func ShouldBindWith(c fiber.Ctx, obj interface{}, b Binding) error {
return b.Bind(c, obj)
}