-
Notifications
You must be signed in to change notification settings - Fork 0
/
checktype.go
152 lines (128 loc) · 3.26 KB
/
checktype.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
package gospec
import "go/types"
// basic types
func IsBoolean(typ types.Type) bool {
t, ok := typ.Underlying().(*types.Basic)
return ok && t.Info()&types.IsBoolean != 0
}
func IsInteger(typ types.Type) bool {
t, ok := typ.Underlying().(*types.Basic)
return ok && t.Info()&types.IsInteger != 0
}
func IsUnsigned(typ types.Type) bool {
t, ok := typ.Underlying().(*types.Basic)
return ok && t.Info()&types.IsUnsigned != 0
}
func IsFloat(typ types.Type) bool {
t, ok := typ.Underlying().(*types.Basic)
return ok && t.Info()&types.IsFloat != 0
}
func IsComplex(typ types.Type) bool {
t, ok := typ.Underlying().(*types.Basic)
return ok && t.Info()&types.IsComplex != 0
}
func IsString(typ types.Type) bool {
t, ok := typ.Underlying().(*types.Basic)
return ok && t.Info()&types.IsString != 0
}
func IsUntyped(typ types.Type) bool {
t, ok := typ.Underlying().(*types.Basic)
return ok && t.Info()&types.IsUntyped != 0
}
func IsTyped(typ types.Type) bool {
t, ok := typ.Underlying().(*types.Basic)
return !ok || t.Info()&types.IsUntyped == 0
}
// IsNumeric = IsInteger | IsFloat | IsComplex
func IsNumeric(typ types.Type) bool {
t, ok := typ.Underlying().(*types.Basic)
return ok && t.Info()&types.IsNumeric != 0
}
// IsOrdered = IsInteger | IsFloat | IsString
func IsOrdered(typ types.Type) bool {
t, ok := typ.Underlying().(*types.Basic)
return ok && t.Info()&types.IsOrdered != 0
}
// IsConstType = IsBoolean | IsNumeric | IsString
func IsConstType(typ types.Type) bool {
t, ok := typ.Underlying().(*types.Basic)
return ok && t.Info()&types.IsConstType != 0
}
// other type
func IsByte(typ types.Type) bool {
t, ok := typ.Underlying().(*types.Basic)
return ok && t.Kind() == types.Byte
}
func IsRune(typ types.Type) bool {
t, ok := typ.Underlying().(*types.Basic)
return ok && t.Kind() == types.Rune
}
//check types.Type if be a basic, pointer, function, slice, map, channel, or interface
func ToBasic(t types.Type) (*types.Basic, bool) {
if t == nil {
return nil, false
}
tu := t.Underlying()
tb, ok := tu.(*types.Basic)
return tb, ok
}
func ToPointer(t types.Type) (*types.Pointer, bool) {
if t == nil {
return nil, false
}
tu := t.Underlying()
tp, ok := tu.(*types.Pointer)
return tp, ok
}
func ToFunction(t types.Type) (*types.Signature, bool) {
if t == nil {
return nil, false
}
tu := t.Underlying()
tf, ok := tu.(*types.Signature)
return tf, ok
}
func ToSlice(t types.Type) (*types.Slice, bool) {
if t == nil {
return nil, false
}
tu := t.Underlying()
ts, ok := tu.(*types.Slice)
return ts, ok
}
func ToMap(t types.Type) (*types.Map, bool) {
if t == nil {
return nil, false
}
tu := t.Underlying()
tm, ok := tu.(*types.Map)
return tm, ok
}
func ToInterface(t types.Type) (*types.Interface, bool) {
if t == nil {
return nil, false
}
tu := t.Underlying()
ti, ok := tu.(*types.Interface)
return ti, ok
}
func ToChan(t types.Type) (*types.Chan, bool) {
if t == nil {
return nil, false
}
tu := t.Underlying()
tc, ok := tu.(*types.Chan)
return tc, ok
}
//check types.Object if be a const
func ToConstObject(o types.Object) (*types.Const, bool) {
if o == nil {
return nil, false
}
constObj, ok := o.(*types.Const)
return constObj, ok
}
func IsConstObject(o types.Object) bool {
_, isConstObject := ToConstObject(o)
return isConstObject
}