added sort support

This commit is contained in:
ston1th 2017-08-30 22:01:19 +02:00
commit 1674abae73
5 changed files with 377 additions and 66 deletions

View file

@ -12,6 +12,37 @@ const (
anys = string(any)
)
type less func(i, j int) bool
func reverse(f less) less {
return func(i, j int) bool {
return f(j, i)
}
}
func index(f Filter, i int, fields []string) (val reflect.Value) {
val = reflect.ValueOf(f.Index(i)).FieldByName(fields[0])
for j := 1; j < len(fields); j++ {
val = val.FieldByName(fields[j])
}
return
}
func makeLess(f Filter, fields []string) less {
switch index(f, 0, fields).Kind() {
case reflect.Int, reflect.Int8, reflect.Int16,
reflect.Int32, reflect.Int64:
return func(i, j int) bool { return index(f, i, fields).Int() < index(f, j, fields).Int() }
case reflect.Float32, reflect.Float64:
return func(i, j int) bool { return index(f, i, fields).Float() < index(f, j, fields).Float() }
case reflect.String:
return func(i, j int) bool { return index(f, i, fields).String() < index(f, j, fields).String() }
case reflect.Bool:
return func(i, _ int) bool { return index(f, i, fields).Bool() }
}
return nil
}
func getValue(v reflect.Value) (s string, b bool, err error) {
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16,