From a178b5d553c3b0748b9b022031e38febf87ccb12 Mon Sep 17 00:00:00 2001 From: Janet Kuo Date: Fri, 13 May 2016 16:40:48 -0700 Subject: [PATCH] Support sort-by timestamp in kubectl get --- pkg/kubectl/sorting_printer.go | 30 ++++-------------------------- 1 file changed, 4 insertions(+), 26 deletions(-) diff --git a/pkg/kubectl/sorting_printer.go b/pkg/kubectl/sorting_printer.go index c2d15f3180..577ee0d9ae 100644 --- a/pkg/kubectl/sorting_printer.go +++ b/pkg/kubectl/sorting_printer.go @@ -156,10 +156,10 @@ func isLess(i, j reflect.Value) (bool, error) { case reflect.Ptr: return isLess(i.Elem(), j.Elem()) case reflect.Struct: - // special case handling - lessFuncList := []structLessFunc{timeLess} - if ok, less := structLess(i, j, lessFuncList); ok { - return less, nil + // sort unversioned.Time + in := i.Interface() + if t, ok := in.(unversioned.Time); ok { + return t.Before(j.Interface().(unversioned.Time)), nil } // fallback to the fields comparision for idx := 0; idx < i.NumField(); idx++ { @@ -183,28 +183,6 @@ func isLess(i, j reflect.Value) (bool, error) { } } -// structLessFunc checks whether i and j could be compared(the first return value), -// and if it could, return whether i is less than j(the second return value) -type structLessFunc func(i, j reflect.Value) (bool, bool) - -// structLess returns whether i and j could be compared with the given function list -func structLess(i, j reflect.Value, lessFuncList []structLessFunc) (bool, bool) { - for _, lessFunc := range lessFuncList { - if ok, less := lessFunc(i, j); ok { - return ok, less - } - } - return false, false -} - -// compare two unversioned.Time values. -func timeLess(i, j reflect.Value) (bool, bool) { - if i.Type() != reflect.TypeOf(unversioned.Unix(0, 0)) { - return false, false - } - return true, i.MethodByName("Before").Call([]reflect.Value{j})[0].Bool() -} - func (r *RuntimeSort) Less(i, j int) bool { iObj := r.objs[i] jObj := r.objs[j]