-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.go
92 lines (72 loc) · 1.55 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
package talkback
import (
"net/url"
"strconv"
"strings"
)
// FromQueryString returns a Query from a query string.
func FromQueryString(qs string) (Query, error) {
params, err := url.ParseQuery(qs)
if err != nil {
return Query{}, err
}
return FromURLValues(params)
}
// FromQueryString returns a Query from a query string.
func FromURLValues(params url.Values) (Query, error) {
query := Query{}
for key, values := range params {
spliten := strings.Split(key, "_")
if len(spliten) < 2 {
continue
}
field := strings.Join(spliten[:len(spliten)-1], "_")
op := spliten[len(spliten)-1]
cond := Condition{
Field: field,
Op: op,
Values: values,
}
if cond.Valid() {
query.Conditions = append(query.Conditions, cond)
}
}
if with, ok := params["with"]; ok {
query.With = with
}
if group, ok := params["group"]; ok {
query.Group = group
}
if accumulator, ok := params["accumulator"]; ok {
query.Accumulator = accumulator
}
if sort, ok := params["sort"]; ok {
for _, s := range sort {
field := s
reverse := false
if strings.HasPrefix(field, "-") {
field = field[1:]
reverse = true
}
query.Sort = append(query.Sort, Sort{
Field: field,
Reverse: reverse,
})
}
}
if limit := params.Get("limit"); limit != "" {
var err error
query.Limit, err = strconv.Atoi(limit)
if err != nil {
return Query{}, err
}
}
if skip := params.Get("skip"); skip != "" {
var err error
query.Skip, err = strconv.Atoi(skip)
if err != nil {
return Query{}, err
}
}
return query, nil
}