forked from thoas/go-funk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
56 lines (41 loc) · 987 Bytes
/
utils.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
package funk
import "reflect"
func equal(expected, actual interface{}) bool {
if expected == nil || actual == nil {
return expected == actual
}
return reflect.DeepEqual(expected, actual)
}
func sliceElem(rtype reflect.Type) reflect.Type {
for {
if rtype.Kind() != reflect.Slice && rtype.Kind() != reflect.Array {
return rtype
}
rtype = rtype.Elem()
}
}
func redirectValue(value reflect.Value) reflect.Value {
for {
if !value.IsValid() || value.Kind() != reflect.Ptr {
return value
}
res := reflect.Indirect(value)
// Test for a circular type.
if res.Kind() == reflect.Ptr && value.Pointer() == res.Pointer() {
return value
}
value = res
}
}
func makeSlice(value reflect.Value, values ...int) reflect.Value {
sliceType := sliceElem(value.Type())
size := value.Len()
cap := size
if len(values) > 0 {
size = values[0]
}
if len(values) > 1 {
cap = values[1]
}
return reflect.MakeSlice(reflect.SliceOf(sliceType), size, cap)
}