-
Notifications
You must be signed in to change notification settings - Fork 0
/
binding.go
167 lines (146 loc) · 4.4 KB
/
binding.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package gors
import (
"context"
"errors"
"fmt"
"github.com/gin-gonic/gin/binding"
bindingPkg "github.com/go-leo/gors/pkg/binding"
"github.com/go-leo/gox/stringx"
"io"
"net/http"
)
var (
ErrBinding = Error{StatusCode: 200, Code: 400, Message: "failed to bind request"}.Froze()
ErrValidate = Error{StatusCode: 200, Code: 400, Message: "failed to validate request"}.Froze()
)
func RequestBind(ctx context.Context, req any, tag string, bindings ...func(ctx context.Context, req any, tag string) error) error {
for _, fn := range bindings {
if err := fn(ctx, req, tag); err != nil {
return ErrBinding.Wrap(err)
}
}
if err := Validate(req); err != nil {
return ErrValidate.Wrap(err)
}
return nil
}
func StringBinding(ctx context.Context, req any, tag string) error {
c := FromContext(ctx)
body, err := io.ReadAll(c.Request.Body)
if err != nil {
return err
}
reqPtr, ok := req.(*string)
if !ok {
return fmt.Errorf("%T not converted to *string", req)
}
*reqPtr = string(body)
return nil
}
func BytesBinding(ctx context.Context, req any, tag string) error {
c := FromContext(ctx)
body, err := io.ReadAll(c.Request.Body)
if err != nil {
return err
}
reqPtr, ok := req.(*[]byte)
if !ok {
return fmt.Errorf("%T not converted to *[]byte", req)
}
*reqPtr = body
return nil
}
func ReaderBinding(ctx context.Context, req any, _ string) error {
c := FromContext(ctx)
reqPtr, ok := req.(*io.Reader)
if !ok {
return fmt.Errorf("%T not converted to *io.Reader", req)
}
*reqPtr = c.Request.Body
return nil
}
func UriBinding(ctx context.Context, req any, tag string) error {
c := FromContext(ctx)
if stringx.IsBlank(tag) {
return c.ShouldBindUri(req)
}
m := make(map[string][]string)
for _, v := range c.Params {
m[v.Key] = []string{v.Value}
}
return binding.MapFormWithTag(req, m, tag)
}
func QueryBinding(ctx context.Context, req any, tag string) error {
c := FromContext(ctx)
if stringx.IsBlank(tag) {
return c.ShouldBindWith(req, binding.Query)
}
return binding.MapFormWithTag(req, c.Request.URL.Query(), tag)
}
func HeaderBinding(ctx context.Context, req any, tag string) error {
c := FromContext(ctx)
if stringx.IsBlank(tag) {
return c.ShouldBindWith(req, binding.Header)
}
return binding.MapFormWithTag(req, c.Request.Header, tag)
}
func FormBinding(ctx context.Context, req any, tag string) error {
c := FromContext(ctx)
if stringx.IsBlank(tag) {
return c.ShouldBindWith(req, binding.Form)
}
if err := c.Request.ParseForm(); err != nil {
return err
}
const defaultMemory = 32 << 20
if err := c.Request.ParseMultipartForm(defaultMemory); err != nil && !errors.Is(err, http.ErrNotMultipart) {
return err
}
return binding.MapFormWithTag(req, c.Request.Form, tag)
}
func FormPostBinding(ctx context.Context, req any, tag string) error {
c := FromContext(ctx)
if stringx.IsBlank(tag) {
return c.ShouldBindWith(req, binding.FormPost)
}
if err := c.Request.ParseForm(); err != nil {
return err
}
return binding.MapFormWithTag(req, c.Request.PostForm, tag)
}
func FormMultipartBinding(ctx context.Context, req any, tag string) error {
return FromContext(ctx).ShouldBindWith(req, binding.FormMultipart)
}
func JSONBinding(ctx context.Context, req any, _ string) error {
return FromContext(ctx).ShouldBindWith(req, binding.JSON)
}
func ProtoJSONBinding(ctx context.Context, req any, _ string) error {
return FromContext(ctx).ShouldBindWith(req, bindingPkg.ProtoJSON)
}
func XMLBinding(ctx context.Context, req any, _ string) error {
return FromContext(ctx).ShouldBindWith(req, binding.XML)
}
func ProtoBufBinding(ctx context.Context, req any, _ string) error {
return FromContext(ctx).ShouldBindWith(req, binding.ProtoBuf)
}
func MsgPackBinding(ctx context.Context, req any, _ string) error {
return FromContext(ctx).ShouldBindWith(req, binding.MsgPack)
}
func YAMLBinding(ctx context.Context, req any, _ string) error {
return FromContext(ctx).ShouldBindWith(req, binding.YAML)
}
func TOMLBinding(ctx context.Context, req any, _ string) error {
return FromContext(ctx).ShouldBindWith(req, binding.TOML)
}
func CustomBinding(ctx context.Context, req any, _ string) error {
customBinding, ok := req.(Binding)
if !ok {
return nil
}
return customBinding.Bind(ctx)
}
func HttpRuleBinding(binding *bindingPkg.HttpRuleBinding) func(ctx context.Context, req any, _ string) error {
return func(ctx context.Context, req any, _ string) error {
return binding.Bind(FromContext(ctx), req)
}
}