-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
149 lines (138 loc) · 2.82 KB
/
query.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
package plover
import (
`encoding/json`
"net/url"
`strconv`
"strings"
)
type Query struct {
url.Values
}
// @title 是否有值
func (q Query) HasQuery() bool {
return q.Values != nil
}
// @title 是否有值
// @return bool
func (q Query) Has(key string) bool {
return !strings.EqualFold(q.Values.Get(key), "")
}
func (q Query) ToStruct(data interface{}, invoke ...func(key string, value interface{}) interface{}) (err error) {
var jsonByte []byte
var values = make(map[string]interface{})
for key, value := range q.Values {
if len(invoke) > 0 {
Invoke := invoke[0]
if len(value) > 1 {
values[key] = Invoke(key, value)
} else {
values[key] = Invoke(key, value[0])
}
} else {
if len(value) > 1 {
values[key] = value
} else {
values[key] = value[0]
}
}
}
jsonByte, err = json.Marshal(values)
if err != nil {
return err
}
err = json.Unmarshal(jsonByte, &data)
if err != nil {
return err
}
return nil
}
// @title 获取值
// @param key string
// @param defValue mixed
// @return mixed
func (q Query) Get(key string, defValue ...interface{}) interface{} {
var defaultValue interface{}
if len(defValue) > 0 {
defaultValue = defValue[0]
} else {
defaultValue = nil
}
if q.Values == nil {
return defaultValue
}
vs := q.Values[key]
if len(vs) == 0 {
return defaultValue
}
return vs[0]
}
func (q Query) GetFloat64(key string, defValue ...float64) float64 {
var defaultValue float64
if len(defValue) > 0 {
defaultValue = defValue[0]
} else {
defaultValue = 0
}
if q.Values == nil {
return defaultValue
}
vs := q.Values[key]
if len(vs) == 0 {
return defaultValue
}
value, _ := strconv.ParseFloat(vs[0], 32)
return value
}
// @title 获取int64数据
// @param key string
// @param defValue int64
// @return mixed
func (q Query) GetInt64(key string, defValue ...int64) int64 {
var defaultValue int64
if len(defValue) > 0 {
defaultValue = defValue[0]
} else {
defaultValue = 0
}
if q.Values == nil {
return defaultValue
}
vs := q.Values[key]
if len(vs) == 0 {
return defaultValue
}
value, _ := strconv.ParseInt(vs[0], 10, 64)
return value
}
// @title 获取String数据
// @param key string
// @param defValue int64
// @return mixed
func (q Query) GetString(key string, defValue ...string) string {
var defaultValue string
if len(defValue) > 0 {
defaultValue = defValue[0]
} else {
defaultValue = ""
}
if q.Values == nil {
return defaultValue
}
vs := q.Values[key]
if len(vs) == 0 {
return defaultValue
}
return vs[0]
}
func (q Query) Set(key, value string) {
q.Values.Set(key, value)
}
// Add adds the value to key. It appends to any existing
// values associated with key.
func (q Query) Add(key, value string) {
q.Values.Add(key, value)
}
// Del deletes the values associated with key.
func (q Query) Del(key string) {
q.Values.Del(key)
}