added sort support
This commit is contained in:
parent
558d6ae7d0
commit
1674abae73
5 changed files with 377 additions and 66 deletions
146
README.md
146
README.md
|
|
@ -1,6 +1,61 @@
|
|||
# filter - a simple struct field filter for go
|
||||
# filter - a simple struct field filter and sorter for go
|
||||
|
||||
## Usage
|
||||
## Query Syntax
|
||||
|
||||
### Matching
|
||||
|
||||
Explicit match: `<struct field>:<struct value>`
|
||||
|
||||
Wildcard match (use ** to match the '*' character):
|
||||
|
||||
```
|
||||
<struct field>:*<right part of struct value>
|
||||
<struct field>:<left part of struct value>*
|
||||
<struct field>:*<middl of struct value>*
|
||||
```
|
||||
|
||||
Nested structs: `<struct field>.<struct field>:<struct value>`
|
||||
|
||||
Multiple matches (seperated by a single whitespace):
|
||||
|
||||
```
|
||||
<struct field>:<struct value> <struct field>:<struct value>*
|
||||
```
|
||||
|
||||
Bool does not support the wildcard operator, only the literal strings `true` and `false`.
|
||||
|
||||
### Sorting
|
||||
|
||||
The sort statement consists of the literal string `sort`, the field name (nesting like above is supported) and the literal strings `asc` or `desc`.
|
||||
|
||||
```
|
||||
sort:<struct field>:asc
|
||||
sort:<struct field>:desc
|
||||
sort:<struct field>.<struct field>:desc
|
||||
```
|
||||
|
||||
### Struct fields
|
||||
|
||||
Supported struct field values are:
|
||||
|
||||
* `int`, `int8`, `int16`, `int32`, `int64`
|
||||
* `float32`, `float64`
|
||||
* `string`
|
||||
* `bool`
|
||||
|
||||
For nested structs:
|
||||
|
||||
* `struct`
|
||||
|
||||
### Filter logic
|
||||
|
||||
Supported logical matching:
|
||||
|
||||
* `AND` - all statements have to match
|
||||
* `OR` - only one statement has to match
|
||||
* `NOT` - none of the statements have to match
|
||||
|
||||
## Examples
|
||||
|
||||
```
|
||||
// Example data structure
|
||||
|
|
@ -35,27 +90,6 @@ var data = Filter{
|
|||
}
|
||||
```
|
||||
|
||||
### Struct fields
|
||||
|
||||
Supported struct field values are:
|
||||
|
||||
* `int`, `int8`, `int16`, `int32`, `int64`
|
||||
* `float32`, `float64`
|
||||
* `string`
|
||||
* `bool`
|
||||
|
||||
For nested structs:
|
||||
|
||||
* `struct`
|
||||
|
||||
### Filter logic
|
||||
|
||||
Supported logical matching:
|
||||
|
||||
* `AND` - all of the queries have to match
|
||||
* `OR` - only one query has to match
|
||||
* `NOT` - none of the queries have to match
|
||||
|
||||
### Explicit match
|
||||
|
||||
```
|
||||
|
|
@ -91,35 +125,45 @@ filter.NewQuery("Struct.String:*2 Bool:true", filter.AND).Filter(&data)
|
|||
data = [{2str 42 true {s2 {2}}}]
|
||||
```
|
||||
|
||||
### Sorting
|
||||
|
||||
The `GenericFilter` does not support sorting.
|
||||
|
||||
#### Sort
|
||||
|
||||
```
|
||||
filter.NewQuery("sort:Bool:desc", filter.AND).Filter(&data)
|
||||
|
||||
data = [{str1 42 false {s1 {1}}} {2str 42 true {s2 {2}}} {str3 42 true {s3 {3}}}]
|
||||
```
|
||||
|
||||
#### Sort and Filter
|
||||
|
||||
```
|
||||
filter.NewQuery("sort:Bool:desc String:str*", filter.AND).Filter(&data)
|
||||
|
||||
data = [{str1 42 false {s1 {1}}} {str3 42 true {s3 {3}}}]
|
||||
```
|
||||
|
||||
## Benchmarks
|
||||
|
||||
```
|
||||
go test -cpu 1,2,4 --bench .
|
||||
BenchmarkNewQuerySimple 3000000 401 ns/op 176 B/op 7 allocs/op
|
||||
BenchmarkNewQuerySimple-2 5000000 390 ns/op 176 B/op 7 allocs/op
|
||||
BenchmarkNewQuerySimple-4 2000000 792 ns/op 176 B/op 7 allocs/op
|
||||
BenchmarkNewQueryComplex 1000000 1416 ns/op 656 B/op 17 allocs/op
|
||||
BenchmarkNewQueryComplex-2 1000000 1384 ns/op 656 B/op 17 allocs/op
|
||||
BenchmarkNewQueryComplex-4 1000000 2488 ns/op 656 B/op 17 allocs/op
|
||||
BenchmarkNewQueryComplexOr 1000000 1192 ns/op 640 B/op 17 allocs/op
|
||||
BenchmarkNewQueryComplexOr-2 1000000 1158 ns/op 640 B/op 17 allocs/op
|
||||
BenchmarkNewQueryComplexOr-4 1000000 1963 ns/op 640 B/op 17 allocs/op
|
||||
BenchmarkFilterSimple 2000000 907 ns/op 432 B/op 12 allocs/op
|
||||
BenchmarkFilterSimple-2 2000000 899 ns/op 432 B/op 12 allocs/op
|
||||
BenchmarkFilterSimple-4 1000000 1468 ns/op 432 B/op 12 allocs/op
|
||||
BenchmarkFilterComplex 500000 3129 ns/op 672 B/op 48 allocs/op
|
||||
BenchmarkFilterComplex-2 500000 3208 ns/op 672 B/op 48 allocs/op
|
||||
BenchmarkFilterComplex-4 500000 6644 ns/op 672 B/op 48 allocs/op
|
||||
BenchmarkFilterComplexOr 500000 2698 ns/op 816 B/op 39 allocs/op
|
||||
BenchmarkFilterComplexOr-2 500000 2832 ns/op 816 B/op 39 allocs/op
|
||||
BenchmarkFilterComplexOr-4 500000 5509 ns/op 816 B/op 39 allocs/op
|
||||
BenchmarkGenericFilterSimple 500000 2386 ns/op 1200 B/op 24 allocs/op
|
||||
BenchmarkGenericFilterSimple-2 1000000 2360 ns/op 1200 B/op 24 allocs/op
|
||||
BenchmarkGenericFilterSimple-4 1000000 3656 ns/op 1200 B/op 24 allocs/op
|
||||
BenchmarkGenericFilterComplex 200000 7178 ns/op 1680 B/op 96 allocs/op
|
||||
BenchmarkGenericFilterComplex-2 200000 7102 ns/op 1680 B/op 96 allocs/op
|
||||
BenchmarkGenericFilterComplex-4 200000 12602 ns/op 1680 B/op 96 allocs/op
|
||||
BenchmarkGenericFilterComplexOr 200000 5454 ns/op 2456 B/op 61 allocs/op
|
||||
BenchmarkGenericFilterComplexOr-2 300000 5444 ns/op 2456 B/op 61 allocs/op
|
||||
BenchmarkGenericFilterComplexOr-4 300000 9970 ns/op 2456 B/op 61 allocs/op
|
||||
go test -bench .
|
||||
BenchmarkNewQuerySimple-4 2000000 974 ns/op 272 B/op 8 allocs/op
|
||||
BenchmarkNewQueryComplex-4 500000 2779 ns/op 848 B/op 20 allocs/op
|
||||
BenchmarkNewQueryComplexOr-4 500000 2677 ns/op 832 B/op 20 allocs/op
|
||||
BenchmarkNewQuerySort-4 3000000 576 ns/op 160 B/op 4 allocs/op
|
||||
BenchmarkNewQuerySortSimple-4 1000000 1192 ns/op 304 B/op 9 allocs/op
|
||||
BenchmarkNewQuerySortComplex-4 500000 2598 ns/op 784 B/op 19 allocs/op
|
||||
BenchmarkNewQuerySortComplexOr-4 500000 2326 ns/op 768 B/op 19 allocs/op
|
||||
BenchmarkFilterSimple-4 1000000 2123 ns/op 432 B/op 12 allocs/op
|
||||
BenchmarkFilterComplex-4 200000 6840 ns/op 672 B/op 42 allocs/op
|
||||
BenchmarkFilterComplexOr-4 300000 5929 ns/op 744 B/op 30 allocs/op
|
||||
BenchmarkSort-4 200000 7859 ns/op 1864 B/op 51 allocs/op
|
||||
BenchmarkSortFilterSimple-4 200000 6294 ns/op 1432 B/op 39 allocs/op
|
||||
BenchmarkSortFilterComplex-4 200000 11990 ns/op 1672 B/op 69 allocs/op
|
||||
BenchmarkSortFilterComplexOr-4 200000 12138 ns/op 2176 B/op 69 allocs/op
|
||||
BenchmarkGenericFilterSimple-4 300000 5939 ns/op 1200 B/op 24 allocs/op
|
||||
BenchmarkGenericFilterComplex-4 100000 16638 ns/op 1680 B/op 84 allocs/op
|
||||
BenchmarkGenericFilterComplexOr-4 200000 11846 ns/op 2360 B/op 49 allocs/op
|
||||
```
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue