| 43 | ) |
| 44 | |
| 45 | func compare(obj1, obj2 interface{}, kind reflect.Kind) (compareResult, bool) { |
| 46 | obj1Value := reflect.ValueOf(obj1) |
| 47 | obj2Value := reflect.ValueOf(obj2) |
| 48 | |
| 49 | // throughout this switch we try and avoid calling .Convert() if possible, |
| 50 | // as this has a pretty big performance impact |
| 51 | switch kind { |
| 52 | case reflect.Int: |
| 53 | { |
| 54 | intobj1, ok := obj1.(int) |
| 55 | if !ok { |
| 56 | intobj1 = obj1Value.Convert(intType).Interface().(int) |
| 57 | } |
| 58 | intobj2, ok := obj2.(int) |
| 59 | if !ok { |
| 60 | intobj2 = obj2Value.Convert(intType).Interface().(int) |
| 61 | } |
| 62 | if intobj1 > intobj2 { |
| 63 | return compareGreater, true |
| 64 | } |
| 65 | if intobj1 == intobj2 { |
| 66 | return compareEqual, true |
| 67 | } |
| 68 | if intobj1 < intobj2 { |
| 69 | return compareLess, true |
| 70 | } |
| 71 | } |
| 72 | case reflect.Int8: |
| 73 | { |
| 74 | int8obj1, ok := obj1.(int8) |
| 75 | if !ok { |
| 76 | int8obj1 = obj1Value.Convert(int8Type).Interface().(int8) |
| 77 | } |
| 78 | int8obj2, ok := obj2.(int8) |
| 79 | if !ok { |
| 80 | int8obj2 = obj2Value.Convert(int8Type).Interface().(int8) |
| 81 | } |
| 82 | if int8obj1 > int8obj2 { |
| 83 | return compareGreater, true |
| 84 | } |
| 85 | if int8obj1 == int8obj2 { |
| 86 | return compareEqual, true |
| 87 | } |
| 88 | if int8obj1 < int8obj2 { |
| 89 | return compareLess, true |
| 90 | } |
| 91 | } |
| 92 | case reflect.Int16: |
| 93 | { |
| 94 | int16obj1, ok := obj1.(int16) |
| 95 | if !ok { |
| 96 | int16obj1 = obj1Value.Convert(int16Type).Interface().(int16) |
| 97 | } |
| 98 | int16obj2, ok := obj2.(int16) |
| 99 | if !ok { |
| 100 | int16obj2 = obj2Value.Convert(int16Type).Interface().(int16) |
| 101 | } |
| 102 | if int16obj1 > int16obj2 { |