-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
type.go
191 lines (177 loc) · 3.97 KB
/
type.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package xtype
import (
"go/types"
"strings"
"github.com/dave/jennifer/jen"
)
const ThisVar = "c"
type Signature struct {
Source string
Target string
}
type Type struct {
T types.Type
Interface bool
InterfaceType *types.Interface
Struct bool
StructType *types.Struct
Named bool
NamedType *types.Named
Pointer bool
PointerType *types.Pointer
PointerInner *Type
List bool
ListFixed bool
ListInner *Type
Map bool
MapType *types.Map
MapKey *Type
MapValue *Type
Basic bool
BasicType *types.Basic
}
type JenID struct {
Code *jen.Statement
Variable bool
}
func VariableID(code *jen.Statement) *JenID {
return &JenID{Code: code, Variable: true}
}
func OtherID(code *jen.Statement) *JenID {
return &JenID{Code: code, Variable: false}
}
func TypeOf(t types.Type) *Type {
rt := &Type{}
rt.T = t
switch value := t.(type) {
case *types.Pointer:
rt.Pointer = true
rt.PointerType = value
rt.PointerInner = TypeOf(value.Elem())
case *types.Basic:
rt.Basic = true
rt.BasicType = value
case *types.Map:
rt.Map = true
rt.MapType = value
rt.MapKey = TypeOf(value.Key())
rt.MapValue = TypeOf(value.Elem())
case *types.Slice:
rt.List = true
rt.ListInner = TypeOf(value.Elem())
case *types.Array:
rt.List = true
rt.ListFixed = true
rt.ListInner = TypeOf(value.Elem())
case *types.Named:
underlying := TypeOf(value.Underlying())
underlying.T = value
underlying.Named = true
underlying.NamedType = value
return underlying
case *types.Struct:
rt.Struct = true
rt.StructType = value
case *types.Interface:
rt.Interface = true
rt.InterfaceType = value
default:
panic("unknown types.Type " + t.String())
}
return rt
}
func (t *Type) ID() string {
return t.asID(true, true)
}
func (t *Type) UnescapedID() string {
return t.asID(true, false)
}
func (t *Type) asID(seeNamed, escapeReserved bool) string {
if seeNamed && t.Named {
pkgName := t.NamedType.Obj().Pkg().Name()
name := pkgName + t.NamedType.Obj().Name()
return name
}
if t.List {
return t.ListInner.asID(true, false) + "List"
}
if t.Basic {
if escapeReserved {
return "x" + t.BasicType.String()
}
return t.BasicType.String()
}
if t.Pointer {
return "p" + strings.Title(t.PointerInner.asID(true, false))
}
if t.Map {
return "map" + strings.Title(t.MapKey.asID(true, false)+strings.Title(t.MapValue.asID(true, false)))
}
if t.Struct {
if escapeReserved {
return "xstruct"
}
return "struct"
}
return "unknown"
}
func (t Type) TypeAsJen() *jen.Statement {
if t.Named {
return toCode(t.NamedType, &jen.Statement{})
}
return toCode(t.T, &jen.Statement{})
}
func toCode(t types.Type, st *jen.Statement) *jen.Statement {
switch cast := t.(type) {
case *types.Named:
if cast.Obj().Pkg() == nil {
return st.Id(cast.Obj().Name())
}
return st.Qual(cast.Obj().Pkg().Path(), cast.Obj().Name())
case *types.Map:
key := toCode(cast.Key(), &jen.Statement{})
return toCode(cast.Elem(), st.Map(key))
case *types.Slice:
return toCode(cast.Elem(), st.Index())
case *types.Array:
return toCode(cast.Elem(), st.Index(jen.Lit(int(cast.Len()))))
case *types.Pointer:
return toCode(cast.Elem(), st.Op("*"))
case *types.Basic:
switch cast.Kind() {
case types.String:
return st.String()
case types.Int:
return st.Int()
case types.Int8:
return st.Int8()
case types.Int16:
return st.Int16()
case types.Int32:
return st.Int32()
case types.Int64:
return st.Int64()
case types.Uint:
return st.Uint()
case types.Uint8:
return st.Uint8()
case types.Uint16:
return st.Uint16()
case types.Uint32:
return st.Uint32()
case types.Uint64:
return st.Uint64()
case types.Bool:
return st.Bool()
case types.Complex128:
return st.Complex128()
case types.Complex64:
return st.Complex64()
case types.Float32:
return st.Float32()
case types.Float64:
return st.Float64()
}
}
panic("unsupported type " + t.String())
}