added float support and cleaner code

This commit is contained in:
ston1th 2017-07-09 20:04:05 +02:00
commit 0ebcd6967b
5 changed files with 208 additions and 159 deletions

167
filter.go
View file

@ -6,12 +6,9 @@ package filter
import (
"errors"
"reflect"
"strconv"
"strings"
)
const any = '*'
// Filter is the filter interface to be implemented for faster non generic filtering
// Example implementation:
// type f []struct
@ -26,7 +23,8 @@ type Filter interface {
type match struct {
fields []string
value string
mf func(string) bool
bf func(bool) bool
}
// Logic is the filter logic
@ -50,7 +48,7 @@ type Query struct {
// NewQuery parses the filter query and match logic
// Examples:
// Explicit match: <struct field>:<struct value>
// Wildcard match:
// Wildcard match (use ** to match the '*' character):
// <struct field>:*<part of struct value>
// <struct field>:<part of struct value>*
// <struct field>:*<middl of struct value>*
@ -58,13 +56,17 @@ type Query struct {
// <struct field>.<struct field>:<struct value>
// Multiple matches:
// <struct field>:<struct value> <struct field>:<struct value>*
// Supported data types are: string, int, bool
// Supported data types are: string, int, float, bool
// Bool does not support the wildcard operator
func NewQuery(s string, l Logic) (q *Query, err error) {
if l > 2 || l < 0 {
l = AND
}
q = &Query{l: l}
if s == "" {
err = errors.New("query is empty")
return
}
qm := make(map[string]struct{})
fields := strings.Split(s, " ")
for _, v := range fields {
@ -83,7 +85,11 @@ func NewQuery(s string, l Logic) (q *Query, err error) {
}
qm[m[0]] = struct{}{}
}
q.m = append(q.m, match{strings.Split(m[0], "."), m[1]})
q.m = append(q.m, match{
strings.Split(m[0], "."),
matchFunc(m[1]),
boolFunc(m[1]),
})
}
return
}
@ -95,26 +101,23 @@ func (q *Query) check(oks []bool) (b bool) {
for _, ok := range oks {
if !ok {
b = false
return
}
}
case OR:
b = false
case NOT:
for _, ok := range oks {
if ok {
b = false
return
}
}
}
return
}
func (q *Query) match(v reflect.Value) bool {
if q.l == OR {
return q.orMatch(v)
}
return q.allMatch(v)
}
func (q *Query) allMatch(v reflect.Value) (ok bool) {
func (q *Query) match(v reflect.Value) (ok bool) {
if v.Kind() != reflect.Struct {
return
}
@ -122,11 +125,15 @@ func (q *Query) allMatch(v reflect.Value) (ok bool) {
if ql == 0 {
return
}
oks := make([]bool, ql)
var (
oks []bool
match bool
)
if q.l != OR {
oks = make([]bool, ql)
}
for i, qv := range q.m {
s := ""
fl := len(qv.fields)
vl := len(qv.value)
if fl == 0 {
continue
}
@ -134,125 +141,27 @@ func (q *Query) allMatch(v reflect.Value) (ok bool) {
for j := 1; j < fl; j++ {
val = val.FieldByName(qv.fields[j])
}
switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16,
reflect.Int32, reflect.Int64:
s = strconv.Itoa(int(val.Int()))
case reflect.String:
s = val.String()
case reflect.Bool:
if vl == 0 {
continue
}
b := val.Bool()
if b && qv.value[0] == 't' {
oks[i] = true
continue
}
if !b && qv.value[0] == 'f' {
oks[i] = true
}
s, b, err := getValue(val)
if err != nil {
continue
}
switch s {
case "":
match = qv.bf(b)
default:
match = qv.mf(s)
}
if q.l == OR {
if match {
return true
}
continue
}
switch {
case vl >= 3:
if qv.value[0] == any && qv.value[vl-1] == any {
if strings.Contains(s, qv.value[1:vl-1]) {
oks[i] = true
continue
}
}
fallthrough
case vl >= 2:
switch {
case qv.value[0] == any:
if strings.HasSuffix(s, qv.value[1:]) {
oks[i] = true
continue
}
case qv.value[vl-1] == any:
if strings.HasPrefix(s, qv.value[:vl-1]) {
oks[i] = true
continue
}
}
}
if s == qv.value {
oks[i] = true
}
oks[i] = match
}
return q.check(oks)
}
func (q *Query) orMatch(v reflect.Value) bool {
if v.Kind() != reflect.Struct {
return false
}
ql := len(q.m)
if ql == 0 {
return false
}
for _, qv := range q.m {
s := ""
fl := len(qv.fields)
vl := len(qv.value)
if fl == 0 {
continue
}
val := v.FieldByName(qv.fields[0])
for j := 1; j < fl; j++ {
val = val.FieldByName(qv.fields[j])
}
switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16,
reflect.Int32, reflect.Int64:
s = strconv.Itoa(int(val.Int()))
case reflect.String:
s = val.String()
case reflect.Bool:
if vl == 0 {
continue
}
b := val.Bool()
if b && qv.value[0] == 't' {
return true
}
if !b && qv.value[0] == 'f' {
return true
}
continue
default:
continue
}
switch {
case vl >= 3:
if qv.value[0] == any && qv.value[vl-1] == any {
if strings.Contains(s, qv.value[1:vl-1]) {
return true
}
}
fallthrough
case vl >= 2:
switch {
case qv.value[0] == any:
if strings.HasSuffix(s, qv.value[1:]) {
return true
}
case qv.value[vl-1] == any:
if strings.HasPrefix(s, qv.value[:vl-1]) {
return true
}
}
}
if s == qv.value {
return true
}
}
return false
}
// GenericFilter takes any slice of structs value and returns the same slice type containing only the filter matches.
// Returns nil if interface{} is not a slice.
func (q *Query) GenericFilter(i interface{}) interface{} {