-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_query.go
164 lines (152 loc) · 4.48 KB
/
url_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package conv
import (
"encoding"
"encoding/json"
"net/url"
"reflect"
"strconv"
"time"
"github.com/mitchellh/mapstructure"
"github.com/MicroOps-cn/fuck/capacity"
)
// DecodeQuery takes an input query string and uses reflection to translate it to
// the dst structure. dst must be a pointer to a map or struct.
func DecodeQuery(query string, dst interface{}, config *mapstructure.DecoderConfig, fs ...mapstructure.DecodeHookFunc) error {
u, err := url.ParseQuery(query)
if err != nil {
return err
}
return DecodeURLValues(u, dst, config, fs...)
}
var defaultDecodeHookFuncs = []mapstructure.DecodeHookFunc{
StringToTimeHookFunc,
BinaryUnmarshalerHookFunc(),
mapstructure.TextUnmarshallerHookFunc(),
mapstructure.StringToIPHookFunc(),
mapstructure.StringToIPNetHookFunc(),
mapstructure.StringToTimeDurationHookFunc(),
capacity.StringToCapacityHookFunc(),
}
func DefaultDecodeHookFuncs() []mapstructure.DecodeHookFunc {
funcs := make([]mapstructure.DecodeHookFunc, len(defaultDecodeHookFuncs))
copy(funcs, defaultDecodeHookFuncs)
return funcs
}
// DecodeURLValues takes an input url.Values and uses reflection to translate it to
// the dst structure. dst must be a pointer to a map or struct.
func DecodeURLValues(u url.Values, dst interface{}, config *mapstructure.DecoderConfig, fs ...mapstructure.DecodeHookFunc) error {
if config == nil {
config = &mapstructure.DecoderConfig{
DecodeHook: mapstructure.ComposeDecodeHookFunc(DefaultDecodeHookFuncs()...),
}
}
config.Result = dst
fs = append(fs, config.DecodeHook, StringToBaseTypeHookFunc)
config.DecodeHook = URLValuesHookFunc(mapstructure.ComposeDecodeHookFunc(fs...))
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return err
}
return decoder.Decode(u)
}
// StringToTimeHookFunc returns a DecodeHookFunc that converts
// strings to time.Time.
func StringToTimeHookFunc(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}
if t != reflect.TypeOf(time.Time{}) {
return data, nil
}
if ts, err := strconv.ParseInt(data.(string), 10, 64); err == nil {
if ts > 1e12 {
return time.UnixMilli(ts), nil
}
return time.Unix(ts, 0), nil
}
if ts, err := strconv.ParseFloat(data.(string), 64); err == nil {
if ts > 1e12 {
return time.UnixMilli(int64(ts)), nil
}
return time.Unix(int64(ts), int64((ts-float64(int64(ts)))*1e3)%1e3*1e6), nil
}
var tm time.Time
var err error
for _, timeFormat := range timeFormats {
tm, err = time.Parse(timeFormat, data.(string))
if err == nil {
break
}
}
return tm, err
}
func StringToBaseTypeHookFunc(fv reflect.Value, tv reflect.Value) (interface{}, error) {
data := fv.Interface()
if fv.Kind() != reflect.String {
return data, nil
}
if fv.Kind() == reflect.String {
switch tv.Kind() {
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64:
return strconv.ParseInt(data.(string), 10, 64)
case reflect.Float64,
reflect.Float32:
return strconv.ParseFloat(data.(string), 64)
case reflect.Bool:
return strconv.ParseBool(data.(string))
case reflect.Struct, reflect.Map:
v := tv.Interface()
err := json.Unmarshal([]byte(data.(string)), &v)
if err != nil {
return nil, err
}
return v, nil
}
}
return data, nil
}
func URLValuesHookFunc(fc mapstructure.DecodeHookFunc) mapstructure.DecodeHookFunc {
return func(fv reflect.Value, tv reflect.Value) (interface{}, error) {
data, err := mapstructure.DecodeHookExec(fc, fv, tv)
if err != nil {
return nil, err
}
fv = reflect.ValueOf(data)
if fv.Kind() == reflect.Slice && tv.Kind() != reflect.Slice {
if fv.Len() > 0 {
fv = fv.Index(0)
return mapstructure.DecodeHookExec(fc, fv, tv)
}
return nil, nil
}
return data, nil
}
}
// BinaryUnmarshalerHookFunc returns a DecodeHookFunc that applies
// strings to the UnmarshalBinary function, when the target type
// implements the encoding.BinaryUnmarshaler interface
func BinaryUnmarshalerHookFunc() mapstructure.DecodeHookFuncType {
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}
result := reflect.New(t).Interface()
unmarshaller, ok := result.(encoding.BinaryUnmarshaler)
if !ok {
return data, nil
}
if err := unmarshaller.UnmarshalBinary([]byte(data.(string))); err != nil {
return nil, err
}
return result, nil
}
}