-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttputils.go
More file actions
96 lines (93 loc) · 2.18 KB
/
httputils.go
File metadata and controls
96 lines (93 loc) · 2.18 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
// httputils
package utils
import (
"fmt"
"net/url"
"reflect"
"strconv"
)
func StructToURLQuery(i interface{}, hasSubKey bool) (string, error) {
iValue := reflect.ValueOf(i)
if iValue.Kind() == reflect.Ptr {
iValue = iValue.Elem()
}
if iValue.Kind() != reflect.Struct {
return "", fmt.Errorf("utils:准备转换的对象不是Struct类型,实际类型为:%s", iValue.Kind())
}
values := url.Values{}
setValues(iValue, values, "", hasSubKey)
return values.Encode(), nil
}
func setValues(iValue reflect.Value, values url.Values, key string, hasSubKey bool) {
if iValue.Kind() == reflect.Ptr {
iValue = iValue.Elem()
}
switch iValue.Kind() {
case reflect.String:
values.Add(key, iValue.String())
break
case reflect.Bool:
if iValue.Bool() {
values.Add(key, "true")
} else {
values.Add(key, "false")
}
break
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int8:
values.Add(key, strconv.FormatInt(iValue.Int(), 10))
break
case reflect.Float32, reflect.Float64:
values.Add(key, strconv.FormatFloat(iValue.Float(), 'f', 2, 64))
break
case reflect.Struct:
iType := iValue.Type()
fieldSize := iValue.NumField()
if key != "" {
key = key + "."
}
for i := 0; i < fieldSize; i++ {
fieldValue := iValue.Field(i)
if !fieldValue.IsValid() {
continue
}
fieldType := iType.Field(i)
subkey := fieldType.Name
tag := fieldType.Tag.Get("json")
if tag != "" {
subkey = tag
}
if hasSubKey {
subkey = key + subkey
}
setValues(fieldValue, values, subkey, hasSubKey)
}
break
case reflect.Array, reflect.Slice:
arraySize := iValue.Len()
for index := 0; index < arraySize; index++ {
subValue := iValue.Index(index)
setValues(subValue, values, key, hasSubKey)
}
break
case reflect.Map:
mapKeys := iValue.MapKeys()
if key != "" {
key = key + "."
}
for _, mapkey := range mapKeys {
if mapkey.Type().Kind() != reflect.String {
//日过map的KEY不是string类型则忽略
continue
}
fieldValue := iValue.MapIndex(mapkey)
subkey := mapkey.String()
if hasSubKey {
subkey = key + subkey
}
setValues(fieldValue, values, subkey, hasSubKey)
}
break
default:
break
}
}