| 1 |
package expect |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
) |
| 6 |
|
| 7 |
func SameKind(a, b interface{}) (reflect.Kind, bool) { |
| 8 |
aKind := reflect.ValueOf(a).Kind() |
| 9 |
bKind := reflect.ValueOf(b).Kind() |
| 10 |
if aKind == bKind { |
| 11 |
return aKind, true |
| 12 |
} |
| 13 |
|
| 14 |
if aKind == reflect.Slice && bKind == reflect.String { |
| 15 |
return reflect.Slice, true |
| 16 |
} |
| 17 |
if aKind == reflect.String && bKind == reflect.Slice { |
| 18 |
return reflect.String, true |
| 19 |
} |
| 20 |
|
| 21 |
return aKind, false |
| 22 |
} |
| 23 |
|
| 24 |
func IsNumeric(a interface{}) bool { |
| 25 |
if IsInt(a) || IsUint(a) { |
| 26 |
return true |
| 27 |
} |
| 28 |
kind := reflect.ValueOf(a).Kind() |
| 29 |
return kind == reflect.Float32 || kind == reflect.Float64 |
| 30 |
} |
| 31 |
|
| 32 |
func IsNil(a interface{}) bool { |
| 33 |
if a == nil { |
| 34 |
return true |
| 35 |
} |
| 36 |
value := reflect.ValueOf(a) |
| 37 |
switch value.Kind() { |
| 38 |
case reflect.Chan, reflect.Func, reflect.Interface: |
| 39 |
fallthrough |
| 40 |
case reflect.Ptr, reflect.Map, reflect.Slice: |
| 41 |
return value.IsNil() |
| 42 |
} |
| 43 |
return false |
| 44 |
} |
| 45 |
|
| 46 |
func IsInt(a interface{}) bool { |
| 47 |
kind := reflect.ValueOf(a).Kind() |
| 48 |
return kind == reflect.Int || kind == reflect.Int8 || kind == reflect.Int16 || kind == reflect.Int32 || kind == reflect.Int64 |
| 49 |
} |
| 50 |
|
| 51 |
func IsUint(a interface{}) bool { |
| 52 |
kind := reflect.ValueOf(a).Kind() |
| 53 |
return kind == reflect.Uint || kind == reflect.Uint8 || kind == reflect.Uint16 || kind == reflect.Uint32 || kind == reflect.Uint64 |
| 54 |
} |
| 55 |
|
| 56 |
func ToInt64(a, b interface{}) (interface{}, interface{}) { |
| 57 |
return reflect.ValueOf(a).Int(), reflect.ValueOf(b).Int() |
| 58 |
} |
| 59 |
|
| 60 |
func ToUint64(a, b interface{}) (interface{}, interface{}) { |
| 61 |
return reflect.ValueOf(a).Uint(), reflect.ValueOf(b).Uint() |
| 62 |
} |
| 63 |
|
| 64 |
func IsString(a interface{}) bool { |
| 65 |
return reflect.ValueOf(a).Kind() == reflect.String |
| 66 |
} |
| 67 |
|
| 68 |
func IsSlice(a interface{}) bool { |
| 69 |
return reflect.ValueOf(a).Kind() == reflect.Slice |
| 70 |
} |
| 71 |
|
| 72 |
func ToString(a interface{}) string { |
| 73 |
return reflect.ValueOf(a).String() |
| 74 |
} |