-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.go
169 lines (153 loc) · 3.18 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
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
167
168
169
/*
Package util util consists of general utility functions and structures.
Copyright 2021 Michael Bungenstock
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
package util
import (
"net/url"
"sort"
"strings"
)
// Par takes a list of name value pairs and builds a url parameter list
func Par(pairs ...string) string {
l := len(pairs)
if l%2 != 0 {
l--
}
if l == 0 {
return ""
}
sb := strings.Builder{}
sep := "?"
for i := 0; i < l; {
if pairs[i+1] != "" {
sb.WriteString(sep)
sb.WriteString(pairs[i])
i++
sb.WriteByte('=')
sb.WriteString(url.QueryEscape(pairs[i]))
i++
sep = "&"
} else {
i = i + 2
}
}
a := sb.String()
return a
}
// GetParam returns the first parameter value with given name from the passed
// url
func GetParam(str, name string) (string, error) {
u, err := url.Parse(str)
if err != nil {
return "", err
}
return u.Query().Get(name), nil
}
type Params map[string][]string
func NewParams(pairs ...string) Params {
result := make(Params, len(pairs)/2)
result.Set(pairs...)
return result
}
func (p Params) Add(name, value string) {
p[name] = append(p[name], value)
}
func (p Params) Set(pairs ...string) {
l := len(pairs)
if l%2 != 0 {
l--
}
if l == 0 {
return
}
for i := 0; i < l; i = i + 2 {
if pairs[i+1] != "" {
p.Add(pairs[i], pairs[i+1])
}
}
}
func (p Params) Get(key string) string {
s := p[key]
if len(s) > 0 {
return s[0]
}
return ""
}
func (p Params) GetForPath(key string) string {
return url.PathEscape(p.Get(key))
}
// Encode builds an URL query
func (p Params) Encode() string {
if p == nil {
return ""
}
var buf strings.Builder
keys := make([]string, len(p))
i := 0
for k := range p {
keys[i] = k
i++
}
sort.Strings(keys)
sep := byte('?')
for _, k := range keys {
vs := p[k]
keyEscaped := url.QueryEscape(k)
for _, v := range vs {
if v != "" {
buf.WriteByte(sep)
sep = byte('&')
buf.WriteString(keyEscaped)
buf.WriteByte('=')
buf.WriteString(url.QueryEscape(v))
}
}
}
return buf.String()
}
// EncodeWithout builds an URL query without the passed parameters
func (p Params) EncodeWithout(names ...string) string {
if p == nil {
return ""
}
var buf strings.Builder
keys := make([]string, 0, len(p))
var found bool
for k := range p {
found = false
for _, n := range names {
if n == k {
found = true
break
}
}
if !found {
keys = append(keys, k)
}
}
sort.Strings(keys)
sep := byte('?')
for _, k := range keys {
vs := p[k]
keyEscaped := url.QueryEscape(k)
for _, v := range vs {
if v != "" {
buf.WriteByte(sep)
sep = byte('&')
buf.WriteString(keyEscaped)
buf.WriteByte('=')
buf.WriteString(url.QueryEscape(v))
}
}
}
return buf.String()
}