forked from ghetzel/go-stockutil
/
querystrings.go
163 lines (134 loc) · 3.87 KB
/
querystrings.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
package httputil
import (
"net/http"
"net/url"
"strings"
"time"
"github.com/ghetzel/go-stockutil/sliceutil"
"github.com/ghetzel/go-stockutil/stringutil"
"github.com/ghetzel/go-stockutil/timeutil"
"github.com/ghetzel/go-stockutil/typeutil"
)
// Parses the named query string from a request as an integer.
func QInt(req *http.Request, key string, fallbacks ...int64) int64 {
if v := Q(req, key); v != `` {
if i, err := stringutil.ConvertToInteger(v); err == nil {
return i
}
}
if len(fallbacks) > 0 {
return fallbacks[0]
} else {
return 0
}
}
// Parses the named query string from a request as a float.
func QFloat(req *http.Request, key string, fallbacks ...float64) float64 {
if v := Q(req, key); v != `` {
if i, err := stringutil.ConvertToFloat(v); err == nil {
return i
}
}
if len(fallbacks) > 0 {
return fallbacks[0]
} else {
return 0
}
}
// Parses the named query string from a request as a date/time value.
func QTime(req *http.Request, key string) time.Time {
if v := Q(req, key); v != `` {
if i, err := stringutil.ConvertToTime(v); err == nil {
return i
}
}
return time.Time{}
}
// Parses the named query string from a request as a duration string.
func QDuration(req *http.Request, key string) time.Duration {
if v := Q(req, key); v != `` {
if d, err := timeutil.ParseDuration(v); err == nil {
return d
}
}
return 0
}
// Parses the named query string from a request as a boolean value.
func QBool(req *http.Request, key string, fallbacks ...bool) bool {
if v := Q(req, key); v == `` && len(fallbacks) > 0 {
return fallbacks[0]
} else if typeutil.Bool(v) {
return true
}
return false
}
// Parses the named query string from a request as a string.
func Q(req *http.Request, key string, fallbacks ...string) string {
if v := req.URL.Query().Get(key); v != `` {
if vS, err := url.QueryUnescape(v); err == nil {
return vS
}
}
if len(fallbacks) > 0 {
return fallbacks[0]
} else {
return ``
}
}
// Parses the named query string from a request as a delimiter-separated string slice.
func QStrings(req *http.Request, key string, delimiter string, fallbacks ...string) []string {
if strs := sliceutil.CompactString(strings.Split(Q(req, key), delimiter)); len(strs) > 0 {
return strs
} else if len(fallbacks) > 0 {
return sliceutil.Stringify(sliceutil.Flatten(fallbacks))
} else {
return make([]string, 0)
}
}
// Sets a query string to the given value in the given url.URL
func SetQ(u *url.URL, key string, value interface{}) {
qs := u.Query()
qs.Set(key, stringutil.MustString(value))
u.RawQuery = qs.Encode()
}
// Appends a query string from then given url.URL
func AddQ(u *url.URL, key string, values ...interface{}) {
qs := u.Query()
for _, value := range values {
qs.Add(key, stringutil.MustString(value))
}
u.RawQuery = qs.Encode()
}
// Deletes a query string from then given url.URL
func DelQ(u *url.URL, key string) {
qs := u.Query()
qs.Del(key)
u.RawQuery = qs.Encode()
}
// A version of SetQ that accepts a URL string and makes a best-effort to modify it.
// Will return the modified URL or the original URL if an error occurred.
func SetQString(u string, key string, value interface{}) string {
if ur, err := url.Parse(u); err == nil {
SetQ(ur, key, value)
return ur.String()
}
return u
}
// A version of AddQ that accepts a URL string and makes a best-effort to modify it.
// Will return the modified URL or the original URL if an error occurred.
func AddQString(u string, key string, value interface{}) string {
if ur, err := url.Parse(u); err == nil {
AddQ(ur, key, value)
return ur.String()
}
return u
}
// A version of DelQ that accepts a URL string and makes a best-effort to modify it.
// Will return the modified URL or the original URL if an error occurred.
func DelQString(u string, key string) string {
if ur, err := url.Parse(u); err == nil {
DelQ(ur, key)
return ur.String()
}
return u
}