-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
104 lines (94 loc) · 2.03 KB
/
params.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
package ghttp
import (
"bytes"
"net/url"
)
type Params struct {
params []*Param
}
type Param struct {
Key string
Value []string
}
// Get gets the first value associated with the given key.
// If there are no values associated with the key, Get returns
// the empty string. To access multiple values, use the map
// directly.
func (v *Params) Get(key string) string {
for _, param := range v.params {
if param.Key == key {
if len(param.Value) == 0 {
return ""
}
return param.Value[0]
}
}
return ""
}
// Has exists the key to value.
func (v *Params) Has(key string) bool {
for _, param := range v.params {
if param.Key == key {
return true
}
}
return false
}
// Index gets the index by key.
func (v *Params) Index(key string) int {
for i, param := range v.params {
if param.Key == key {
return i
}
}
return -1
}
// Set sets the key to value. It replaces any existing
// values.
func (v *Params) Set(key, value string) {
if i := v.Index(key); i != -1 {
v.params[i].Value = []string{value}
} else {
v.params = append(v.params, &Param{
Key: key,
Value: []string{value},
})
}
}
// Add adds the value to key. It appends to any existing
// values associated with key.
func (v *Params) Add(key, value string) {
if i := v.Index(key); i != -1 {
v.params[i].Value = append(v.params[i].Value, value)
} else {
v.params = append(v.params, &Param{
Key: key,
Value: []string{value},
})
}
}
// Del deletes the values associated with key.
func (v *Params) Del(key string) {
if i := v.Index(key); i != -1 {
v.params = append(v.params[:i], v.params[i+1:]...)
}
}
// Encode encodes the values into ``URL encoded'' form
// ("bar=baz&foo=quux") sorted by key.
func (v Params) Encode() string {
if v.params == nil {
return ""
}
var buf bytes.Buffer
for _, param := range v.params {
prefix := url.QueryEscape(param.Key) + "="
for _, v := range param.Value {
if buf.Len() > 0 {
buf.WriteByte('&')
}
buf.WriteString(prefix)
buf.WriteString(url.QueryEscape(v))
}
}
return buf.String()
}