filter/helper.go

92 lines
1.5 KiB
Go

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 ft(b bool) bool {
if b {
return true
}
return false
}
func ff(b bool) bool {
if !b {
return true
}
return false
}
func f(b bool) bool {
return false
}
func boolFunc(v string) func(bool) bool {
if len(v) == 0 {
return f
}
if v[0] == 't' {
return ft
}
if v[0] == 'f' {
return ff
}
return f
}