-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
149 lines (124 loc) · 4.13 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
package bongoz
import (
"encoding/json"
"gopkg.in/mgo.v2/bson"
"log"
"net/http"
"strconv"
"strings"
"time"
)
func addIntToQuery(query bson.M, param string, modifier string, value string) {
withoutPrefix := strings.TrimPrefix(param, strings.Join([]string{modifier, "_"}, ""))
parsed, err := strconv.Atoi(value)
if err == nil {
sub := bson.M{}
sub[modifier] = parsed
query[withoutPrefix] = sub
}
}
func addDateToQuery(query bson.M, param string, modifier string, value string) {
withoutPrefix := strings.TrimPrefix(param, strings.Join([]string{modifier, "_"}, ""))
// Remove date from modifier
parsed, err := strconv.Atoi(value)
if err == nil {
i64 := int64(parsed)
t := time.Unix(i64, 0)
sub := bson.M{}
sub[modifier] = t
query[withoutPrefix] = sub
}
}
func addDateOrIntToQuery(instance interface{}, query bson.M, param string, modifier string, value string) {
withoutPrefix := strings.TrimPrefix(param, strings.Join([]string{modifier, "_"}, ""))
if propertyIsType(instance, withoutPrefix, "time.Time") {
addDateToQuery(query, param, modifier, value)
} else {
addIntToQuery(query, param, modifier, value)
}
}
func addValueToQuery(instance interface{}, query bson.M, param string, modifier string, value interface{}) {
addValueToQueryReplacingModifier(instance, query, param, modifier, strings.Join([]string{modifier, "_"}, ""), value)
}
func addValueToQueryReplacingModifier(instance interface{}, query bson.M, param string, modifier string, replace string, value interface{}) {
withoutPrefix := strings.TrimPrefix(param, replace)
sub := bson.M{}
checkForObjectIdAndAddToQuery(instance, withoutPrefix, modifier, value, sub)
query[withoutPrefix] = sub
}
func checkForObjectIdAndAddToQuery(instance interface{}, property string, key string, value interface{}, query bson.M) {
t, err := getFieldTypeByNameOrBsonTag(property, instance)
if err != nil {
log.Fatal(err.Error())
return
}
if t == "bson.ObjectId" {
// Make sure it's valid...
if val, ok := value.(string); ok {
if bson.IsObjectIdHex(val) {
query[key] = bson.ObjectIdHex(val)
} else {
log.Fatalf("Invalid object ID %s", val)
return
}
} else {
log.Println("Could not convert value to string", value)
return
}
} else if t == "[]bson.ObjectId" {
if val, ok := value.([]string); ok {
parsed := make([]bson.ObjectId, 0)
for _, v := range val {
if bson.IsObjectIdHex(v) {
parsed = append(parsed, bson.ObjectIdHex(v))
}
}
query[key] = parsed
} else {
log.Fatal("Could not parse value as []string", value)
return
}
} else {
query[key] = value
}
}
func (e *Endpoint) getQuery(req *http.Request) (bson.M, error) {
query := req.URL.Query()
q := bson.M{}
if e.AllowFullQuery {
// Marshal the query base64 into bson.M
val := query.Get("_query")
if len(val) > 0 {
err := json.Unmarshal([]byte(val), &q)
return q, err
}
}
// Get an instance so we can inspect it with reflection
instance := e.Factory()
for _, param := range e.QueryParams {
if val, ok := query[param]; ok {
if len(val) > 0 {
if strings.HasPrefix(param, "$lt_") {
addDateOrIntToQuery(instance, q, param, "$lt", query.Get(param))
} else if strings.HasPrefix(param, "$gt_") {
addDateOrIntToQuery(instance, q, param, "$gt", query.Get(param))
} else if strings.HasPrefix(param, "$gte_") {
addDateOrIntToQuery(instance, q, param, "$gte", query.Get(param))
} else if strings.HasPrefix(param, "$lte_") {
addDateOrIntToQuery(instance, q, param, "$lte", query.Get(param))
} else if strings.HasPrefix(param, "$in_") {
addValueToQuery(instance, q, param, "$in", val)
} else if strings.HasPrefix(param, "$nin_") {
addValueToQuery(instance, q, param, "$nin_", val)
} else if strings.HasPrefix(param, "$regex_") {
addValueToQuery(instance, q, param, "$regex", bson.RegEx{query.Get(param), ""})
} else if strings.HasPrefix(param, "$regexi_") {
addValueToQueryReplacingModifier(instance, q, param, "$regex", "$regexi_", bson.RegEx{query.Get(param), "i"})
} else {
checkForObjectIdAndAddToQuery(instance, param, param, query.Get(param), q)
}
}
}
}
return q, nil
}