# 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}}}, } ``` ### Explicit match ``` filter.NewQuery("Bool:true").Filter(&data) data = [{str1 42 true {s1 {1}}} {str2 42 true {s2 {2}}}] ``` ### Wildcard match ``` filter.NewQuery("String:s*").Filter(&data) data = [{str1 42 true {s1 {1}}} {str3 42 false {s3 {3}}}] ``` ### Nested struct match ``` filter.NewQuery("Struct.Struct.Int:3").Filter(&data) data = [{str3 42 false {s3 {3}}}] ``` ### Multiple matches ``` filter.NewQuery("Struct.String:*2 Bool:true").Filter(&data) data = [{2str 42 true {s2 {2}}}] ``` ## Benchmarks ``` go test -cpu 1,2,4 --bench . BenchmarkFilterSimple 2000000 899 ns/op 432 B/op 12 allocs/op BenchmarkFilterSimple-2 2000000 896 ns/op 432 B/op 12 allocs/op BenchmarkFilterSimple-4 1000000 1439 ns/op 432 B/op 12 allocs/op BenchmarkGenericFilterSimple 500000 2438 ns/op 1200 B/op 24 allocs/op BenchmarkGenericFilterSimple-2 500000 2406 ns/op 1200 B/op 24 allocs/op BenchmarkGenericFilterSimple-4 500000 2645 ns/op 1200 B/op 24 allocs/op BenchmarkFilterComplex 500000 2929 ns/op 672 B/op 42 allocs/op BenchmarkFilterComplex-2 500000 2918 ns/op 672 B/op 42 allocs/op BenchmarkFilterComplex-4 500000 5574 ns/op 672 B/op 42 allocs/op BenchmarkGenericFilterComplex 200000 7343 ns/op 1680 B/op 84 allocs/op BenchmarkGenericFilterComplex-2 200000 7400 ns/op 1680 B/op 84 allocs/op BenchmarkGenericFilterComplex-4 200000 11979 ns/op 1680 B/op 84 allocs/op ```