-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreflect.go
60 lines (51 loc) · 1.03 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
package climate
import (
"context"
"reflect"
)
var contextType = reflect.TypeOf((*context.Context)(nil)).Elem()
func typeIsContext(t reflect.Type) bool {
return t.Kind() == reflect.Interface && t.Implements(contextType)
}
var errorType = reflect.TypeOf((*error)(nil)).Elem()
func typeIsError(t reflect.Type) bool {
return t.Kind() == reflect.Interface && t.Implements(errorType)
}
func typeIsStructPointer(t reflect.Type) bool {
return t.Kind() == reflect.Pointer && t.Elem().Kind() == reflect.Struct
}
type reflection struct {
ptr *reflection
ot reflect.Type
ov *reflect.Value
}
func (r *reflection) t() reflect.Type {
if r == nil {
return nil
}
if r.ot == nil {
if r.ptr.t() != nil {
r.ot = r.ptr.ot.Elem()
} else {
r.ot = r.ov.Type()
}
}
return r.ot
}
func (r *reflection) v() *reflect.Value {
if r == nil {
return nil
}
if r.ov == nil {
if r.ptr == nil {
r.ptr = &reflection{}
}
if r.ptr.ov == nil {
v := reflect.New(r.t())
r.ptr.ov = &v
}
v := r.ptr.ov.Elem()
r.ov = &v
}
return r.ov
}