Skip to content

Commit 2395c18

Browse files
authored
Merge pull request #4 from dennigogo/added-filter-list-with-query-parser
Added filter list with query parser ('NOT IN' as 'nin')
2 parents 85946f8 + 030af52 commit 2395c18

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ See cmd/main.go and tests for more examples.
6464
* `:bool` - parameter must be convertable to bool type. Raise error if not.
6565

6666
## Supported types
67-
- `string` - the default type for all provided filters if not specified another. Could be compared by `eq, ne, gt, lt, gte, lte, like, ilike, nlike, nilike, in, is, not` methods (`nlike, nilike` means `NOT LIKE, NOT ILIKE` respectively, `is, not` for comparison to NULL `IS NULL, IS NOT NULL`).
68-
- `int` - integer type. Must be specified with tag ":int". Could be compared by `eq, ne, gt, lt, gte, lte, in` methods.
67+
- `string` - the default type for all provided filters if not specified another. Could be compared by `eq, ne, gt, lt, gte, lte, like, ilike, nlike, nilike, in, nin, is, not` methods (`nlike, nilike` means `NOT LIKE, NOT ILIKE` respectively, `in, nin` means `IN, NOT IN` respectively, `is, not` for comparison to NULL `IS NULL, IS NOT NULL`).
68+
- `int` - integer type. Must be specified with tag ":int". Could be compared by `eq, ne, gt, lt, gte, lte, in, nin` methods.
6969
- `bool` - boolean type. Must be specified with tag ":bool". Could be compared by `eq` method.
7070

7171
## Date usage

filter.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func (f *Filter) Where() (string, error) {
210210
return exp, nil
211211
}
212212
return exp, ErrUnknownMethod
213-
case IN:
213+
case IN, NIN:
214214
exp = fmt.Sprintf("%s %s (?)", f.Name, translateMethods[f.Method])
215215
exp, _, _ = in(exp, f.Value)
216216
return exp, nil
@@ -244,7 +244,7 @@ func (f *Filter) Args() ([]interface{}, error) {
244244
}
245245
args = append(args, value)
246246
return args, nil
247-
case IN:
247+
case IN, NIN:
248248
_, params, _ := in("?", f.Value)
249249
args = append(args, params...)
250250
return args, nil
@@ -256,7 +256,7 @@ func (f *Filter) Args() ([]interface{}, error) {
256256
func (f *Filter) setInt(list []string) error {
257257
if len(list) == 1 {
258258
switch f.Method {
259-
case EQ, NE, GT, LT, GTE, LTE, IN:
259+
case EQ, NE, GT, LT, GTE, LTE, IN, NIN:
260260
i, err := strconv.Atoi(list[0])
261261
if err != nil {
262262
return ErrBadFormat
@@ -266,7 +266,7 @@ func (f *Filter) setInt(list []string) error {
266266
return ErrMethodNotAllowed
267267
}
268268
} else {
269-
if f.Method != IN {
269+
if f.Method != IN && f.Method != NIN {
270270
return ErrMethodNotAllowed
271271
}
272272
intSlice := make([]int, len(list))
@@ -302,7 +302,7 @@ func (f *Filter) setBool(list []string) error {
302302
func (f *Filter) setString(list []string) error {
303303
if len(list) == 1 {
304304
switch f.Method {
305-
case EQ, NE, GT, LT, GTE, LTE, LIKE, ILIKE, NLIKE, NILIKE, IN:
305+
case EQ, NE, GT, LT, GTE, LTE, LIKE, ILIKE, NLIKE, NILIKE, IN, NIN:
306306
f.Value = list[0]
307307
return nil
308308
case IS, NOT:
@@ -315,7 +315,7 @@ func (f *Filter) setString(list []string) error {
315315
}
316316
} else {
317317
switch f.Method {
318-
case IN:
318+
case IN, NIN:
319319
f.Value = list
320320
return nil
321321
}

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var (
4545
IS Method = "IS"
4646
NOT Method = "NOT"
4747
IN Method = "IN"
48+
NIN Method = "NIN"
4849
)
4950

5051
// NULL constant
@@ -65,6 +66,7 @@ var (
6566
IS: "IS",
6667
NOT: "IS NOT",
6768
IN: "IN",
69+
NIN: "NOT IN",
6870
}
6971
)
7072

main_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,16 @@ func TestWhere(t *testing.T) {
212212
{url: "?id=1.2", expected: "", err: "id: bad format"},
213213
{url: "?id[in]=1.2", expected: "", err: "id[in]: bad format"},
214214
{url: "?id[in]=1.2,1.2", expected: "", err: "id[in]: bad format"},
215+
{url: "?id[nin]=1.2", expected: "", err: "id[nin]: bad format"},
216+
{url: "?id[nin]=1.2,1.2", expected: "", err: "id[nin]: bad format"},
215217
{url: "?id[test]=1", expected: "", err: "id[test]: unknown method"},
216218
{url: "?id[like]=1", expected: "", err: "id[like]: method are not allowed"},
217219
{url: "?id=1,2", expected: "", err: "id: method are not allowed"},
218220
{url: "?id=4", expected: " WHERE id = ?"},
219221

220222
{url: "?id=100", err: "id: can't be greater then 10"},
221223
{url: "?id[in]=100,200", err: "id[in]: can't be greater then 10"},
224+
{url: "?id[nin]=100,200", err: "id[nin]: can't be greater then 10"},
222225

223226
// not like, not ilike:
224227
{url: "?u[nlike]=superman", expected: " WHERE u NOT LIKE ?"},
@@ -229,6 +232,8 @@ func TestWhere(t *testing.T) {
229232
{url: "?s=super", expected: " WHERE s = ?"},
230233
{url: "?s[in]=super,puper", err: "s[in]: puper: not in scope"},
231234
{url: "?s[in]=super,best", expected: " WHERE s IN (?, ?)"},
235+
{url: "?s[nin]=super,puper", err: "s[nin]: puper: not in scope"},
236+
{url: "?s[nin]=super,best", expected: " WHERE s NOT IN (?, ?)"},
232237
{url: "?s=puper", expected: "", err: "s: puper: not in scope"},
233238
{url: "?u=puper", expected: " WHERE u = ?"},
234239
{url: "?u[eq]=1,2", expected: "", err: "u[eq]: method are not allowed"},

0 commit comments

Comments
 (0)