added float support and cleaner code
This commit is contained in:
parent
da12fce77a
commit
0ebcd6967b
5 changed files with 208 additions and 159 deletions
86
helper.go
Normal file
86
helper.go
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
package filter
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
any = '*'
|
||||
anys = string(any)
|
||||
)
|
||||
|
||||
func getValue(v reflect.Value) (s string, b bool, err error) {
|
||||
switch v.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16,
|
||||
reflect.Int32, reflect.Int64:
|
||||
s = strconv.FormatInt(v.Int(), 10)
|
||||
case reflect.Float32, reflect.Float64:
|
||||
s = strconv.FormatFloat(v.Float(), 'f', -1, 64)
|
||||
case reflect.String:
|
||||
s = v.String()
|
||||
case reflect.Bool:
|
||||
b = v.Bool()
|
||||
default:
|
||||
err = errors.New("nil")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func matchFunc(v string) func(string) bool {
|
||||
vl := len(v)
|
||||
switch {
|
||||
case vl >= 3:
|
||||
if v[0] == any && v[vl-1] == any && v[1] != any && v[vl-2] != any {
|
||||
return func(s string) bool {
|
||||
return strings.Contains(s, v[1:vl-1])
|
||||
}
|
||||
}
|
||||
fallthrough
|
||||
case vl >= 2:
|
||||
switch {
|
||||
case v[0] == any && v[1] != any:
|
||||
return func(s string) bool {
|
||||
return strings.HasSuffix(s, v[1:])
|
||||
}
|
||||
case v[vl-1] == any && v[vl-2] != any:
|
||||
return func(s string) bool {
|
||||
return strings.HasPrefix(s, v[:vl-1])
|
||||
}
|
||||
}
|
||||
}
|
||||
v = strings.Replace(v, anys+anys, anys, -1)
|
||||
return func(s string) bool {
|
||||
if s == v {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
func boolFunc(v string) func(bool) bool {
|
||||
f := func(b bool) bool {
|
||||
return false
|
||||
}
|
||||
if len(v) == 0 {
|
||||
return f
|
||||
}
|
||||
if v[0] == 't' {
|
||||
return func(b bool) bool {
|
||||
if b {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
if v[0] == 'f' {
|
||||
return func(b bool) bool {
|
||||
if !b {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
return f
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue