-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
types.go
238 lines (230 loc) · 6.2 KB
/
types.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package valast
import (
"fmt"
"go/ast"
"go/token"
"reflect"
)
type cacheKeyOptions struct {
Unqualify bool
PackagePath string
PackageName string
ExportedOnly bool
}
type cacheKey struct {
v reflect.Type
opt cacheKeyOptions
}
func newCacheKey(v reflect.Type, opt *Options) cacheKey {
return cacheKey{v: v, opt: cacheKeyOptions{
Unqualify: opt.Unqualify,
PackagePath: opt.PackagePath,
PackageName: opt.PackageName,
ExportedOnly: opt.ExportedOnly,
}}
}
type typeExprCache map[cacheKey]Result
// typeExpr returns an AST type expression for the value v.
//
// It is cached to avoid building type expressions again for types we've already seen, which can
// get quite complex (see BenchmarkComplexType.)
func typeExpr(v reflect.Type, opt *Options, cache typeExprCache) (Result, error) {
key := newCacheKey(v, opt)
if cached, ok := cache[key]; ok {
return cached, nil
}
result, err := uncachedTypeExpr(v, opt, cache)
if err != nil {
return Result{}, err
}
cache[key] = result
return result, nil
}
func uncachedTypeExpr(v reflect.Type, opt *Options, cache typeExprCache) (Result, error) {
if v.Kind() != reflect.UnsafePointer && v.Name() != "" {
pkgPath := v.PkgPath()
if pkgPath != "" && pkgPath != opt.PackagePath {
pkgName, err := opt.packagePathToName(v.PkgPath())
if err != nil {
return Result{}, err
}
if pkgName != opt.PackageName {
return Result{
AST: &ast.SelectorExpr{X: ast.NewIdent(pkgName), Sel: ast.NewIdent(v.Name())},
RequiresUnexported: !ast.IsExported(v.Name()),
}, nil
}
}
return Result{
AST: ast.NewIdent(v.Name()),
RequiresUnexported: false,
}, nil
}
switch v.Kind() {
case reflect.Array:
elemType, err := typeExpr(v.Elem(), opt, cache)
if err != nil {
return Result{}, err
}
return Result{
AST: &ast.ArrayType{
Len: &ast.BasicLit{Kind: token.INT, Value: fmt.Sprint(v.Len())},
Elt: elemType.AST,
},
RequiresUnexported: elemType.RequiresUnexported,
}, nil
case reflect.Interface:
var methods []*ast.Field
var requiresUnexported bool
for i := 0; i < v.NumMethod(); i++ {
method := v.Method(i)
methodType, err := typeExpr(method.Type, opt, cache)
if err != nil {
return Result{}, err
}
if methodType.RequiresUnexported {
requiresUnexported = true
}
methods = append(methods, &ast.Field{
Names: []*ast.Ident{ast.NewIdent(method.Name)},
Type: methodType.AST,
})
}
return Result{
AST: &ast.InterfaceType{Methods: &ast.FieldList{List: methods}},
RequiresUnexported: requiresUnexported,
}, nil
case reflect.Func:
// Note: reflect cannot determine parameter/result names. See https://groups.google.com/g/golang-nuts/c/nM_ZhL7fuGc
var (
requiresUnexported bool
params []*ast.Field
)
for i := 0; i < v.NumIn(); i++ {
param := v.In(i)
paramType, err := typeExpr(param, opt, cache)
if err != nil {
return Result{}, err
}
if paramType.RequiresUnexported {
requiresUnexported = true
}
params = append(params, &ast.Field{
Type: paramType.AST,
})
}
var results []*ast.Field
for i := 0; i < v.NumOut(); i++ {
result := v.Out(i)
resultType, err := typeExpr(result, opt, cache)
if err != nil {
return Result{}, err
}
if resultType.RequiresUnexported {
requiresUnexported = true
}
results = append(results, &ast.Field{
Type: resultType.AST,
})
}
return Result{
AST: &ast.FuncType{
Params: &ast.FieldList{List: params},
Results: &ast.FieldList{List: results},
},
RequiresUnexported: requiresUnexported,
}, nil
case reflect.Map:
keyType, err := typeExpr(v.Key(), opt, cache)
if err != nil {
return Result{}, err
}
valueType, err := typeExpr(v.Elem(), opt, cache)
if err != nil {
return Result{}, err
}
return Result{
AST: &ast.MapType{
Key: keyType.AST,
Value: valueType.AST,
},
RequiresUnexported: keyType.RequiresUnexported || valueType.RequiresUnexported,
}, nil
case reflect.Ptr:
ptrType, err := typeExpr(v.Elem(), opt, cache)
if err != nil {
return Result{}, err
}
return Result{
AST: &ast.StarExpr{X: ptrType.AST},
RequiresUnexported: ptrType.RequiresUnexported,
}, nil
case reflect.Slice:
elemType, err := typeExpr(v.Elem(), opt, cache)
if err != nil {
return Result{}, err
}
return Result{
AST: &ast.ArrayType{Elt: elemType.AST},
RequiresUnexported: elemType.RequiresUnexported,
}, nil
case reflect.Struct:
var (
fields []*ast.Field
requiresUnexported, omittedUnexported bool
)
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
fieldType, err := typeExpr(field.Type, opt, cache)
if err != nil {
return Result{}, err
}
if fieldType.RequiresUnexported {
requiresUnexported = true
if opt.ExportedOnly {
return Result{RequiresUnexported: true}, nil
}
}
if fieldType.OmittedUnexported {
omittedUnexported = true
}
fields = append(fields, &ast.Field{
Names: []*ast.Ident{ast.NewIdent(field.Name)},
Type: fieldType.AST,
})
}
return Result{
AST: &ast.StructType{
Fields: &ast.FieldList{List: fields},
},
RequiresUnexported: requiresUnexported,
OmittedUnexported: omittedUnexported,
}, nil
case reflect.UnsafePointer:
// Note: For a plain unsafe.Pointer type, v.PkgPath() does not report "unsafe" but rather
// an empty string "".
isPlainUnsafePointer := v.String() == "unsafe.Pointer"
if !isPlainUnsafePointer && v.Name() != "" {
pkgPath := v.PkgPath()
if pkgPath != "" && pkgPath != opt.PackagePath {
pkgName, err := opt.packagePathToName(v.PkgPath())
if err != nil {
return Result{}, err
}
if pkgName != opt.PackageName {
return Result{
AST: &ast.SelectorExpr{X: ast.NewIdent(pkgName), Sel: ast.NewIdent(v.Name())},
RequiresUnexported: !ast.IsExported(v.Name()),
}, nil
}
}
return Result{
AST: ast.NewIdent(v.Name()),
RequiresUnexported: false,
}, nil
}
return Result{AST: &ast.SelectorExpr{X: ast.NewIdent("unsafe"), Sel: ast.NewIdent("Pointer")}}, nil
default:
return Result{AST: ast.NewIdent(v.Name())}, nil
}
}