/
rtype.go
136 lines (109 loc) · 2.52 KB
/
rtype.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package types
import (
"go/types"
"reflect"
)
func FromRType(rtype reflect.Type) *RType {
return &RType{Type: rtype}
}
type RType struct {
reflect.Type
}
func (rtype *RType) Unwrap() any {
return rtype.Type
}
func (rtype *RType) Method(i int) Method {
return &RMethod{Method: rtype.Type.Method(i)}
}
func (rtype *RType) MethodByName(name string) (Method, bool) {
if m, ok := rtype.Type.MethodByName(name); ok {
return &RMethod{Method: m}, true
}
return nil, false
}
func (rtype *RType) In(i int) Type {
return FromRType(rtype.Type.In(i))
}
func (rtype *RType) Out(i int) Type {
return FromRType(rtype.Type.Out(i))
}
func (rtype *RType) Implements(u Type) bool {
switch x := u.(type) {
case *RType:
return rtype.Type.Implements(x.Type)
case *TType:
if rtype.PkgPath() == "" {
return false
}
if i, ok := x.Type.(*types.Interface); ok {
return types.Implements(NewTypesTypeFromReflectType(rtype.Type), i)
}
}
return false
}
func (rtype *RType) AssignableTo(u Type) bool {
return rtype.Type.AssignableTo(u.(*RType).Type)
}
func (rtype *RType) ConvertibleTo(u Type) bool {
return rtype.Type.ConvertibleTo(u.(*RType).Type)
}
func (rtype *RType) Field(i int) StructField {
return &RStructField{
StructField: rtype.Type.Field(i),
}
}
func (rtype *RType) FieldByName(name string) (StructField, bool) {
if sf, ok := rtype.Type.FieldByName(name); ok {
return &RStructField{
StructField: sf,
}, true
}
return nil, false
}
func (rtype *RType) FieldByNameFunc(match func(string) bool) (StructField, bool) {
if sf, ok := rtype.Type.FieldByNameFunc(match); ok {
return &RStructField{
StructField: sf,
}, true
}
return nil, false
}
func (rtype *RType) Key() Type {
return FromRType(rtype.Type.Key())
}
func (rtype *RType) Elem() Type {
return FromRType(rtype.Type.Elem())
}
func (rtype *RType) String() string {
return typeString(rtype)
}
type RStructField struct {
StructField reflect.StructField
}
func (f *RStructField) PkgPath() string {
return f.StructField.PkgPath
}
func (f *RStructField) Name() string {
return f.StructField.Name
}
func (f *RStructField) Tag() reflect.StructTag {
return f.StructField.Tag
}
func (f *RStructField) Type() Type {
return FromRType(f.StructField.Type)
}
func (f *RStructField) Anonymous() bool {
return f.StructField.Anonymous
}
type RMethod struct {
Method reflect.Method
}
func (m *RMethod) PkgPath() string {
return m.Method.PkgPath
}
func (m *RMethod) Name() string {
return m.Method.Name
}
func (m *RMethod) Type() Type {
return FromRType(m.Method.Type)
}