-
Notifications
You must be signed in to change notification settings - Fork 2
/
query.go
302 lines (262 loc) · 7.12 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package go4th
import (
"fmt"
)
// Query is a global object just to give a method scope
type Query struct {
Query interface{} `json:"query,omitempty"`
}
// Eq will construct a equal query
type Eq struct {
Field string `json:"_field,omitempty"`
Value interface{} `json:"_value,omitempty"`
}
// Gt will construct a greater than query
type Gt struct {
Gt map[string]interface{} `json:"_gt,omitempty"`
}
// Gte will construct a greater than equal query
type Gte struct {
Gte map[string]interface{} `json:"_gte,omitempty"`
}
// Lt will construct a less than query
type Lt struct {
Lt map[string]interface{} `json:"_lt,omitempty"`
}
// Lte will construct a less than equal query
type Lte struct {
Lte map[string]interface{} `json:"_lte,omitempty"`
}
// And will construct a logical and query
type And struct {
And []interface{} `json:"_and,omitempty"`
}
// Or will construct a logical or query
type Or struct {
Or []interface{} `json:"_or,omitempty"`
}
// Not will construct a logical not query
type Not struct {
Not interface{} `json:"_not,omitempty"`
}
// In will construct a in query
type In struct {
In struct {
Field string `json:"_field,omitempty"`
Value interface{} `json:"_value,omitempty"`
} `json:"_in,omitempty"`
}
// Contains will construct a contains query
type Contains struct {
Contains interface{} `json:"_contains,omitempty"`
}
// ID will construct a query to filter by ID
type ID struct {
ID interface{} `json:"_id,omitempty"`
}
// IBetweenD will construct a query to filter a field
type Between struct {
Between struct {
Field string `json:"_field,omitempty"`
From interface{} `json:"_from,omitempty"`
To interface{} `json:"_to,omitempty"`
} `json:"_between,omitempty"`
}
// ParentID will construct a query to filter by parent ID
type ParentID struct {
ParentID struct {
Type interface{} `json:"_type,omitempty"`
ID interface{} `json:"_id,omitempty"`
} `json:"_parent,omitempty"`
}
// Parent will construct a query to filter by parent
type Parent struct {
Parent struct {
Type interface{} `json:"_type,omitempty"`
Query interface{} `json:"_query,omitempty"`
} `json:"_parent,omitempty"`
}
// Child will construct a query to filter by child
type Child struct {
Child struct {
Type interface{} `json:"_type,omitempty"`
Query interface{} `json:"_query,omitempty"`
} `json:"_child,omitempty"`
}
// Type will construct a query to filter by type
type Type struct {
Type interface{} `json:"_type,omitempty"`
}
// Status will construct a query to filter by status
type Status struct {
Status interface{} `json:"status,omitempty"`
}
// String will construct a query to filter by string
type String struct {
String interface{} `json:"_string,omitempty"`
}
// NewQuery returns a new pointer to Query whichs is used to build up a query
func NewQuery() *Query {
return &Query{}
}
// BuildQuery returns a new query ready to be used
func BuildQuery(q interface{}) *Query {
return &Query{Query: q}
}
// Eq returns a Eq object
func (q *Query) Eq(field string, value interface{}) (Eq, error) {
if field == "" {
return Eq{}, fmt.Errorf("field could not be empty")
}
return Eq{field, value}, nil
}
// Gt returns a Gt object
func (q *Query) Gt(field string, value interface{}) (Gt, error) {
if field == "" {
return Gt{}, fmt.Errorf("field could not be empty")
}
gt := Gt{}
gt.Gt = make(map[string]interface{})
gt.Gt[field] = value
return gt, nil
}
// Gte returns a Gte object
func (q *Query) Gte(field string, value interface{}) (Gte, error) {
if field == "" {
return Gte{}, fmt.Errorf("field could not be empty")
}
gte := Gte{}
gte.Gte = make(map[string]interface{})
gte.Gte[field] = value
return gte, nil
}
// Lt returns a Lt object
func (q *Query) Lt(field string, value interface{}) (Lt, error) {
if field == "" {
return Lt{}, fmt.Errorf("field could not be empty")
}
lt := Lt{}
lt.Lt = make(map[string]interface{})
lt.Lt[field] = value
return lt, nil
}
// Lte returns a Lte object
func (q *Query) Lte(field string, value interface{}) (Lte, error) {
if field == "" {
return Lte{}, fmt.Errorf("field could not be empty")
}
lte := Lte{}
lte.Lte = make(map[string]interface{})
lte.Lte[field] = value
return lte, nil
}
// And returns a And object
func (q *Query) And(query ...interface{}) (And, error) {
and := And{}
for _, q := range query {
and.And = append(and.And, q)
}
return and, nil
}
// Or returns a Or object
func (q *Query) Or(query ...interface{}) (Or, error) {
or := Or{}
for _, q := range query {
or.Or = append(or.Or, q)
}
return or, nil
}
// Not returns a Not object
func (q *Query) Not(not interface{}) (Not, error) {
return Not{not}, nil
}
// In returns a In object
func (q *Query) In(field string, value interface{}) (In, error) {
if field == "" {
return In{}, fmt.Errorf("field could not be empty")
}
var subIn struct {
Field string `json:"_field,omitempty"`
Value interface{} `json:"_value,omitempty"`
}
subIn.Field = field
subIn.Value = value
return In{subIn}, nil
}
// Contains returns a Contains object
func (q *Query) Contains(contains interface{}) (Contains, error) {
return Contains{contains}, nil
}
// ID returns a ID object
func (q *Query) ID(id interface{}) (ID, error) {
return ID{id}, nil
}
// Between returns a Between object
func (q *Query) Between(field string, from, to interface{}) (Between, error) {
if field == "" {
return Between{}, fmt.Errorf("field could not be empty")
}
var subBetween struct {
Field string `json:"_field,omitempty"`
From interface{} `json:"_from,omitempty"`
To interface{} `json:"_to,omitempty"`
}
subBetween.Field = field
subBetween.From = from
subBetween.To = to
return Between{subBetween}, nil
}
// ParentID returns a ParentID object
func (q *Query) ParentID(typ, id interface{}) (ParentID, error) {
if typ == nil {
return ParentID{}, fmt.Errorf("type could not be nil")
}
var subParentID struct {
Type interface{} `json:"_type,omitempty"`
ID interface{} `json:"_id,omitempty"`
}
subParentID.Type = typ
subParentID.ID = id
return ParentID{subParentID}, nil
}
// Parent returns a Parent object
func (q *Query) Parent(typ, query interface{}) (Parent, error) {
if typ == nil {
return Parent{}, fmt.Errorf("type could not be nil")
}
var subParent struct {
Type interface{} `json:"_type,omitempty"`
Query interface{} `json:"_query,omitempty"`
}
subParent.Type = typ
subParent.Query = query
return Parent{subParent}, nil
}
// Child returns a Child object
func (q *Query) Child(typ, query interface{}) (Child, error) {
if typ == nil {
return Child{}, fmt.Errorf("type could not be nil")
}
var subChild struct {
Type interface{} `json:"_type,omitempty"`
Query interface{} `json:"_query,omitempty"`
}
subChild.Type = typ
subChild.Query = query
return Child{subChild}, nil
}
// Type returns a Type object
func (q *Query) Type(typ interface{}) (Type, error) {
if typ == nil {
return Type{}, fmt.Errorf("type could not be nil")
}
return Type{typ}, nil
}
// Status returns a Status object
func (q *Query) Status(str interface{}) (Status, error) {
return Status{str}, nil
}
// String returns a String object
func (q *Query) String(str interface{}) (String, error) {
return String{str}, nil
}