This repository has been archived by the owner on May 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkendo.go
141 lines (121 loc) · 3.34 KB
/
kendo.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
package kendoparser
/*
* @Author
* Radityo <[email protected]>
*/
// KendoRequest option variable to struct (each apps has different format, defined/extend yourself if needed)
type KendoRequest struct {
Data Data `json:"data"`
}
// Data Kendo DataSource payload
type Data struct {
Filter Filter `json:"filter"`
Page int `json:"page"`
PageSize int `json:"pageSize"`
Skip int `json:"skip"`
Take int `json:"take"`
Sort Sort `json:"sort"`
}
// Filter struct filters
type Filter struct {
Filters []Filter `json:"filters"`
Logic string `json:"logic"`
Field string `json:"field"`
Operator string `json:"operator"`
IgnoreCase bool `json:"ignoreCase"`
Value interface{} `json:"value"`
Values []interface{} `json:"values"`
// logic pre-parser
additionalParsers []FilterParser
operatorManager *OperatorManager
}
// AdditionalParsers AdditionalParsers
func (f *Filter) AdditionalParsers() []FilterParser {
return f.additionalParsers
}
// SetOperatorManager scoped Operator Manager
func (f *Filter) SetOperatorManager(om *OperatorManager) *Filter {
f.operatorManager = om
for i := range f.Filters {
f.Filters[i].SetOperatorManager(om)
}
return f
}
// GetOperatorManager scoped Operator Manager
func (f *Filter) GetOperatorManager() *OperatorManager {
return f.operatorManager
}
// AddParser AddParser
func (f *Filter) AddParser(parsers ...FilterParser) *Filter {
if f.additionalParsers == nil {
f.additionalParsers = []FilterParser{}
}
for _, parser := range parsers {
if parser != nil {
f.additionalParsers = append(f.additionalParsers, parser)
}
}
return f
}
// AddAllParser AddAllParser
func (f *Filter) AddAllParser(parsers ...FilterParser) *Filter {
for i := range f.Filters {
f.Filters[i].AddAllParser(parsers...)
}
f.AddParser(parsers...)
return f
}
// Parse Parse will return interface
func (f *Filter) Parse(parser FilterParser) interface{} {
return parser(f)
}
// ResetAdditionalParsers reset all pre-filter available
func (f *Filter) ResetAdditionalParsers() *Filter {
f.additionalParsers = []FilterParser{}
return f
}
// ResetAllAdditionalParsers reset all pre-filter available
func (f *Filter) ResetAllAdditionalParsers() *Filter {
for i := range f.Filters {
f.Filters[i].ResetAllAdditionalParsers()
}
f.additionalParsers = []FilterParser{}
return f
}
// Transform your filter
func (f *Filter) Transform(transform func(*Filter)) *Filter {
transform(f)
return f
}
// TransformField only transform field
func (f *Filter) TransformField(transform func(string) string) *Filter {
f.Field = transform(f.Field)
return f
}
// TransformAll your filter include all childs
func (f *Filter) TransformAll(transform func(*Filter)) *Filter {
for i := range f.Filters {
f.Filters[i].TransformAll(transform)
}
f.Transform(transform)
return f
}
// TransformAllField only transform field include all childs
func (f *Filter) TransformAllField(transform func(string) string) *Filter {
for i := range f.Filters {
f.Filters[i].TransformAllField(transform)
}
f.TransformField(transform)
return f
}
// SortDetail struct sort
type SortDetail struct {
Dir string `json:"dir"`
Field string `json:"field"`
}
// Sort alias []SortDetail
type Sort []SortDetail
// Parse Parse
func (s *Sort) Parse(parser SortParser) interface{} {
return parser(s)
}