-
Notifications
You must be signed in to change notification settings - Fork 273
/
utils.go
103 lines (77 loc) · 2.42 KB
/
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
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
package funk
import (
"fmt"
"reflect"
)
func equal(expectedOrPredicate interface{}, optionalIsMap ...bool) func(keyValueIfMap, actualValue reflect.Value) bool {
isMap := append(optionalIsMap, false)[0]
if IsFunction(expectedOrPredicate) {
inTypes := []reflect.Type{nil}; if isMap {
inTypes = append(inTypes, nil)
}
if !IsPredicate(expectedOrPredicate, inTypes...) {
panic(fmt.Sprintf("Predicate function must have %d parameter and must return boolean", len(inTypes)))
}
predicateValue := reflect.ValueOf(expectedOrPredicate)
return func(keyValueIfMap, actualValue reflect.Value) bool {
if isMap && !keyValueIfMap.Type().ConvertibleTo(predicateValue.Type().In(0)) {
panic("Given key is not compatible with type of parameter for the predicate.")
}
if (isMap && !actualValue.Type().ConvertibleTo(predicateValue.Type().In(1))) ||
(!isMap && !actualValue.Type().ConvertibleTo(predicateValue.Type().In(0))) {
panic("Given value is not compatible with type of parameter for the predicate.")
}
args := []reflect.Value{actualValue}
if isMap {
args = append([]reflect.Value{keyValueIfMap}, args...)
}
return predicateValue.Call(args)[0].Bool()
}
}
expected := expectedOrPredicate
return func(keyValueIfMap, actualValue reflect.Value) bool {
if isMap {
actualValue = keyValueIfMap
}
if expected == nil || actualValue.IsZero() {
return actualValue.Interface() == expected
}
return reflect.DeepEqual(actualValue.Interface(), expected)
}
}
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 && 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
}
}
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)
}