-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
187 lines (171 loc) · 5.39 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package stgin
import (
"errors"
"reflect"
"regexp"
"strconv"
"strings"
)
type queryDecl = map[string]string
var intQueryRegex = regexp.MustCompile("^" + intRegexStr + "$")
var floatQueryRegex = regexp.MustCompile("^" + floatRegexStr + "$")
var strQueryRegex = regexp.MustCompile(".*")
// Queries is just a struct holding all the key value pairs of request's query parameters.
// It also defines some useful receiver functions in order to ease fetching query params.
type Queries struct {
All map[string][]string
}
// Get looks for the given key in all queries, and returns the value if it exists.
func (q Queries) Get(key string) ([]string, bool) {
values, found := q.All[key]
return values, found
}
// GetOne is used when you're sure if one and only one value exists for the given query.
func (q Queries) GetOne(key string) (string, bool) {
all := q.All[key]
if len(all) == 1 {
return all[0], true
} else {
return "", false
}
}
// MustGet can be used when you're sure about the existence of a key in query parameters.
// It panics in it has 0 or more than 1 values.
func (q Queries) MustGet(key string) string {
if value, ok := q.GetOne(key); ok {
return value
} else {
panic("query parameter entry " + key + " had either 0 or more than 1 value (must've been exactly one)")
}
}
// GetInt is the same as Get, but when you have defined query type to be integer inside the route pattern.
// In case of any error from finding to converting, the error is returned immediately.
func (q Queries) GetInt(key string) (int, error) {
stringed, ok := q.GetOne(key)
if !ok {
return 0, errors.New("query parameter entry " + key + " had either 0 or more than 1 value (must've been exactly one)")
}
return strconv.Atoi(stringed)
}
// MustGetInt is the same as MustGet, but when you have defined query type to be integer inside the route pattern.
// It panics in case of any error from finding to converting the value to int.
func (q Queries) MustGetInt(key string) int {
value, err := q.GetInt(key)
if err != nil {
panic(err)
}
return value
}
// GetFloat is the same as Get, but when you have defined query type to be floating point inside the route pattern.
// In case of any error from finding to converting, the error is returned immediately.
func (q Queries) GetFloat(key string) (float64, error) {
stringed, ok := q.GetOne(key)
if !ok {
return 0, errors.New("query parameter entry " + key + " had either 0 or more than 1 value (must've been exactly one)")
}
return strconv.ParseFloat(stringed, 64)
}
// MustGetFloat is the same as MustGet, but when you have defined query type to be floating point inside the route pattern.
// It panics in case of any error from finding to converting the value to float.
func (q Queries) MustGetFloat(key string) float64 {
value, err := q.GetFloat(key)
if err != nil {
panic(err)
}
return value
}
func getQueryMatcher(tpe string) *regexp.Regexp {
switch tpe {
case "int":
return intQueryRegex
case "float":
return floatQueryRegex
default:
return strQueryRegex
}
}
func acceptsQuery(tpe string, value string) bool {
if tpe == "" {
return true
} else {
return getQueryMatcher(tpe).Match([]byte(value))
}
}
func acceptsAllQueries(declarations queryDecl, qs map[string][]string) bool {
var accepts = true
for name, tpe := range declarations {
value := qs[name]
if len(value) == 0 {
accepts = false
break
}
for _, v := range value {
if v == "" || !acceptsQuery(tpe, v) {
accepts = false
break
}
}
}
return accepts
}
func getQueryDefinitionsFromPattern(pattern string) queryDecl {
defs := strings.SplitN(pattern, "&", -1)
qs := make(queryDecl, 10)
for _, def := range defs {
if def != "" {
arr := strings.SplitN(def, ":", 2)
name := arr[0]
var tpe = "string"
if len(arr) == 2 {
tpe = arr[1]
}
qs[name] = tpe
}
}
return qs
}
// QueryToObj receives a pointer to a struct, and tries to parse the query params into it.
// Please read the documentations [here](https://github.com/AminMal/stgin#query-parameters) for more details.
func (request RequestContext) QueryToObj(a any) error {
if reflect.TypeOf(a).Kind() != reflect.Ptr {
return errors.New("passed raw type instead of value pointer to QueryToObj function, please use pointers instead")
}
if reflect.ValueOf(a).Kind() != reflect.Ptr {
return errors.New("passed raw value instead of value pointer to QueryToObj function, please use pointers instead")
}
tpe := reflect.TypeOf(a).Elem()
value := reflect.ValueOf(a).Elem()
for i := 0; i < tpe.NumField(); i++ {
tpeField := tpe.Field(i)
if tpeField.IsExported() {
queryTag, _ := tpeField.Tag.Lookup("qp")
if queryTag == "" {
queryTag = tpeField.Name
}
tags := strings.SplitN(queryTag, ",", -1)
queryName := tags[0]
//otherTags := tags[1:] WIP
query, found := request.QueryParams.GetOne(queryName)
valueField := value.Field(i)
if found {
switch valueField.Kind() {
case reflect.String:
valueField.Set(reflect.ValueOf(query))
case reflect.Int:
if queryInt, err := strconv.Atoi(query); err == nil {
valueField.Set(reflect.ValueOf(queryInt))
}
case reflect.Float64:
if queryFloat, err := strconv.ParseFloat(query, 64); err == nil {
valueField.Set(reflect.ValueOf(queryFloat))
}
case reflect.Float32:
if queryFloat, err := strconv.ParseFloat(query, 32); err == nil {
valueField.Set(reflect.ValueOf(queryFloat))
}
}
}
}
}
return nil
}