forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filtering.go
88 lines (74 loc) · 3.54 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
package gorm
import (
"context"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/infobloxopen/atlas-app-toolkit/query"
)
type LogicalOperatorConverter interface {
LogicalOperatorToGorm(ctx context.Context, lop *query.LogicalOperator, obj interface{}) (string, []interface{}, map[string]struct{}, error)
}
type NullConditionConverter interface {
NullConditionToGorm(ctx context.Context, c *query.NullCondition, obj interface{}) (string, []interface{}, map[string]struct{}, error)
}
type StringConditionConverter interface {
StringConditionToGorm(ctx context.Context, c *query.StringCondition, obj interface{}) (string, []interface{}, map[string]struct{}, error)
}
type StringArrayConditionConverter interface {
StringArrayConditionToGorm(ctx context.Context, c *query.StringArrayCondition, obj interface{}) (string, []interface{}, map[string]struct{}, error)
}
type NumberConditionConverter interface {
NumberConditionToGorm(ctx context.Context, c *query.NumberCondition, obj interface{}) (string, []interface{}, map[string]struct{}, error)
}
type NumberArrayConditionConverter interface {
NumberArrayConditionToGorm(ctx context.Context, c *query.NumberArrayCondition, obj interface{}) (string, []interface{}, map[string]struct{}, error)
}
type FilteringConditionConverter interface {
LogicalOperatorConverter
NullConditionConverter
StringConditionConverter
StringArrayConditionConverter
NumberConditionConverter
NumberArrayConditionConverter
}
type FilteringConditionProcessor interface {
ProcessStringCondition(ctx context.Context, fieldPath []string, value string) (interface{}, error)
}
// 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
}
c := &DefaultFilteringConditionConverter{&DefaultFilteringConditionProcessor{pb}}
return FilteringToGormEx(ctx, f, obj, c)
}
//Deprecated: Use FilteringToGormEx instead
// 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) {
c := &DefaultFilteringConditionConverter{&DefaultFilteringConditionProcessor{pb}}
return FilteringToGormEx(ctx, m, obj, c)
}
// FilteringToGorm returns GORM Plain SQL representation of the filtering expression.
func FilteringToGormEx(ctx context.Context, m *query.Filtering, obj interface{}, c FilteringConditionConverter) (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 c.LogicalOperatorToGorm(ctx, r.Operator, obj)
case *query.Filtering_StringCondition:
return c.StringConditionToGorm(ctx, r.StringCondition, obj)
case *query.Filtering_NumberCondition:
return c.NumberConditionToGorm(ctx, r.NumberCondition, obj)
case *query.Filtering_NullCondition:
return c.NullConditionToGorm(ctx, r.NullCondition, obj)
case *query.Filtering_NumberArrayCondition:
return c.NumberArrayConditionToGorm(ctx, r.NumberArrayCondition, obj)
case *query.Filtering_StringArrayCondition:
return c.StringArrayConditionToGorm(ctx, r.StringArrayCondition, obj)
default:
return "", nil, nil, fmt.Errorf("%T type is not supported in Filtering", r)
}
}