-
Notifications
You must be signed in to change notification settings - Fork 7
/
check.go
158 lines (145 loc) · 3.3 KB
/
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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package gotype
import (
"strings"
)
// Equal Reports whether the type is equal
// Deprecated: should use Identical instead.
func Equal(t0 Type, t1 Type) bool {
return Identical(t0, t1)
}
// Implements reports whether type inter implements interface t.
func Implements(t Type, inter Type) bool {
if inter.Kind() == Declaration {
inter = inter.Declaration()
}
if inter.Kind() != Interface {
return false
}
if t.Kind() == Declaration {
t = t.Declaration()
}
lenMet0 := inter.NumField()
for i := 0; i != lenMet0; i++ {
m0 := inter.Field(i)
name := m0.Name()
m := m0.Declaration()
switch m.Kind() {
case Func:
m1, ok := t.MethodByName(name)
if !ok {
return false
}
if !Identical(m0, m1) {
return false
}
case Interface:
if !Implements(t, m0) {
return false
}
}
}
return true
}
// Identical reports whether t0 and t1 are identical types.
func Identical(t0, t1 Type) bool {
return identical(t0, t1, true)
}
// IdenticalIgnoreTags reports whether t0 and t1 are identical types if tags are ignored.
func IdenticalIgnoreTags(t0, t1 Type) bool {
return identical(t0, t1, false)
}
func identical(t0, t1 Type, cmpTags bool) bool {
if t0 == t1 {
return true
}
if t0.PkgPath() == t1.PkgPath() && t0.Name() == t1.Name() {
return true
}
k0 := t0.Kind()
k1 := t1.Kind()
if k0 != k1 {
return false
}
switch k0 {
case Bool,
Int, Int8, Int16, Int32, Int64,
Uint, Uint8, Uint16, Uint32, Uint64,
Uintptr,
Float32, Float64,
Complex64, Complex128,
String, Byte, Rune, Error:
return true
case Slice, Ptr:
return identical(t0.Elem(), t1.Elem(), cmpTags)
case Array:
return t0.Len() == t1.Len() && identical(t0.Elem(), t1.Elem(), cmpTags)
case Chan:
return t0.ChanDir() == t1.ChanDir() && identical(t0.Elem(), t1.Elem(), cmpTags)
case Map:
return identical(t0.Key(), t1.Key(), cmpTags) && identical(t0.Elem(), t1.Elem(), cmpTags)
case Field:
if cmpTags && t0.Tag() != t1.Tag() {
return false
}
return identical(t0.Elem(), t1.Elem(), cmpTags)
case Scope:
return t0.PkgPath() == t1.PkgPath()
case Declaration:
return identical(t0.Declaration(), t1.Declaration(), cmpTags)
case Func:
numIn0 := t0.NumIn()
numIn1 := t1.NumIn()
if numIn0 != numIn1 {
return false
}
for i := 0; i != numIn0; i++ {
if !identical(t0.In(i), t1.In(i), cmpTags) {
return false
}
}
numOut0 := t0.NumOut()
numOut1 := t1.NumOut()
if numOut0 != numOut1 {
return false
}
for i := 0; i != numOut0; i++ {
if !identical(t0.Out(i), t1.Out(i), cmpTags) {
return false
}
}
return true
case Interface:
numMethod0 := t0.NumMethod()
numMethod1 := t1.NumMethod()
if numMethod0 != numMethod1 {
return false
}
for i := 0; i != numMethod0; i++ {
if !identical(t0.Method(i), t1.Method(i), cmpTags) {
return false
}
}
return true
case Struct:
numField0 := t0.NumField()
numField1 := t1.NumField()
if numField0 != numField1 {
return false
}
for i := 0; i != numField0; i++ {
if !identical(t0.Field(i), t1.Field(i), cmpTags) {
return false
}
}
return true
}
return false
}
// IsBuiltin return it is a built-in base type
func IsBuiltin(t Type) bool {
kind := t.Kind()
if kind <= predeclaredTypesBeg || kind >= predeclaredTypesEnd {
return false
}
return t.Name() == strings.ToLower(kind.String())
}