-
Notifications
You must be signed in to change notification settings - Fork 10
/
reflect.go
115 lines (100 loc) · 2.79 KB
/
reflect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package reflect
import (
"bytes"
"reflect"
)
// IsBlank defined
func IsBlank(value reflect.Value) bool {
switch value.Kind() {
case reflect.String:
return value.Len() == 0
case reflect.Bool:
return !value.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return value.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return value.Uint() == 0
case reflect.Float32, reflect.Float64:
return value.Float() == 0
case reflect.Interface, reflect.Ptr:
return value.IsNil()
}
return reflect.DeepEqual(value.Interface(), reflect.Zero(value.Type()).Interface())
}
// OrOne defined TODO
func OrOne(arr ...interface{}) interface{} {
for i := range arr {
item := arr[i]
if !IsBlank(reflect.ValueOf(item)) {
return item
}
}
return reflect.Zero(SliceElem(reflect.TypeOf(arr))).Interface()
}
// SliceElem defined
func SliceElem(rtype reflect.Type) reflect.Type {
for {
if rtype.Kind() != reflect.Slice && rtype.Kind() != reflect.Array {
return rtype
}
rtype = rtype.Elem()
}
}
// RedirectValue defined
func RedirectValue(value reflect.Value) reflect.Value {
for {
if !value.IsValid() || (value.Kind() != reflect.Ptr && value.Kind() != reflect.Interface) {
return value
}
res := value.Elem()
// Test for a circular type.
if res.Kind() == reflect.Ptr && value.Kind() == reflect.Ptr && value.Pointer() == res.Pointer() {
return value
}
if !res.IsValid() && value.Kind() == reflect.Ptr {
return reflect.Zero(value.Type().Elem())
}
value = res
}
}
// IsFunction returns if the argument is a function.
func IsFunction(in interface{}, num ...int) bool {
funcType := reflect.TypeOf(in)
result := funcType != nil && funcType.Kind() == reflect.Func
if len(num) >= 1 {
result = result && funcType.NumIn() == num[0]
}
if len(num) == 2 {
result = result && funcType.NumOut() == num[1]
}
return result
}
// IsEqual returns if the two objects are equal
func IsEqual(expected interface{}, actual interface{}) bool {
if expected == nil || actual == nil {
return expected == actual
}
if exp, ok := expected.([]byte); ok {
act, ok := actual.([]byte)
if !ok {
return false
}
if exp == nil || act == nil {
return true
}
return bytes.Equal(exp, act)
}
return reflect.DeepEqual(expected, actual)
}
// IsType returns if the two objects are in the same type
func IsType(expected interface{}, actual interface{}) bool {
return IsEqual(reflect.TypeOf(expected), reflect.TypeOf(actual))
}
// Equal returns if the two objects are equal
func Equal(expected interface{}, actual interface{}) bool {
return IsEqual(expected, actual)
}
// NotEqual returns if the two objects are not equal
func NotEqual(expected interface{}, actual interface{}) bool {
return !IsEqual(expected, actual)
}