@@ -20,6 +20,10 @@ func (a *All) Eval(filterable Filterable) (bool, error) {
20
20
return true , nil
21
21
}
22
22
23
+ func (a * All ) ExtractConditions () []Condition {
24
+ return extractConditions (a .rules )
25
+ }
26
+
23
27
// Any represents a filter chain type that matches when at least one of its Rules matches.
24
28
type Any struct {
25
29
rules []Filter
@@ -40,6 +44,10 @@ func (a *Any) Eval(filterable Filterable) (bool, error) {
40
44
return false , nil
41
45
}
42
46
47
+ func (a * Any ) ExtractConditions () []Condition {
48
+ return extractConditions (a .rules )
49
+ }
50
+
43
51
// None represents a filter chain type that matches when none of its Rules matches.
44
52
type None struct {
45
53
rules []Filter
@@ -60,115 +68,151 @@ func (n *None) Eval(filterable Filterable) (bool, error) {
60
68
return true , nil
61
69
}
62
70
71
+ func (n * None ) ExtractConditions () []Condition {
72
+ return extractConditions (n .rules )
73
+ }
74
+
63
75
// Condition represents a single filter condition.
64
76
type Condition struct {
65
- column string
66
- value string
77
+ Column string
78
+ Value string
67
79
}
68
80
69
- func NewCondition (column string , value string ) * Condition {
70
- return & Condition {
71
- column : column ,
72
- value : value ,
81
+ func NewCondition (column string , value string ) Condition {
82
+ return Condition {
83
+ Column : column ,
84
+ Value : value ,
73
85
}
74
86
}
75
87
76
- type Exists Condition
88
+ func (e Condition ) ExtractConditions () []Condition {
89
+ return []Condition {e }
90
+ }
91
+
92
+ type Exists struct {
93
+ Condition
94
+ }
77
95
78
96
func NewExists (column string ) * Exists {
79
- return & Exists {column : column }
97
+ return & Exists {Condition { Column : column } }
80
98
}
81
99
82
100
func (e * Exists ) Eval (filterable Filterable ) (bool , error ) {
83
- return filterable .EvalExists (e .column ), nil
101
+ return filterable .EvalExists (e .Column ), nil
84
102
}
85
103
86
- type Equal Condition
104
+ type Equal struct {
105
+ Condition
106
+ }
87
107
88
108
func (e * Equal ) Eval (filterable Filterable ) (bool , error ) {
89
- match , err := filterable .EvalEqual (e .column , e .value )
109
+ match , err := filterable .EvalEqual (e .Column , e .Value )
90
110
if err != nil {
91
111
return false , err
92
112
}
93
113
94
114
return match , nil
95
115
}
96
116
97
- type UnEqual Condition
117
+ type UnEqual struct {
118
+ Condition
119
+ }
98
120
99
121
func (u * UnEqual ) Eval (filterable Filterable ) (bool , error ) {
100
- match , err := filterable .EvalEqual (u .column , u .value )
122
+ match , err := filterable .EvalEqual (u .Column , u .Value )
101
123
if err != nil {
102
124
return false , err
103
125
}
104
126
105
- return filterable .EvalExists (u .column ) && ! match , nil
127
+ return filterable .EvalExists (u .Column ) && ! match , nil
106
128
}
107
129
108
- type Like Condition
130
+ type Like struct {
131
+ Condition
132
+ }
109
133
110
134
func (l * Like ) Eval (filterable Filterable ) (bool , error ) {
111
- match , err := filterable .EvalLike (l .column , l .value )
135
+ match , err := filterable .EvalLike (l .Column , l .Value )
112
136
if err != nil {
113
137
return false , err
114
138
}
115
139
116
140
return match , nil
117
141
}
118
142
119
- type Unlike Condition
143
+ type Unlike struct {
144
+ Condition
145
+ }
120
146
121
147
func (u * Unlike ) Eval (filterable Filterable ) (bool , error ) {
122
- match , err := filterable .EvalLike (u .column , u .value )
148
+ match , err := filterable .EvalLike (u .Column , u .Value )
123
149
if err != nil {
124
150
return false , err
125
151
}
126
152
127
- return filterable .EvalExists (u .column ) && ! match , nil
153
+ return filterable .EvalExists (u .Column ) && ! match , nil
128
154
}
129
155
130
- type LessThan Condition
156
+ type LessThan struct {
157
+ Condition
158
+ }
131
159
132
160
func (less * LessThan ) Eval (filterable Filterable ) (bool , error ) {
133
- match , err := filterable .EvalLess (less .column , less .value )
161
+ match , err := filterable .EvalLess (less .Column , less .Value )
134
162
if err != nil {
135
163
return false , err
136
164
}
137
165
138
166
return match , nil
139
167
}
140
168
141
- type LessThanOrEqual Condition
169
+ type LessThanOrEqual struct {
170
+ Condition
171
+ }
142
172
143
173
func (loe * LessThanOrEqual ) Eval (filterable Filterable ) (bool , error ) {
144
- match , err := filterable .EvalLessOrEqual (loe .column , loe .value )
174
+ match , err := filterable .EvalLessOrEqual (loe .Column , loe .Value )
145
175
if err != nil {
146
176
return false , err
147
177
}
148
178
149
179
return match , nil
150
180
}
151
181
152
- type GreaterThan Condition
182
+ type GreaterThan struct {
183
+ Condition
184
+ }
153
185
154
186
func (g * GreaterThan ) Eval (filterable Filterable ) (bool , error ) {
155
- match , err := filterable .EvalLessOrEqual (g .column , g .value )
187
+ match , err := filterable .EvalLessOrEqual (g .Column , g .Value )
156
188
if err != nil {
157
189
return false , err
158
190
}
159
191
160
- return filterable .EvalExists (g .column ) && ! match , nil
192
+ return filterable .EvalExists (g .Column ) && ! match , nil
161
193
}
162
194
163
- type GreaterThanOrEqual Condition
195
+ type GreaterThanOrEqual struct {
196
+ Condition
197
+ }
164
198
165
199
func (goe * GreaterThanOrEqual ) Eval (filterable Filterable ) (bool , error ) {
166
- match , err := filterable .EvalLess (goe .column , goe .value )
200
+ match , err := filterable .EvalLess (goe .Column , goe .Value )
167
201
if err != nil {
168
202
return false , err
169
203
}
170
204
171
- return filterable .EvalExists (goe .column ) && ! match , nil
205
+ return filterable .EvalExists (goe .Column ) && ! match , nil
206
+ }
207
+
208
+ // extractConditions extracts filter conditions from the specified filter rules.
209
+ func extractConditions (rules []Filter ) []Condition {
210
+ var conditions []Condition
211
+ for _ , rule := range rules {
212
+ conditions = append (conditions , rule .ExtractConditions ()... )
213
+ }
214
+
215
+ return conditions
172
216
}
173
217
174
218
var (
0 commit comments