-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.go
126 lines (118 loc) · 2.24 KB
/
url.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
package kit
import (
"bytes"
"net/url"
"sort"
)
// func SortedKeys(m *map[string]string) []string {
// sortedKeys := make([]string, len(*m))
// i := 0
// for key := range *m {
// sortedKeys[i] = key
// i++
// }
// sort.Strings(sortedKeys)
// return sortedKeys
// }
// func SortedUrl(m *map[string]string) string {
// if len(*m) == 0 {
// return ""
// }
// sk := SortedKeys(m)
// var sortedData string
// for _, k := range sk {
// sortedData += "&" + k + "=" + (*m)[k]
// }
// return sortedData[1:]
// }
func JointedUrl(m *map[string]string) string {
if len(*m) == 0 {
return ""
}
var data string
for k, v := range *m {
data += "&" + k + "=" + v
}
return data[1:]
}
/*
escape:
false is for sign
true is for url
*/
func Encode(v *map[string]string, isSign ...bool) string {
var escape bool
if len(isSign) == 0 {
escape = false
} else {
escape = isSign[0]
}
if v == nil {
return ""
}
var buf bytes.Buffer
keys := make([]string, 0, len(*v))
for k := range *v {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
vs := (*v)[k]
if buf.Len() > 0 {
buf.WriteByte('&')
}
if escape {
buf.WriteString(url.QueryEscape(k))
buf.WriteByte('=')
buf.WriteString(url.QueryEscape(vs))
} else {
buf.WriteString(k)
buf.WriteByte('=')
buf.WriteString(vs)
}
}
return buf.String()
}
func EncodeObject(v *map[string]interface{}, isSign ...bool) string {
var escape bool
if len(isSign) == 0 {
escape = false
} else {
escape = isSign[0]
}
if v == nil {
return ""
}
var buf bytes.Buffer
keys := make([]string, 0, len(*v))
for k := range *v {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
vs := (*v)[k]
if buf.Len() > 0 {
buf.WriteByte('&')
}
if escape {
buf.WriteString(url.QueryEscape(k))
buf.WriteByte('=')
if tValue, ok := vs.(string); ok {
buf.WriteString(url.QueryEscape(tValue))
} else if tValue, ok := vs.([]byte); ok {
buf.Write(tValue)
}
//buf.Write(url.QueryEscape(vs))
} else {
buf.WriteString(k)
buf.WriteByte('=')
if tValue, ok := vs.(string); ok {
buf.WriteString(tValue)
} else if tValue, ok := vs.([]byte); ok {
buf.Write(tValue)
}
//buf.WriteString(vs)
}
}
return buf.String()
}