-
Notifications
You must be signed in to change notification settings - Fork 1
/
binding.go
189 lines (169 loc) · 5.66 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package gors
import (
"context"
"errors"
"fmt"
"github.com/gin-gonic/gin/binding"
internalbinding "github.com/go-leo/gors/internal/pkg/binding"
"github.com/go-leo/gox/stringx"
"io"
"net/http"
)
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 Error{StatusCode: http.StatusBadRequest, Message: err.Error()}
}
}
if err := Validate(req); err != nil {
return Error{StatusCode: http.StatusBadRequest, Message: err.Error()}
}
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, internalbinding.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 AutoBinding(ctx context.Context, req any, _ string) error {
// c := FromContext(ctx)
// switch {
// case strings.Contains(c.ContentType(), annotation.FormContentType):
// return FormBinding(ctx, req, annotation.FormContentType)
// case strings.Contains(c.ContentType(), annotation.FormMultipartContentType):
// return FormMultipartBinding(ctx, req, annotation.FormMultipartContentType)
// case strings.Contains(c.ContentType(), annotation.JSONContentType):
// return JSONBinding(ctx, req, annotation.JSONContentType)
// case strings.Contains(c.ContentType(), annotation.XMLContentType):
// return XMLBinding(ctx, req, annotation.XMLContentType)
// case strings.Contains(c.ContentType(), annotation.XML2ContentType):
// return XMLBinding(ctx, req, annotation.XML2ContentType)
// case strings.Contains(c.ContentType(), annotation.ProtoBufContentType):
// return ProtoBufBinding(ctx, req, annotation.ProtoBufContentType)
// case strings.Contains(c.ContentType(), annotation.MsgPackContentType):
// return MsgPackBinding(ctx, req, annotation.MsgPackContentType)
// case strings.Contains(c.ContentType(), annotation.MsgPack2ContentType):
// return MsgPackBinding(ctx, req, annotation.MsgPack2ContentType)
// case strings.Contains(c.ContentType(), annotation.YAMLContentType):
// return YAMLBinding(ctx, req, annotation.YAMLContentType)
// case strings.Contains(c.ContentType(), annotation.TOMLContentType):
// return TOMLBinding(ctx, req, annotation.TOMLContentType)
// default:
// customBinding, ok := req.(Binding)
// if !ok {
// return nil
// }
// return customBinding.Bind(ctx)
// }
//}