-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery.go
73 lines (58 loc) · 1.69 KB
/
query.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
package waggy
import (
"github.com/syke99/waggy/internal/resources"
"net/http"
)
// QueryParams contains a map containing all the query params from the provided *http.Request
type QueryParams struct {
qp map[string][]string
}
// Query returns QueryParams from the provided *http.Request
func Query(r *http.Request) *QueryParams {
if rv := r.Context().Value(resources.QueryParams); rv != nil {
qp := rv.(map[string][]string)
q := QueryParams{
qp: qp,
}
return &q
}
return nil
}
// Get returns the first value stored in q with the provided key
func (q *QueryParams) Get(key string) string {
v, ok := q.qp[key]
if !ok {
return ""
}
return v[0]
}
// Set sets the provided val in q with the provided key. If an existing
// value/set of values is already stored at the provided key, then Set
// will override that value
func (q *QueryParams) Set(key string, val string) {
if _, ok := q.qp[key]; ok {
delete(q.qp, key)
}
newSlice := append(make([]string, 0), val)
q.qp[key] = newSlice
}
// Add either appends the provided val at the provided key stored in q, or,
// if no value is currently stored at the provided key, then a new slice will
// be stored at the provided key and the provided val appended to it
func (q *QueryParams) Add(key string, val string) {
if _, ok := q.qp[key]; !ok {
q.qp[key] = make([]string, 0)
}
q.qp[key] = append(q.qp[key], val)
}
// Del deletes the value(s) stored in q at the provided key
func (q *QueryParams) Del(key string) {
delete(q.qp, key)
}
// Values returns a slice of all values stored in q with the provided key
func (q *QueryParams) Values(key string) []string {
if _, ok := q.qp[key]; !ok {
return make([]string, 0)
}
return q.qp[key]
}