-
Notifications
You must be signed in to change notification settings - Fork 4
/
input.go
283 lines (247 loc) · 6.24 KB
/
input.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package pine
import (
"encoding/json"
"errors"
"fmt"
"mime/multipart"
"strconv"
"strings"
"unsafe"
"github.com/valyala/fasthttp"
"github.com/valyala/fastjson"
)
var ErrKeyNotFound = errors.New("key not found")
const GoRawBody = "pine://input"
var EmptyBytes = []byte("")
type input struct {
ctx *Context
form *multipart.Form
err error
data map[string]any
}
func newInput(ctx *Context) *input {
v := &input{ctx: ctx}
v.ResetFromContext()
return v
}
// All 返回所有数据
func (i *input) All() map[string]any {
return i.data
}
// IsJson 判断是否为json提交
func (i *input) IsJson() bool {
return strings.Contains(i.ctx.Header(fasthttp.HeaderContentType), "/json")
}
// Add 新增数据
func (i *input) Add(key string, value any) {
if _, exist := i.data[key]; !exist {
i.data[key] = value
}
}
// Has 是否存在某个key数据
func (i *input) Has(key string) bool {
var exist bool
_, exist = i.data[key]
return exist
}
// Set 设置数据
func (i *input) Set(key string, value any) {
i.data[key] = value
}
// Get 获取数据
func (i *input) Get(key string) any {
return i.data[key]
}
// Only 获取指定Key的值
func (i *input) Only(keys ...string) map[string]any {
var data = map[string]any{}
for _, key := range keys {
data[key] = i.data[key]
}
return data
}
// Del 删除数据
func (i *input) Del(keys ...string) {
for _, key := range keys {
delete(i.data, key)
}
}
// Clear 清除数据
func (i *input) Clear() {
for key := range i.data {
delete(i.data, key)
}
}
func (i *input) LastErr() error {
return i.err
}
func (i *input) ResetFromContext() {
data := map[string]any{}
bodyJsonData := map[string]any{}
postData := i.ctx.RequestCtx.PostBody()
if i.IsJson() && len(postData) > 0 {
if postData[0] == 123 /** { **/ {
i.err = json.Unmarshal(postData, &bodyJsonData)
} else if postData[0] == 91 /** [ **/ {
var arrData []any
i.err = json.Unmarshal(postData, &arrData)
bodyJsonData[GoRawBody] = arrData
}
}
for key, values := range i.PostForm() {
if len(values[0]) > 0 {
data[key] = i.str2bytes(values[0])
} else {
data[key] = EmptyBytes
}
}
if multiForm, err := i.ctx.MultipartForm(); err == nil {
for key, values := range multiForm.Value {
if len(values[0]) > 0 {
data[key] = i.str2bytes(values[0])
} else {
data[key] = EmptyBytes
}
}
} else if fasthttp.ErrNoMultipartForm != err {
i.err = err
}
if len(bodyJsonData) > 0 {
for key, value := range bodyJsonData {
data[key] = value
}
}
i.data = data
}
func (i *input) GetForm() *multipart.Form {
if i.form == nil {
i.form, _ = i.ctx.MultipartForm()
}
return i.form
}
func (i *input) PostForm() map[string][]string {
data := map[string][]string{}
i.ctx.PostArgs().VisitAll(func(key, value []byte) {
data[string(key)] = []string{string(value)}
})
i.ctx.QueryArgs().VisitAll(func(key, value []byte) {
data[string(key)] = []string{string(value)}
})
return data
}
// DelExcept 删除指定keys之外的数据
func (i *input) DelExcept(keys ...string) {
for k := range i.data {
for _, exceptKey := range keys {
if exceptKey != k {
delete(i.data, k)
}
}
}
}
func (i *input) str2bytes(s string) []byte {
x := (*[2]uintptr)(unsafe.Pointer(&s))
h := [3]uintptr{x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h))
}
// GetDeep 深层获取value
func (i *input) GetDeep(key string) (*fastjson.Value, error) {
pars := strings.Split(key, ".")
if i.Has(pars[0]) {
data, err := i.GetBytes(pars[0])
if err != nil {
return nil, err
}
if data == nil && len(pars) == 1 {
return nil, nil
}
value, err := fastjson.ParseBytes(data)
if err != nil {
return nil, err
}
for _, par := range pars[:1] {
value = value.Get(par)
}
return value, nil
}
return nil, ErrKeyNotFound
}
// GetBytes 获取bytes数据, 仅个别类型
func (i *input) GetBytes(key string) ([]byte, error) {
if i.Has(key) {
switch value := i.Get(key); value.(type) {
case bool:
return i.str2bytes(strconv.FormatBool(value.(bool))), nil
case []byte:
return value.([]byte), nil
case string:
return i.str2bytes(value.(string)), nil
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return i.str2bytes(fmt.Sprintf("%d", value)), nil
case float64:
return i.str2bytes(strconv.FormatFloat(value.(float64), 'f', -1, 64)), nil
default:
return json.Marshal(value)
}
}
return nil, ErrKeyNotFound
}
func (i *input) GetInt(key string, defaultVal ...int) (val int, err error) {
var byts []byte
if byts, err = i.GetBytes(key); err == nil {
if val, err = strconv.Atoi(*(*string)(unsafe.Pointer(&byts))); err != nil && len(defaultVal) > 0 {
val, err = defaultVal[0], nil
}
}
return
}
func (i *input) GetInt64(key string, defaultVal ...int64) (val int64, err error) {
var byts []byte
if byts, err = i.GetBytes(key); err == nil {
if val, err = strconv.ParseInt(*(*string)(unsafe.Pointer(&byts)), 10, 64); err != nil && len(defaultVal) > 0 {
val, err = defaultVal[0], nil
}
}
return
}
func (i *input) GetBool(key string, defaultVal ...bool) (val bool, err error) {
var byts []byte
if byts, err = i.GetBytes(key); err == nil {
if val, err = strconv.ParseBool(*(*string)(unsafe.Pointer(&byts))); err != nil && len(defaultVal) > 0 {
val, err = defaultVal[0], nil
}
}
return val, err
}
func (i *input) GetFloat64(key string, defaultVal ...float64) (val float64, err error) {
var byts []byte
if byts, err = i.GetBytes(key); err == nil {
if val, err = strconv.ParseFloat(*(*string)(unsafe.Pointer(&byts)), 64); err != nil && len(defaultVal) > 0 {
val, err = defaultVal[0], nil
}
}
return
}
func (i *input) GetFormStrings(key string) []string {
if i.GetForm() != nil {
return i.GetForm().Value[key]
}
return nil
}
func (i *input) GetString(key string, defaultVal ...string) (val string, err error) {
var byts []byte
if byts, err = i.GetBytes(key); err == nil {
if len(byts) > 0 {
val = *(*string)(unsafe.Pointer(&byts))
} else if len(defaultVal) > 0 {
val = defaultVal[0]
}
}
return
}
func (i *input) Files(key string) (*multipart.FileHeader, error) {
return i.ctx.FormFile(key)
}