-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathurl.go
67 lines (58 loc) · 968 Bytes
/
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
package economic
import (
"encoding/json"
"fmt"
"net/url"
"strings"
)
type URL url.URL
func NewURL(s string) (*URL, error) {
uu, err := url.Parse(s)
if err != nil {
return nil, fmt.Errorf("bad URL: %v", err)
}
nu := URL(*uu)
return &nu, nil
}
func (u URL) String() string {
uu := url.URL(u)
return uu.String()
}
func (u *URL) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
nu, err := NewURL(s)
if err != nil {
return err
}
*u = *nu
return nil
}
func (u URL) MarshalJSON() ([]byte, error) {
return json.Marshal(u.String())
}
func QueryEscape(s string) string {
mm := map[string]string{
"<": "0",
">": "1",
"*": "2",
"%": "3",
":": "4",
"&": "5",
"/": "6",
"\\": "7",
"_": "8",
" ": "9",
"?": "10",
".": "11",
"#": "12",
"+": "13",
}
for k, v := range mm {
r := "_" + v + "_"
s = strings.Replace(s, k, r, -1)
}
return s
}