-
Notifications
You must be signed in to change notification settings - Fork 2
/
check.go
34 lines (29 loc) · 848 Bytes
/
check.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
package goctx
import (
"context"
"reflect"
)
func checkField(typ reflect.Type, name string, offset uintptr, exp reflect.Type) bool {
field, ok := typ.FieldByName(name)
if !ok {
return false
}
if field.Offset != offset {
return false
}
if field.Type != exp {
return false
}
return true
}
func checkContextField(typ reflect.Type, name string, offset uintptr) bool {
return checkField(typ, name, offset, reflect.TypeOf((*context.Context)(nil)).Elem())
}
func checkInterfaceField(typ reflect.Type, name string, offset uintptr) bool {
return checkField(typ, name, offset, reflect.TypeOf((*interface{})(nil)).Elem())
}
func checkCancelContextField(typ reflect.Type, name string, offset uintptr) bool {
ctx, cancel := context.WithCancel(context.TODO())
cancel()
return checkField(typ, name, offset, reflect.TypeOf(ctx).Elem())
}