forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.go
362 lines (327 loc) · 11.9 KB
/
converter.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package gorm
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/infobloxopen/atlas-app-toolkit/query"
"github.com/infobloxopen/atlas-app-toolkit/rpc/resource"
"github.com/infobloxopen/atlas-app-toolkit/util"
)
// DefaultFilteringConditionProcessor processes filter operator conversion
type DefaultFilteringConditionProcessor struct {
pb proto.Message
}
// DefaultFilteringConditionConverter performs default convertion for Filter collection operator
type DefaultFilteringConditionConverter struct {
Processor FilteringConditionProcessor
}
// DefaultSortingCriteriaConverter performs default convertion for Sorting collection operator
type DefaultSortingCriteriaConverter struct{}
// DefaultPaginationConverter performs default convertion for Paging collection operator
type DefaultPaginationConverter struct{}
// DefaultPbToOrmConverter performs default convertion for all collection operators
type DefaultPbToOrmConverter struct {
DefaultFilteringConditionConverter
DefaultSortingCriteriaConverter
DefaultFieldSelectionConverter
DefaultPaginationConverter
}
// NewDefaultPbToOrmConverter creates default converter for all collection operators
func NewDefaultPbToOrmConverter(pb proto.Message) CollectionOperatorsConverter {
return &DefaultPbToOrmConverter{
DefaultFilteringConditionConverter{&DefaultFilteringConditionProcessor{pb}},
DefaultSortingCriteriaConverter{},
DefaultFieldSelectionConverter{},
DefaultPaginationConverter{},
}
}
// LogicalOperatorToGorm returns GORM Plain SQL representation of the logical operator.
func (converter *DefaultFilteringConditionConverter) LogicalOperatorToGorm(ctx context.Context, lop *query.LogicalOperator, obj interface{}) (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 = converter.LogicalOperatorToGorm(ctx, l.LeftOperator, obj)
case *query.LogicalOperator_LeftStringCondition:
lres, largs, lAssocToJoin, err = converter.StringConditionToGorm(ctx, l.LeftStringCondition, obj)
case *query.LogicalOperator_LeftNumberCondition:
lres, largs, lAssocToJoin, err = converter.NumberConditionToGorm(ctx, l.LeftNumberCondition, obj)
case *query.LogicalOperator_LeftNullCondition:
lres, largs, lAssocToJoin, err = converter.NullConditionToGorm(ctx, l.LeftNullCondition, obj)
case *query.LogicalOperator_LeftNumberArrayCondition:
lres, largs, lAssocToJoin, err = converter.NumberArrayConditionToGorm(ctx, l.LeftNumberArrayCondition, obj)
case *query.LogicalOperator_LeftStringArrayCondition:
lres, largs, lAssocToJoin, err = converter.StringArrayConditionToGorm(ctx, l.LeftStringArrayCondition, obj)
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 = converter.LogicalOperatorToGorm(ctx, r.RightOperator, obj)
case *query.LogicalOperator_RightStringCondition:
rres, rargs, rAssocToJoin, err = converter.StringConditionToGorm(ctx, r.RightStringCondition, obj)
case *query.LogicalOperator_RightNumberCondition:
rres, rargs, rAssocToJoin, err = converter.NumberConditionToGorm(ctx, r.RightNumberCondition, obj)
case *query.LogicalOperator_RightNullCondition:
rres, rargs, rAssocToJoin, err = converter.NullConditionToGorm(ctx, r.RightNullCondition, obj)
case *query.LogicalOperator_RightNumberArrayCondition:
rres, rargs, rAssocToJoin, err = converter.NumberArrayConditionToGorm(ctx, r.RightNumberArrayCondition, obj)
case *query.LogicalOperator_RightStringArrayCondition:
rres, rargs, rAssocToJoin, err = converter.StringArrayConditionToGorm(ctx, r.RightStringArrayCondition, obj)
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 (converter *DefaultFilteringConditionConverter) StringConditionToGorm(ctx context.Context, c *query.StringCondition, obj interface{}) (string, []interface{}, map[string]struct{}, error) {
var (
assocToJoin map[string]struct{}
dbName, assoc string
err error
)
if IsJSONCondition(ctx, c.FieldPath, obj) {
dbName, assoc, err = HandleJSONFieldPath(ctx, c.FieldPath, obj, c.Value)
} else {
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 = "~"
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 := converter.Processor.ProcessStringCondition(ctx, c.FieldPath, c.Value); err != nil {
value = c.Value
} else {
value = v
}
if c.Type == query.StringCondition_IEQ {
return converter.insensitiveCaseStringConditionToGorm(neg, dbName, o), []interface{}{value}, assocToJoin, nil
}
return fmt.Sprintf("%s(%s %s ?)", neg, dbName, o), []interface{}{value}, assocToJoin, nil
}
func (converter *DefaultFilteringConditionConverter) insensitiveCaseStringConditionToGorm(neg, dbName, operator string) string {
return fmt.Sprintf("%s(lower(%s) %s lower(?))", neg, dbName, operator)
}
// ProcessStringCondition processes a string condition to GORM Plain SQL representation
func (p *DefaultFilteringConditionProcessor) ProcessStringCondition(ctx context.Context, fieldPath []string, value string) (interface{}, error) {
objType := indirectType(reflect.TypeOf(p.pb))
pathLength := len(fieldPath)
for i, part := range fieldPath {
sf, ok := objType.FieldByName(util.Camel(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(util.Camel(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(util.Camel(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 (converter *DefaultFilteringConditionConverter) NumberConditionToGorm(ctx context.Context, c *query.NumberCondition, obj interface{}) (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.NumberCondition_EQ:
o = "="
case query.NumberCondition_GT:
o = ">"
case query.NumberCondition_GE:
o = ">="
case query.NumberCondition_LT:
o = "<"
case query.NumberCondition_LE:
o = "<="
}
var neg string
if c.IsNegative {
neg = "NOT"
}
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 (converter *DefaultFilteringConditionConverter) NullConditionToGorm(ctx context.Context, c *query.NullCondition, obj interface{}) (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 (converter *DefaultFilteringConditionConverter) NumberArrayConditionToGorm(ctx context.Context, c *query.NumberArrayCondition, obj interface{}) (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 (converter *DefaultFilteringConditionConverter) StringArrayConditionToGorm(ctx context.Context, c *query.StringArrayCondition, obj interface{}) (string, []interface{}, map[string]struct{}, error) {
var (
assocToJoin map[string]struct{}
dbName, assoc string
err error
)
if IsJSONCondition(ctx, c.FieldPath, obj) {
dbName, assoc, err = HandleJSONFieldPath(ctx, c.FieldPath, obj, c.Values...)
} else {
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 := converter.Processor.ProcessStringCondition(ctx, c.FieldPath, str); 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
}
func (converter *DefaultSortingCriteriaConverter) SortingCriteriaToGorm(ctx context.Context, cr *query.SortCriteria, obj interface{}) (string, string, error) {
dbCr, assoc, err := HandleFieldPath(ctx, strings.Split(cr.GetTag(), "."), obj)
if cr.IsDesc() {
dbCr += " desc"
}
return dbCr, assoc, err
}
func (converter *DefaultPaginationConverter) PaginationToGorm(ctx context.Context, p *query.Pagination) (offset, limit int32) {
if p != nil {
return p.GetOffset(), p.GetLimit()
}
return 0, 0
}