-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmquery.go
More file actions
376 lines (305 loc) · 8.39 KB
/
mquery.go
File metadata and controls
376 lines (305 loc) · 8.39 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package mquery
import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
)
var ErrURLEmpty = errors.New("url is empty")
// ParseQuery 解析 URL 上的 query string
func ParseQuery(r *http.Request) (url.Values, error) {
var newValues url.Values
if r.URL == nil {
return nil, ErrURLEmpty
}
newValues = r.URL.Query()
if newValues != nil {
return newValues, nil
}
return url.ParseQuery(r.URL.RawQuery)
}
// Queries 获取 URL 上的请求字段,若不存在则返回空的 url.Value
func Queries(r *http.Request) url.Values {
values, err := ParseQuery(r)
if err != nil {
return make(url.Values)
}
return values
}
// Query url.Values 装载对象
type Query struct {
v url.Values
}
// DynamicQueryValue 回调计算 value 值的 handler,可以用于延迟计算 value
type DynamicQueryValue func() string
// DynamicQueryPairs 回调计算 pair 对的 handler,可以用于延迟计算多对 key value
type DynamicQueryPairs func() []string
// New 获取一个 Query 实例
func New() *Query {
return &Query{v: url.Values{}}
}
// NewWith 获取一个 Query 实例,同时初始化
func NewWith(v url.Values) *Query {
return &Query{v: v}
}
// Set set the key to value. It replaces any existing
// values.
func (q *Query) Set(key, value string) *Query {
q.v.Set(key, value)
return q
}
// SetD set the key to value but dynamic. It replaces any existing
// values.
func (q *Query) SetD(key string, value DynamicQueryValue) *Query {
q.v.Set(key, value())
return q
}
// SetIf set the key to value if ensure is true. It replaces any existing
// values.
func (q *Query) SetIf(ensure bool, key, value string) *Query {
if ensure {
q.Set(key, value)
}
return q
}
// SetIfD set the key to value dynamically if ensure is true. It replaces any existing
// values.
func (q *Query) SetIfD(ensure bool, key string, value DynamicQueryValue) *Query {
if ensure {
q.Set(key, value())
}
return q
}
// SetPairs set the key to value. It replaces any existing
// values.
// q.SetPairs(key1,value1,key2,value2,...)
func (q *Query) SetPairs(key, value string, pairs ...string) *Query {
q.v.Set(key, value)
if len(pairs) <= 0 || len(pairs)%2 != 0 {
return q
}
for i := 0; i < len(pairs); i += 2 {
q.v.Set(pairs[i], pairs[i+1])
}
return q
}
// SetPairsD set the key to value but dynamic. It replaces any existing
// values.
// q.SetPairsD(func ()[]string{return []string{key1,value1,key2,value2,...}})
func (q *Query) SetPairsD(pairs DynamicQueryPairs) *Query {
_pairs := pairs()
l := len(_pairs)
if l <= 0 || l%2 != 0 {
return q
}
for i := 0; i < l; i += 2 {
q.v.Set(_pairs[i], _pairs[i+1])
}
return q
}
// SetPairsIf set the key to value if ensure is true. It replaces any existing
// values.
// q.SetPairsIf(GetTrue(),key1,value1,key2,value2,...)
func (q *Query) SetPairsIf(ensure bool, key, value string, pairs ...string) *Query {
if ensure {
q.SetPairs(key, value, pairs...)
}
return q
}
// SetPairsIfD set the key to value dynamically if ensure is true. It replaces any existing
// values.
// q.SetPairsIfD(GetTrue(),func ()[]string{return []string{key1,value1,key2,value2,...}})
func (q *Query) SetPairsIfD(ensure bool, pairs DynamicQueryPairs) *Query {
if ensure {
q.SetPairsD(pairs)
}
return q
}
// SetByM sets the key to value by map. It replaces any existing
// values.
// q.SetByM(map[string]string{"key1":value1})
func (q *Query) SetByM(pairs map[string]string) *Query {
if pairs == nil {
return q
}
for key, value := range pairs {
q.v.Set(key, value)
}
return q
}
// SetByMIf set the key to value by map if ensure is true. It replaces any existing
// values.
// q.SetByMIf(GetTrue(),map[string]string{"key1":value1})
func (q *Query) SetByMIf(ensure bool, pairs map[string]string) *Query {
if ensure {
q.SetByM(pairs)
}
return q
}
// Get get the first value associated with the given key.
// If there are no values associated with the key, Get returns
// the empty string. To access multiple values, use the map
// directly.
func (q *Query) Get(key string) string {
return q.v.Get(key)
}
// Add add the value to key. It appends to any existing
// values associated with key.
func (q *Query) Add(key, value string) *Query {
q.v.Add(key, value)
return q
}
// AddD add the value to key but dynamic. It appends to any existing
// values associated with key.
func (q *Query) AddD(key string, value DynamicQueryValue) *Query {
q.v.Add(key, value())
return q
}
// AddIf add the value to key if ensure is true. It appends to any existing
// values associated with key.
func (q *Query) AddIf(ensure bool, key, value string) *Query {
if ensure {
q.Add(key, value)
}
return q
}
// AddIf add the value to key dynamically if ensure is true. It appends to any existing
// values associated with key.
func (q *Query) AddIfD(ensure bool, key string, value DynamicQueryValue) *Query {
if ensure {
q.Add(key, value())
}
return q
}
// AddPairs add the value to key. It appends to any existing
// values associated with key.
// q.AddPairs(key1,value1,key2,value2,...)
func (q *Query) AddPairs(key, value string, pairs ...string) *Query {
q.v.Add(key, value)
if len(pairs) <= 0 || len(pairs)%2 != 0 {
return q
}
for i := 0; i < len(pairs); i += 2 {
q.v.Add(pairs[i], pairs[i+1])
}
return q
}
// AddPairsD add the value to key but dynamic. It appends to any existing
// values associated with key.
// q.AddPairsD(func()[]string{return []string{key1,value1,key2,value2,...}})
func (q *Query) AddPairsD(pairs DynamicQueryPairs) *Query {
_pairs := pairs()
l := len(_pairs)
if l <= 0 || l%2 != 0 {
return q
}
for i := 0; i < l; i += 2 {
q.v.Add(_pairs[i], _pairs[i+1])
}
return q
}
// AddPairsIf add the value to key if ensure is true. It appends to any existing
// values associated with key.
// q.AddPairsIf(GetTrue(),key1,value1,key2,value2,...)
func (q *Query) AddPairsIf(ensure bool, key, value string, pairs ...string) *Query {
if ensure {
q.AddPairs(key, value, pairs...)
}
return q
}
// AddPairsIfD add the value to key dynamically if ensure is true. It appends to any existing
// values associated with key.
// q.AddPairsIfD(GetTrue(),func()[]string{return []string{key1,value1,key2,value2,...}})
func (q *Query) AddPairsIfD(ensure bool, pairs DynamicQueryPairs) *Query {
if ensure {
q.AddPairsD(pairs)
}
return q
}
// AddByM add the value to key by map. It appends to any existing
// values associated with key.
// q.AddByM(map[string]string{"key1":value1})
func (q *Query) AddByM(pairs map[string]string) *Query {
if pairs == nil {
return q
}
for key, value := range pairs {
q.v.Add(key, value)
}
return q
}
// AddByMIf add the value to key by map if ensure is true. It appends to any existing
// values associated with key.
// q.AddByMIf(GetTrue(),map[string]string{"key1":value1})
func (q *Query) AddByMIf(ensure bool, pairs map[string]string) *Query {
if ensure {
q.AddByM(pairs)
}
return q
}
// Del delete the values associated with key.
func (q *Query) Del(key string) *Query {
q.v.Del(key)
return q
}
// DelIf delete the values associated with key if ensure is true.
func (q *Query) DelIf(ensure bool, key string) *Query {
if ensure {
q.Del(key)
}
return q
}
// Encode encode the values into ``URL encoded'' form
// ("bar=baz&foo=quux") sorted by key.
func (q *Query) Encode() string {
return q.v.Encode()
}
// With 将 v 同步合并
func (q *Query) With(v url.Values) *Query {
for key, valueArr := range v {
for _, valueItem := range valueArr {
if q.v.Get(key) == "" {
q.v.Set(key, valueItem)
} else {
q.v.Add(key, valueItem)
}
}
}
return q
}
// WithIf 将 v 同步合并,当且 ensure 为 true
func (q *Query) WithIf(ensure bool, v url.Values) *Query {
if ensure {
q.With(v)
}
return q
}
// ParseForm 解析指定 URL-encoded query string
// 底层调用的是 url.ParseQuery
func (q *Query) ParseForm(query string) *Query {
values, err := url.ParseQuery(query)
if err != nil {
return q
}
return q.With(values)
}
// AppendToURI 将请求字段追加至 URI 上
func (q *Query) AppendToURI(uri string) string {
if strings.Index(uri, "?") != -1 {
return uri + q.Encode()
}
return uri + "?" + q.Encode()
}
// AppendToURIf 将请求字段追加至 URI 上
func (q *Query) AppendToURIFormat(formatURI string, a ...interface{}) string {
return q.AppendToURI(fmt.Sprintf(formatURI, a...))
}
// Values 获取内部持有的 Values
func (q *Query) Values() url.Values {
return q.v
}
// Len 获取键值对数量
func (q *Query) Len() int {
return len(q.v)
}