100 lines
3.2 KiB
Markdown
100 lines
3.2 KiB
Markdown
# filter - a simple struct field filter for go
|
|
|
|
## Usage
|
|
|
|
```
|
|
// Example data structure
|
|
type Example struct {
|
|
String string
|
|
Int int
|
|
Bool bool
|
|
Struct Struct1
|
|
}
|
|
|
|
type Struct1 struct {
|
|
String string
|
|
Struct Struct2
|
|
}
|
|
|
|
type Struct2 struct {
|
|
Int int
|
|
}
|
|
|
|
// Filter implements the filter.Filter interface
|
|
type Filter []Example
|
|
|
|
func (v Filter) Len() int { return len(v) }
|
|
func (v Filter) Index(i int) interface{} { return v[i] }
|
|
func (v *Filter) Remove(i, j int) { *v = append((*v)[:i], (*v)[j:]...) }
|
|
|
|
// Example data
|
|
var data = Filter{
|
|
{"str1", 42, true, Struct1{"s1", Struct2{1}}},
|
|
{"2str", 42, true, Struct1{"s2", Struct2{2}}},
|
|
{"str3", 42, false, Struct1{"s3", Struct2{3}}},
|
|
}
|
|
```
|
|
|
|
### 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
|
|
|
|
```
|
|
filter.NewQuery("Bool:true", filter.OR).Filter(&data)
|
|
|
|
data = [{str1 42 true {s1 {1}}} {str2 42 true {s2 {2}}}]
|
|
```
|
|
|
|
### Wildcard match
|
|
|
|
```
|
|
filter.NewQuery("String:s*", filter.OR).Filter(&data)
|
|
|
|
data = [{str1 42 true {s1 {1}}} {str3 42 false {s3 {3}}}]
|
|
```
|
|
|
|
### Nested struct match
|
|
|
|
```
|
|
filter.NewQuery("Struct.Struct.Int:3", filter.OR).Filter(&data)
|
|
|
|
data = [{str3 42 false {s3 {3}}}]
|
|
```
|
|
|
|
### Multiple matches
|
|
|
|
```
|
|
filter.NewQuery("Struct.String:*2 Bool:true", filter.AND).Filter(&data)
|
|
|
|
data = [{2str 42 true {s2 {2}}}]
|
|
```
|
|
|
|
## Benchmarks
|
|
|
|
```
|
|
go test -cpu 1,2,4 --bench .
|
|
BenchmarkFilterSimple 2000000 877 ns/op 432 B/op 12 allocs/op
|
|
BenchmarkFilterSimple-2 2000000 865 ns/op 432 B/op 12 allocs/op
|
|
BenchmarkFilterSimple-4 1000000 1713 ns/op 432 B/op 12 allocs/op
|
|
BenchmarkFilterComplex 500000 3032 ns/op 672 B/op 48 allocs/op
|
|
BenchmarkFilterComplex-2 500000 3100 ns/op 672 B/op 48 allocs/op
|
|
BenchmarkFilterComplex-4 500000 6235 ns/op 672 B/op 48 allocs/op
|
|
BenchmarkFilterComplexOr 500000 2649 ns/op 816 B/op 39 allocs/op
|
|
BenchmarkFilterComplexOr-2 500000 2659 ns/op 816 B/op 39 allocs/op
|
|
BenchmarkFilterComplexOr-4 500000 5548 ns/op 816 B/op 39 allocs/op
|
|
BenchmarkGenericFilterSimple 500000 2340 ns/op 1200 B/op 24 allocs/op
|
|
BenchmarkGenericFilterSimple-2 1000000 2307 ns/op 1200 B/op 24 allocs/op
|
|
BenchmarkGenericFilterSimple-4 1000000 3615 ns/op 1200 B/op 24 allocs/op
|
|
BenchmarkGenericFilterComplex 200000 7073 ns/op 1680 B/op 96 allocs/op
|
|
BenchmarkGenericFilterComplex-2 200000 7598 ns/op 1680 B/op 96 allocs/op
|
|
BenchmarkGenericFilterComplex-4 200000 10735 ns/op 1680 B/op 96 allocs/op
|
|
BenchmarkGenericFilterComplexOr 200000 5406 ns/op 2456 B/op 61 allocs/op
|
|
BenchmarkGenericFilterComplexOr-2 300000 5300 ns/op 2456 B/op 61 allocs/op
|
|
BenchmarkGenericFilterComplexOr-4 200000 7707 ns/op 2456 B/op 61 allocs/op
|
|
```
|