-
Notifications
You must be signed in to change notification settings - Fork 39
/
context.go
44 lines (35 loc) · 965 Bytes
/
context.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
package minimock
import (
"context"
"reflect"
)
func copyValue(value interface{}) reflect.Value {
rValue := reflect.ValueOf(value)
newValue := reflect.New(rValue.Type()).Elem()
newValue.Set(rValue)
return newValue
}
func checkAnyContext(eFieldValue, aFieldValue interface{}) bool {
_, ok := eFieldValue.(anyContext)
if ok {
if ctx, ok := aFieldValue.(context.Context); ok && ctx != nil {
return true
}
}
return false
}
func setAnyContext(src, dst interface{}) interface{} {
srcp := copyValue(src)
dstp := copyValue(dst)
for i := 0; i < srcp.NumField(); i++ {
srcFieldValue := unexportedVal(srcp.Field(i))
dstFieldValue := unexportedVal(dstp.Field(i))
if checkAnyContext(srcFieldValue.Interface(), dstFieldValue.Interface()) {
// we set context field to anyContext because
// we don't want to display diff between two contexts in case
// of anyContext
dstFieldValue.Set(srcFieldValue)
}
}
return dstp.Interface()
}