1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package query
22
23 import (
24 "bytes"
25 "fmt"
26 "net/url"
27 "reflect"
28 "strconv"
29 "strings"
30 "time"
31 )
32
33 var timeType = reflect.TypeOf(time.Time{})
34
35 var encoderType = reflect.TypeOf(new(Encoder)).Elem()
36
37
38
39 type Encoder interface {
40 EncodeValues(key string, v *url.Values) error
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 func Values(v interface{}) (url.Values, error) {
127 values := make(url.Values)
128 val := reflect.ValueOf(v)
129 for val.Kind() == reflect.Ptr {
130 if val.IsNil() {
131 return values, nil
132 }
133 val = val.Elem()
134 }
135
136 if v == nil {
137 return values, nil
138 }
139
140 if val.Kind() != reflect.Struct {
141 return nil, fmt.Errorf("query: Values() expects struct input. Got %v", val.Kind())
142 }
143
144 err := reflectValue(values, val, "")
145 return values, err
146 }
147
148
149
150
151 func reflectValue(values url.Values, val reflect.Value, scope string) error {
152 var embedded []reflect.Value
153
154 typ := val.Type()
155 for i := 0; i < typ.NumField(); i++ {
156 sf := typ.Field(i)
157 if sf.PkgPath != "" && !sf.Anonymous {
158 continue
159 }
160
161 sv := val.Field(i)
162 tag := sf.Tag.Get("url")
163 if tag == "-" {
164 continue
165 }
166 name, opts := parseTag(tag)
167
168 if name == "" {
169 if sf.Anonymous {
170 v := reflect.Indirect(sv)
171 if v.IsValid() && v.Kind() == reflect.Struct {
172
173 embedded = append(embedded, v)
174 continue
175 }
176 }
177
178 name = sf.Name
179 }
180
181 if scope != "" {
182 name = scope + "[" + name + "]"
183 }
184
185 if opts.Contains("omitempty") && isEmptyValue(sv) {
186 continue
187 }
188
189 if sv.Type().Implements(encoderType) {
190
191
192 if !reflect.Indirect(sv).IsValid() && sv.Type().Elem().Implements(encoderType) {
193 sv = reflect.New(sv.Type().Elem())
194 }
195
196 m := sv.Interface().(Encoder)
197 if err := m.EncodeValues(name, &values); err != nil {
198 return err
199 }
200 continue
201 }
202
203
204 for sv.Kind() == reflect.Ptr {
205 if sv.IsNil() {
206 break
207 }
208 sv = sv.Elem()
209 }
210
211 if sv.Kind() == reflect.Slice || sv.Kind() == reflect.Array {
212 var del string
213 if opts.Contains("comma") {
214 del = ","
215 } else if opts.Contains("space") {
216 del = " "
217 } else if opts.Contains("semicolon") {
218 del = ";"
219 } else if opts.Contains("brackets") {
220 name = name + "[]"
221 } else {
222 del = sf.Tag.Get("del")
223 }
224
225 if del != "" {
226 s := new(bytes.Buffer)
227 first := true
228 for i := 0; i < sv.Len(); i++ {
229 if first {
230 first = false
231 } else {
232 s.WriteString(del)
233 }
234 s.WriteString(valueString(sv.Index(i), opts, sf))
235 }
236 values.Add(name, s.String())
237 } else {
238 for i := 0; i < sv.Len(); i++ {
239 k := name
240 if opts.Contains("numbered") {
241 k = fmt.Sprintf("%s%d", name, i)
242 }
243 values.Add(k, valueString(sv.Index(i), opts, sf))
244 }
245 }
246 continue
247 }
248
249 if sv.Type() == timeType {
250 values.Add(name, valueString(sv, opts, sf))
251 continue
252 }
253
254 if sv.Kind() == reflect.Struct {
255 if err := reflectValue(values, sv, name); err != nil {
256 return err
257 }
258 continue
259 }
260
261 values.Add(name, valueString(sv, opts, sf))
262 }
263
264 for _, f := range embedded {
265 if err := reflectValue(values, f, scope); err != nil {
266 return err
267 }
268 }
269
270 return nil
271 }
272
273
274 func valueString(v reflect.Value, opts tagOptions, sf reflect.StructField) string {
275 for v.Kind() == reflect.Ptr {
276 if v.IsNil() {
277 return ""
278 }
279 v = v.Elem()
280 }
281
282 if v.Kind() == reflect.Bool && opts.Contains("int") {
283 if v.Bool() {
284 return "1"
285 }
286 return "0"
287 }
288
289 if v.Type() == timeType {
290 t := v.Interface().(time.Time)
291 if opts.Contains("unix") {
292 return strconv.FormatInt(t.Unix(), 10)
293 }
294 if opts.Contains("unixmilli") {
295 return strconv.FormatInt((t.UnixNano() / 1e6), 10)
296 }
297 if opts.Contains("unixnano") {
298 return strconv.FormatInt(t.UnixNano(), 10)
299 }
300 if layout := sf.Tag.Get("layout"); layout != "" {
301 return t.Format(layout)
302 }
303 return t.Format(time.RFC3339)
304 }
305
306 return fmt.Sprint(v.Interface())
307 }
308
309
310
311 func isEmptyValue(v reflect.Value) bool {
312 switch v.Kind() {
313 case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
314 return v.Len() == 0
315 case reflect.Bool:
316 return !v.Bool()
317 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
318 return v.Int() == 0
319 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
320 return v.Uint() == 0
321 case reflect.Float32, reflect.Float64:
322 return v.Float() == 0
323 case reflect.Interface, reflect.Ptr:
324 return v.IsNil()
325 }
326
327 type zeroable interface {
328 IsZero() bool
329 }
330
331 if z, ok := v.Interface().(zeroable); ok {
332 return z.IsZero()
333 }
334
335 return false
336 }
337
338
339
340 type tagOptions []string
341
342
343
344 func parseTag(tag string) (string, tagOptions) {
345 s := strings.Split(tag, ",")
346 return s[0], s[1:]
347 }
348
349
350 func (o tagOptions) Contains(option string) bool {
351 for _, s := range o {
352 if s == option {
353 return true
354 }
355 }
356 return false
357 }
358
View as plain text