forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filtering.go
333 lines (303 loc) · 11 KB
/
filtering.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package gorm
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/generator"
"github.com/infobloxopen/atlas-app-toolkit/query"
"github.com/infobloxopen/atlas-app-toolkit/rpc/resource"
)
// FilterStringToGorm is a shortcut to parse a filter string using default FilteringParser implementation
// and call FilteringToGorm on the returned filtering expression.
func FilterStringToGorm(ctx context.Context, filter string, obj interface{}, pb proto.Message) (string, []interface{}, map[string]struct{}, error) {
f, err := query.ParseFiltering(filter)
if err != nil {
return "", nil, nil, err
}
return FilteringToGorm(ctx, f, obj, pb)
}
// FilteringToGorm returns GORM Plain SQL representation of the filtering expression.
func FilteringToGorm(ctx context.Context, m *query.Filtering, obj interface{}, pb proto.Message) (string, []interface{}, map[string]struct{}, error) {
if m == nil || m.Root == nil {
return "", nil, nil, nil
}
switch r := m.Root.(type) {
case *query.Filtering_Operator:
return LogicalOperatorToGorm(ctx, r.Operator, obj, pb)
case *query.Filtering_StringCondition:
return StringConditionToGorm(ctx, r.StringCondition, obj, pb)
case *query.Filtering_NumberCondition:
return NumberConditionToGorm(ctx, r.NumberCondition, obj, pb)
case *query.Filtering_NullCondition:
return NullConditionToGorm(ctx, r.NullCondition, obj, pb)
case *query.Filtering_NumberArrayCondition:
return NumberArrayConditionToGorm(ctx, r.NumberArrayCondition, obj, pb)
case *query.Filtering_StringArrayCondition:
return StringArrayConditionToGorm(ctx, r.StringArrayCondition, obj, pb)
default:
return "", nil, nil, fmt.Errorf("%T type is not supported in Filtering", r)
}
}
// LogicalOperatorToGorm returns GORM Plain SQL representation of the logical operator.
func LogicalOperatorToGorm(ctx context.Context, lop *query.LogicalOperator, obj interface{}, pb proto.Message) (string, []interface{}, map[string]struct{}, error) {
var lres string
var largs []interface{}
var lAssocToJoin map[string]struct{}
var err error
switch l := lop.Left.(type) {
case *query.LogicalOperator_LeftOperator:
lres, largs, lAssocToJoin, err = LogicalOperatorToGorm(ctx, l.LeftOperator, obj, pb)
case *query.LogicalOperator_LeftStringCondition:
lres, largs, lAssocToJoin, err = StringConditionToGorm(ctx, l.LeftStringCondition, obj, pb)
case *query.LogicalOperator_LeftNumberCondition:
lres, largs, lAssocToJoin, err = NumberConditionToGorm(ctx, l.LeftNumberCondition, obj, pb)
case *query.LogicalOperator_LeftNullCondition:
lres, largs, lAssocToJoin, err = NullConditionToGorm(ctx, l.LeftNullCondition, obj, pb)
case *query.LogicalOperator_LeftNumberArrayCondition:
lres, largs, lAssocToJoin, err = NumberArrayConditionToGorm(ctx, l.LeftNumberArrayCondition, obj, pb)
case *query.LogicalOperator_LeftStringArrayCondition:
lres, largs, lAssocToJoin, err = StringArrayConditionToGorm(ctx, l.LeftStringArrayCondition, obj, pb)
default:
return "", nil, nil, fmt.Errorf("%T type is not supported in Filtering", l)
}
if err != nil {
return "", nil, nil, err
}
var rres string
var rargs []interface{}
var rAssocToJoin map[string]struct{}
switch r := lop.Right.(type) {
case *query.LogicalOperator_RightOperator:
rres, rargs, rAssocToJoin, err = LogicalOperatorToGorm(ctx, r.RightOperator, obj, pb)
case *query.LogicalOperator_RightStringCondition:
rres, rargs, rAssocToJoin, err = StringConditionToGorm(ctx, r.RightStringCondition, obj, pb)
case *query.LogicalOperator_RightNumberCondition:
rres, rargs, rAssocToJoin, err = NumberConditionToGorm(ctx, r.RightNumberCondition, obj, pb)
case *query.LogicalOperator_RightNullCondition:
rres, rargs, rAssocToJoin, err = NullConditionToGorm(ctx, r.RightNullCondition, obj, pb)
case *query.LogicalOperator_RightNumberArrayCondition:
rres, rargs, rAssocToJoin, err = NumberArrayConditionToGorm(ctx, r.RightNumberArrayCondition, obj, pb)
case *query.LogicalOperator_RightStringArrayCondition:
rres, rargs, rAssocToJoin, err = StringArrayConditionToGorm(ctx, r.RightStringArrayCondition, obj, pb)
default:
return "", nil, nil, fmt.Errorf("%T type is not supported in Filtering", r)
}
if err != nil {
return "", nil, nil, err
}
if lAssocToJoin == nil && rAssocToJoin != nil {
lAssocToJoin = make(map[string]struct{})
}
for k := range rAssocToJoin {
lAssocToJoin[k] = struct{}{}
}
var o string
switch lop.Type {
case query.LogicalOperator_AND:
o = "AND"
case query.LogicalOperator_OR:
o = "OR"
}
var neg string
if lop.IsNegative {
neg = "NOT"
}
return fmt.Sprintf("%s(%s %s %s)", neg, lres, o, rres), append(largs, rargs...), lAssocToJoin, nil
}
// StringConditionToGorm returns GORM Plain SQL representation of the string condition.
func StringConditionToGorm(ctx context.Context, c *query.StringCondition, obj interface{}, pb proto.Message) (string, []interface{}, map[string]struct{}, error) {
var assocToJoin map[string]struct{}
dbName, assoc, err := HandleFieldPath(ctx, c.FieldPath, obj)
if err != nil {
return "", nil, nil, err
}
if assoc != "" {
assocToJoin = make(map[string]struct{})
assocToJoin[assoc] = struct{}{}
}
var o string
switch c.Type {
case query.StringCondition_EQ, query.StringCondition_IEQ:
o = "="
case query.StringCondition_MATCH:
o = "REGEXP"
case query.StringCondition_GT:
o = ">"
case query.StringCondition_GE:
o = ">="
case query.StringCondition_LT:
o = "<"
case query.StringCondition_LE:
o = "<="
}
var neg string
if c.IsNegative {
neg = "NOT"
}
var value interface{}
if v, err := processStringCondition(ctx, c.FieldPath, c.Value, pb); err != nil {
value = c.Value
} else {
value = v
}
if c.Type == query.StringCondition_IEQ {
return insensitiveCaseStringConditionToGorm(neg, dbName, o), []interface{}{value}, assocToJoin, nil
}
return fmt.Sprintf("%s(%s %s ?)", neg, dbName, o), []interface{}{value}, assocToJoin, nil
}
func insensitiveCaseStringConditionToGorm(neg, dbName, operator string) string {
return fmt.Sprintf("%s(lower(%s) %s lower(?))", neg, dbName, operator)
}
func processStringCondition(ctx context.Context, fieldPath []string, value string, pb proto.Message) (interface{}, error) {
objType := indirectType(reflect.TypeOf(pb))
pathLength := len(fieldPath)
for i, part := range fieldPath {
sf, ok := objType.FieldByName(generator.CamelCase(part))
if !ok {
return nil, fmt.Errorf("Cannot find field %s in %s", part, objType)
}
if i < pathLength-1 {
objType = indirectType(sf.Type)
if !isProtoMessage(objType) {
return nil, fmt.Errorf("%s: non-last field of %s field path should be a proto message", objType, fieldPath)
}
} else {
if isIdentifier(indirectType(sf.Type)) {
id := &resource.Identifier{}
if err := jsonpb.UnmarshalString(fmt.Sprintf("\"%s\"", value), id); err != nil {
return nil, err
}
newPb := reflect.New(objType)
v := newPb.Elem().FieldByName(generator.CamelCase(part))
v.Set(reflect.ValueOf(id))
toOrm := newPb.MethodByName("ToORM")
if !toOrm.IsValid() {
return nil, fmt.Errorf("ToORM method cannot be found for %s", objType)
}
res := toOrm.Call([]reflect.Value{reflect.ValueOf(ctx)})
if len(res) != 2 {
return nil, fmt.Errorf("ToORM signature of %s is unknown", objType)
}
orm := res[0]
err := res[1]
if !err.IsNil() {
if tErr, ok := err.Interface().(error); ok {
return nil, tErr
} else {
return nil, fmt.Errorf("ToOrm second return value of %s is expected to be error", objType)
}
}
ormId := orm.FieldByName(generator.CamelCase(part))
if !ormId.IsValid() {
return nil, fmt.Errorf("Cannot find field %s in %s", part, objType)
}
return reflect.Indirect(ormId).Interface(), nil
}
}
}
return value, nil
}
// NumberConditionToGorm returns GORM Plain SQL representation of the number condition.
func NumberConditionToGorm(ctx context.Context, c *query.NumberCondition, obj interface{}, pb proto.Message) (string, []interface{}, map[string]struct{}, error) {
var assocToJoin map[string]struct{}
dbName, assoc, err := HandleFieldPath(ctx, c.FieldPath, obj)
if err != nil {
return "", nil, nil, err
}
if assoc != "" {
assocToJoin = make(map[string]struct{})
assocToJoin[assoc] = struct{}{}
}
var neg string
if c.IsNegative {
neg = "NOT"
}
if c.Type == query.NumberCondition_CONTAINS {
return fmt.Sprintf("%s(%s & ?>0)", neg, dbName), []interface{}{c.Value}, assocToJoin, nil
}
var o string
switch c.Type {
case query.NumberCondition_EQ:
o = "="
case query.NumberCondition_GT:
o = ">"
case query.NumberCondition_GE:
o = ">="
case query.NumberCondition_LT:
o = "<"
case query.NumberCondition_LE:
o = "<="
}
return fmt.Sprintf("%s(%s %s ?)", neg, dbName, o), []interface{}{c.Value}, assocToJoin, nil
}
// NullConditionToGorm returns GORM Plain SQL representation of the null condition.
func NullConditionToGorm(ctx context.Context, c *query.NullCondition, obj interface{}, pb proto.Message) (string, []interface{}, map[string]struct{}, error) {
var assocToJoin map[string]struct{}
dbName, assoc, err := HandleFieldPath(ctx, c.FieldPath, obj)
if err != nil {
return "", nil, nil, err
}
if assoc != "" {
assocToJoin = make(map[string]struct{})
assocToJoin[assoc] = struct{}{}
}
o := "IS NULL"
var neg string
if c.IsNegative {
neg = "NOT"
}
return fmt.Sprintf("%s(%s %s)", neg, dbName, o), nil, assocToJoin, nil
}
func NumberArrayConditionToGorm(ctx context.Context, c *query.NumberArrayCondition, obj interface{}, pb proto.Message) (string, []interface{}, map[string]struct{}, error) {
var assocToJoin map[string]struct{}
dbName, assoc, err := HandleFieldPath(ctx, c.FieldPath, obj)
if err != nil {
return "", nil, nil, err
}
if assoc != "" {
assocToJoin = make(map[string]struct{})
assocToJoin[assoc] = struct{}{}
}
o := "IN"
var neg string
if c.IsNegative {
neg = "NOT"
}
placeholder := ""
values := make([]interface{}, 0, len(c.Values))
for _, val := range c.Values {
placeholder += "?, "
values = append(values, val)
}
return fmt.Sprintf("(%s %s %s (%s))", dbName, neg, o, strings.TrimSuffix(placeholder, ", ")), values, assocToJoin, nil
}
func StringArrayConditionToGorm(ctx context.Context, c *query.StringArrayCondition, obj interface{}, pb proto.Message) (string, []interface{}, map[string]struct{}, error) {
var assocToJoin map[string]struct{}
dbName, assoc, err := HandleFieldPath(ctx, c.FieldPath, obj)
if err != nil {
return "", nil, nil, err
}
if assoc != "" {
assocToJoin = make(map[string]struct{})
assocToJoin[assoc] = struct{}{}
}
o := "IN"
var neg string
if c.IsNegative {
neg = "NOT"
}
values := make([]interface{}, 0, len(c.Values))
placeholder := ""
for _, str := range c.Values {
placeholder += "?, "
if val, err := processStringCondition(ctx, c.FieldPath, str, pb); err == nil {
values = append(values, val)
continue
}
values = append(values, str)
}
return fmt.Sprintf("(%s %s %s (%s))", dbName, neg, o, strings.TrimSuffix(placeholder, ", ")), values, assocToJoin, nil
}