-
Notifications
You must be signed in to change notification settings - Fork 60
/
util.go
72 lines (64 loc) · 1.65 KB
/
util.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
package luno
import (
"fmt"
"net/url"
"reflect"
"strconv"
)
// makeURLValues converts a request struct into a url.Values map.
func makeURLValues(v interface{}) url.Values {
values := make(url.Values)
if v == nil {
return values
}
val := reflect.ValueOf(v)
if val.IsZero() {
return values
}
valElem := val.Elem()
typElem := reflect.TypeOf(v).Elem()
for i := 0; i < typElem.NumField(); i++ {
field := typElem.Field(i)
urlTag := field.Tag.Get("url")
if urlTag == "" || urlTag == "-" {
continue
}
fieldValue := valElem.Field(i)
stringer, ok := fieldValue.Interface().(QueryValuer)
if ok {
values.Set(urlTag, stringer.QueryValue())
continue
}
k := fieldValue.Kind()
ss := make([]string, 0)
switch k {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Int64:
ss = append(ss, strconv.FormatInt(fieldValue.Int(), 10))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
reflect.Uint64:
ss = append(ss, strconv.FormatUint(fieldValue.Uint(), 10))
case reflect.Float32:
ss = append(ss, strconv.FormatFloat(fieldValue.Float(), 'f', 4, 32))
case reflect.Float64:
ss = append(ss, strconv.FormatFloat(fieldValue.Float(), 'f', 4, 64))
case reflect.Slice:
if field.Type.Elem().Kind() == reflect.String {
for i := 0; i < fieldValue.Len(); i++ {
ss = append(ss, fieldValue.Index(i).String())
}
}
case reflect.String:
ss = append(ss, fieldValue.String())
case reflect.Bool:
ss = append(ss, fmt.Sprintf("%v", fieldValue.Bool()))
}
for _, str := range ss {
values.Add(urlTag, str)
}
}
return values
}
type QueryValuer interface {
QueryValue() string
}