-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
helper.go
166 lines (147 loc) · 3.29 KB
/
helper.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
165
166
package gorequests
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/url"
"reflect"
"strconv"
"strings"
"sync"
)
func newFileUploadRequest(params map[string]string, filekey, filename string, reader io.Reader) (string, io.Reader, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(filekey, filename)
if err != nil {
return "", nil, err
}
if reader != nil {
if _, err = io.Copy(part, reader); err != nil {
return "", nil, err
}
}
for key, val := range params {
if err = writer.WriteField(key, val); err != nil {
return "", nil, err
}
}
if err = writer.Close(); err != nil {
return "", nil, err
}
return writer.FormDataContentType(), body, nil
}
func queryToMap(v interface{}) (map[string][]string, error) {
ss, err := getQueryToMapKeys(v)
if err != nil {
return nil, err
} else if len(ss) == 0 {
return map[string][]string{}, nil
}
vv := reflect.ValueOf(v)
if vv.Kind() == reflect.Ptr {
vv = vv.Elem()
}
vals := map[string][]string{}
for _, s := range ss {
vals[s.query], err = toStringList(vv.Field(s.idx))
if err != nil {
return nil, err
}
}
return vals, nil
}
func toStringList(v reflect.Value) ([]string, error) {
switch v.Kind() {
case reflect.String:
return []string{v.String()}, nil
case reflect.Bool:
if v.Bool() {
return []string{"true"}, nil
}
return []string{"false"}, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return []string{strconv.FormatInt(v.Int(), 10)}, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return []string{strconv.FormatUint(v.Uint(), 10)}, nil
case reflect.Array, reflect.Slice:
res := []string{}
for j := 0; j < v.Len(); j++ {
x, err := toStringList(v.Index(j))
if err != nil {
return nil, err
}
res = append(res, x...)
}
return res, nil
}
return nil, fmt.Errorf("invalid value: %s", v.Kind())
}
func getQueryToMapKeys(v interface{}) ([]s, error) {
origin := reflect.TypeOf(v)
v, ok := queryToMapKeys.Load(origin)
if ok {
return v.([]s), nil
}
vt := origin
// vv := reflect.ValueOf(v)
if vt.Kind() == reflect.Ptr {
vt = vt.Elem()
// vv = vv.Elem()
}
if vt.Kind() != reflect.Struct {
return nil, fmt.Errorf("need strcut, but got %s", vt.Kind())
}
ss := []s{}
for i := 0; i < vt.NumField(); i++ {
itemT := vt.Field(i)
// itemV := vv.Field(i)
queryKey := itemT.Tag.Get("query")
if queryKey == "" {
continue
}
// itemV.String()
ss = append(ss, s{
idx: i,
query: queryKey,
})
}
queryToMapKeys.Store(origin, ss)
return ss, nil
}
// request url
func (r *Request) parseRequestURL() string {
URL, err := url.Parse(r.url)
if err != nil {
return r.url
}
q := URL.Query()
for k, v := range r.querys {
q[k] = append(q[k], v...)
}
URL.RawQuery = q.Encode()
return URL.String()
}
func toBody(body interface{}) ([]byte, io.Reader, error) {
switch v := body.(type) {
case io.Reader:
return nil, v, nil
case []byte:
return v, bytes.NewReader(v), nil
case string:
return []byte(v), strings.NewReader(v), nil
default:
bs, err := json.Marshal(body)
if err != nil {
return nil, nil, err
}
return bs, bytes.NewReader(bs), nil
}
}
type s struct {
idx int
query string
}
var queryToMapKeys sync.Map