-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
134 lines (121 loc) · 2.56 KB
/
utils.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
package quest
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/url"
"os"
"strings"
goquery "github.com/google/go-querystring/query"
)
func nopCloser(r io.Reader) io.ReadCloser {
return ioutil.NopCloser(r)
}
func packBodyByString(s string) (io.ReadCloser, int64) {
return nopCloser(bytes.NewBufferString(s)), int64(len(s))
}
func packBodyByBytes(b []byte) (io.ReadCloser, int64) {
return nopCloser(bytes.NewBuffer(b)), int64(len(b))
}
func packBodyByBytesBuffer(b *bytes.Buffer) (io.ReadCloser, int64) {
return nopCloser(b), int64(b.Len())
}
func packBodyByBytesReader(b *bytes.Reader) (io.ReadCloser, int64) {
return nopCloser(b), int64(b.Len())
}
func packBodyByPipeReader(pr *io.PipeReader) (io.ReadCloser, int64) {
b := new(bytes.Buffer)
length, _ := b.ReadFrom(pr)
return nopCloser(b), length
}
func packBodyByReader(pr io.Reader) (io.ReadCloser, int64) {
b := new(bytes.Buffer)
length, _ := b.ReadFrom(pr)
return nopCloser(b), length
}
func packBodyByStringsReader(b *strings.Reader) (io.ReadCloser, int64) {
return nopCloser(b), int64(b.Len())
}
// Pack Request's body to io.ReadCloser.
func packBody(data interface{}) (rc io.ReadCloser, n int64, err error) {
switch t := data.(type) {
case nil:
return
case string:
rc, n = packBodyByString(t)
return
case []byte:
rc, n = packBodyByBytes(t)
return
case *url.Values:
rc, n = packBodyByString(t.Encode())
return
case *bytes.Buffer:
rc, n = packBodyByBytesBuffer(t)
return
case *bytes.Reader:
rc, n = packBodyByBytesReader(t)
return
case *strings.Reader:
rc, n = packBodyByStringsReader(t)
return
case *io.PipeReader:
rc, n = packBodyByPipeReader(t)
return
// JSON Object
default:
b, err := json.Marshal(data)
if err != nil {
return nil, 0, err
}
rc, n = packBodyByBytes(b)
}
return
}
func QueryString(options interface{}) (qs string, err error) {
switch t := options.(type) {
case string:
qs = url.QueryEscape(t)
return
case []byte:
qs = url.QueryEscape(string(t))
return
case *url.Values:
qs = t.Encode()
return
default:
v, err := goquery.Values(t)
if err != nil {
return "", err
}
qs = v.Encode()
}
return
}
func getFile(f interface{}) (file *os.File, name string, err error) {
switch t := f.(type) {
case string:
name = t
file, err = os.Open(name)
break
case *os.File:
var fs os.FileInfo
file = t
fs, err = t.Stat()
name = fs.Name()
break
case os.FileInfo:
name = t.Name()
file, err = os.Open(name)
break
default:
err = errors.New("Not a file.")
}
if err != nil {
file = nil
name = ""
}
return
}