-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconvert.go
199 lines (174 loc) · 4.15 KB
/
convert.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
192
193
194
195
196
197
198
199
package compat
import (
"go/types"
)
func ConvertObject(obj types.Object) *Object {
res := &Object{
Symbol: Symbol{
Package: obj.Pkg().Path(),
Name: obj.Name(),
},
}
switch obj := obj.(type) {
case *types.TypeName:
res.Type = TypeDeclaration
if obj.IsAlias() {
res.Type = AliasDeclaration
}
case *types.Func:
res.Type = FuncDeclaration
case *types.Var:
res.Type = VarDeclaration
case *types.Const:
res.Type = ConstDeclaration
}
if res.Type == AliasDeclaration {
res.Definition = typeToShallowDefinition(obj.Type())
return res
}
if _, ok := obj.Type().Underlying().(*types.Basic); ok {
res.Definition = typeToShallowDefinition(obj.Type())
} else {
res.Definition = typeToDefinition(obj.Type())
}
switch typ := obj.Type().(type) {
case *types.Named:
res.Methods = methoderToFuncs(true, typ)
}
return res
}
func typeToShallowDefinition(typ types.Type) *Definition {
switch typ := typ.(type) {
case *types.Basic:
return &Definition{
Type: BasicType,
Symbol: &Symbol{Name: typ.Name()},
}
case *types.Named:
tn := typ.Obj()
var pkg string
if tn.Pkg() != nil {
pkg = tn.Pkg().Path()
}
return &Definition{
Symbol: &Symbol{Package: pkg, Name: tn.Name()},
}
}
return typeToDefinition(typ)
}
func typeToDefinition(typ types.Type) *Definition {
underlying := typ.Underlying()
switch underlying := underlying.(type) {
case *types.Struct:
return structToDefinition(underlying)
case *types.Signature:
return signatureToDefinition(underlying)
case *types.Slice:
return sliceToDefinition(underlying)
case *types.Array:
return arrayToDefinition(underlying)
case *types.Chan:
return chanToDefinition(underlying)
case *types.Map:
return mapToDefinition(underlying)
case *types.Pointer:
return pointerToDefinition(underlying)
case *types.Interface:
return interfaceToDefinition(underlying)
}
panic("unhandled type")
}
func methoderToFuncs(exportedOnly bool, typ methoder) []*Func {
var funcs []*Func
for i := 0; i < typ.NumMethods(); i++ {
method := typ.Method(i)
if exportedOnly && !method.Exported() {
continue
}
funcs = append(funcs, &Func{
Name: method.Name(),
Signature: signatureToSignature(method.Type().(*types.Signature)),
})
}
return funcs
}
func structToDefinition(typ *types.Struct) *Definition {
def := &Definition{
Type: StructType,
}
for i := 0; i < typ.NumFields(); i++ {
field := typ.Field(i)
if !field.Exported() {
continue
}
def.Fields = append(def.Fields, varToField(field))
}
return def
}
func signatureToDefinition(typ *types.Signature) *Definition {
sig := signatureToSignature(typ)
return &Definition{
Type: FuncType,
Signature: &sig,
}
}
func signatureToSignature(typ *types.Signature) Signature {
return Signature{
Params: tupleToSymbols(typ.Params()),
Results: tupleToSymbols(typ.Results()),
Variadic: typ.Variadic(),
}
}
func sliceToDefinition(typ *types.Slice) *Definition {
return &Definition{
Type: SliceType,
Elem: typeToShallowDefinition(typ.Elem()),
}
}
func arrayToDefinition(typ *types.Array) *Definition {
return &Definition{
Type: ArrayType,
Len: typ.Len(),
Elem: typeToShallowDefinition(typ.Elem()),
}
}
func chanToDefinition(typ *types.Chan) *Definition {
return &Definition{
Type: ChanType,
ChanDir: typ.Dir(),
Elem: typeToShallowDefinition(typ.Elem()),
}
}
func mapToDefinition(typ *types.Map) *Definition {
return &Definition{
Type: MapType,
Key: typeToShallowDefinition(typ.Key()),
Elem: typeToShallowDefinition(typ.Elem()),
}
}
func pointerToDefinition(typ *types.Pointer) *Definition {
return &Definition{
Type: PointerType,
Elem: typeToShallowDefinition(typ.Elem()),
}
}
func interfaceToDefinition(typ *types.Interface) *Definition {
return &Definition{
Type: InterfaceType,
Functions: methoderToFuncs(false, typ),
}
}
func tupleToSymbols(tup *types.Tuple) []*Definition {
var res []*Definition
for i := 0; i < tup.Len(); i++ {
field := tup.At(i)
res = append(res, typeToShallowDefinition(field.Type()))
}
return res
}
func varToField(obj *types.Var) *Field {
return &Field{
Name: obj.Name(),
Type: typeToShallowDefinition(obj.Type()),
}
}