-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgo_type_resolver.go
More file actions
421 lines (378 loc) · 9.72 KB
/
go_type_resolver.go
File metadata and controls
421 lines (378 loc) · 9.72 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package main
import (
"go/token"
"go/types"
"os"
"path/filepath"
"strings"
"sync"
"golang.org/x/tools/go/packages"
)
// loadedPackage holds a type-checked package along with its fileset for position lookup.
type loadedPackage struct {
types *types.Package
fset *token.FileSet
}
// GoTypeResolver implements TypeResolver using go/types.
// It loads Go package type information and caches it.
type GoTypeResolver struct {
mu sync.Mutex
cache map[string]*loadedPackage // import path → loaded package
dir string // project directory (for go.mod resolution)
}
func NewGoTypeResolver(dir string) *GoTypeResolver {
return &GoTypeResolver{
cache: make(map[string]*loadedPackage),
dir: dir,
}
}
func (r *GoTypeResolver) loadPackage(pkg string) *types.Package {
lp := r.loadPackageFull(pkg)
if lp == nil {
return nil
}
return lp.types
}
func (r *GoTypeResolver) loadPackageFull(pkg string) *loadedPackage {
r.mu.Lock()
defer r.mu.Unlock()
if cached, ok := r.cache[pkg]; ok {
return cached
}
// Arca built-in packages: load from embed.FS via go/parser + go/types
for _, ap := range arcaPackages {
if pkg == ap.GoModPath {
tp, fset := loadArcaPackageTypes(&ap)
lp := &loadedPackage{types: tp, fset: fset}
r.cache[pkg] = lp
return lp
}
}
cfg := &packages.Config{
Mode: packages.NeedTypes | packages.NeedName | packages.NeedSyntax | packages.NeedTypesInfo,
Dir: r.dir,
}
pkgs, err := packages.Load(cfg, pkg)
if err != nil || len(pkgs) == 0 || pkgs[0].Types == nil || len(pkgs[0].Errors) > 0 {
r.cache[pkg] = nil
return nil
}
lp := &loadedPackage{types: pkgs[0].Types, fset: pkgs[0].Fset}
r.cache[pkg] = lp
return lp
}
// MemberPos returns the file and position where a package-level member is defined.
// Returns ("", Pos{}) if not found.
func (r *GoTypeResolver) MemberPos(pkg, name string) (string, Pos) {
lp := r.loadPackageFull(pkg)
if lp == nil || lp.types == nil || lp.fset == nil {
return "", Pos{}
}
obj := lp.types.Scope().Lookup(name)
if obj == nil {
return "", Pos{}
}
position := lp.fset.Position(obj.Pos())
if !position.IsValid() {
return "", Pos{}
}
return position.Filename, Pos{Line: position.Line, Col: position.Column}
}
// PackageMembers lists exported package-level members of a package.
func (r *GoTypeResolver) PackageMembers(pkg string) []MemberInfo {
goPkg := r.loadPackage(pkg)
if goPkg == nil {
return nil
}
var members []MemberInfo
scope := goPkg.Scope()
for _, name := range scope.Names() {
obj := scope.Lookup(name)
if !obj.Exported() {
continue
}
var kind, detail string
switch o := obj.(type) {
case *types.Func:
kind = "func"
detail = o.Type().String()
case *types.TypeName:
kind = "type"
detail = obj.Type().String()
case *types.Var:
kind = "var"
detail = o.Type().String()
case *types.Const:
kind = "const"
detail = o.Type().String()
default:
continue
}
members = append(members, MemberInfo{Name: name, Kind: kind, Detail: detail})
}
return members
}
// TypeMembers lists exported methods and fields of a named type in a package.
func (r *GoTypeResolver) TypeMembers(pkg, typeName string) []MemberInfo {
goPkg := r.loadPackage(pkg)
if goPkg == nil {
return nil
}
obj := goPkg.Scope().Lookup(typeName)
if obj == nil {
return nil
}
named, ok := obj.Type().(*types.Named)
if !ok {
return nil
}
var members []MemberInfo
// Methods
for i := 0; i < named.NumMethods(); i++ {
m := named.Method(i)
if !m.Exported() {
continue
}
members = append(members, MemberInfo{
Name: m.Name(),
Kind: "method",
Detail: m.Type().String(),
})
}
// Fields (for struct types)
if st, ok := named.Underlying().(*types.Struct); ok {
for i := 0; i < st.NumFields(); i++ {
f := st.Field(i)
if !f.Exported() {
continue
}
members = append(members, MemberInfo{
Name: f.Name(),
Kind: "field",
Detail: f.Type().String(),
})
}
}
return members
}
// MethodPos returns the file and position of a method on a type in a package.
func (r *GoTypeResolver) MethodPos(pkg, typeName, method string) (string, Pos) {
lp := r.loadPackageFull(pkg)
if lp == nil || lp.types == nil || lp.fset == nil {
return "", Pos{}
}
obj := lp.types.Scope().Lookup(typeName)
if obj == nil {
return "", Pos{}
}
named, ok := obj.Type().(*types.Named)
if !ok {
return "", Pos{}
}
for i := 0; i < named.NumMethods(); i++ {
m := named.Method(i)
if m.Name() == method {
position := lp.fset.Position(m.Pos())
if position.IsValid() {
return position.Filename, Pos{Line: position.Line, Col: position.Column}
}
}
}
return "", Pos{}
}
func (r *GoTypeResolver) ResolveFunc(pkg, name string) *FuncInfo {
goPkg := r.loadPackage(pkg)
if goPkg == nil {
return nil
}
obj := goPkg.Scope().Lookup(name)
if obj == nil {
return nil
}
fn, ok := obj.(*types.Func)
if !ok {
return nil
}
sig, ok := fn.Type().(*types.Signature)
if !ok {
return nil
}
return sigToFuncInfo(sig)
}
func (r *GoTypeResolver) ResolveType(pkg, name string) *TypeInfo {
goPkg := r.loadPackage(pkg)
if goPkg == nil {
return nil
}
obj := goPkg.Scope().Lookup(name)
if obj == nil {
return nil
}
named, ok := obj.Type().(*types.Named)
if !ok {
return nil
}
info := &TypeInfo{}
switch named.Underlying().(type) {
case *types.Struct:
info.Kind = TypeInfoStruct
case *types.Interface:
info.Kind = TypeInfoInterface
case *types.Basic:
info.Kind = TypeInfoBasic
default:
info.Kind = TypeInfoOther
}
// Collect methods
mset := types.NewMethodSet(named)
for i := 0; i < mset.Len(); i++ {
info.Methods = append(info.Methods, mset.At(i).Obj().Name())
}
// Collect struct fields
if strct, ok := named.Underlying().(*types.Struct); ok {
for i := 0; i < strct.NumFields(); i++ {
f := strct.Field(i)
info.Fields = append(info.Fields, FieldInfo{
Name: f.Name(),
Type: f.Type().String(),
})
}
}
return info
}
func (r *GoTypeResolver) ResolveMethod(pkg, typ, method string) *FuncInfo {
goPkg := r.loadPackage(pkg)
if goPkg == nil {
return nil
}
obj := goPkg.Scope().Lookup(typ)
if obj == nil {
return nil
}
// Try both value and pointer receiver
for _, t := range []types.Type{obj.Type(), types.NewPointer(obj.Type())} {
mset := types.NewMethodSet(t)
for i := 0; i < mset.Len(); i++ {
sel := mset.At(i)
if sel.Obj().Name() == method {
if fn, ok := sel.Obj().(*types.Func); ok {
if sig, ok := fn.Type().(*types.Signature); ok {
return sigToFuncInfo(sig)
}
}
}
}
}
return nil
}
func (r *GoTypeResolver) ResolveUnderlying(goType string) string {
// Split "github.com/labstack/echo/v5.HandlerFunc" → pkg + name
dotIdx := strings.LastIndex(goType, ".")
if dotIdx < 0 {
return ""
}
pkgPath := goType[:dotIdx]
typeName := goType[dotIdx+1:]
goPkg := r.loadPackage(pkgPath)
if goPkg == nil {
return ""
}
obj := goPkg.Scope().Lookup(typeName)
if obj == nil {
return ""
}
underlying := obj.Type().Underlying()
if underlying == nil {
return ""
}
return underlying.String()
}
func (r *GoTypeResolver) CanLoadPackage(pkg string) bool {
if isStdLib(pkg) {
return r.loadPackage(pkg) != nil
}
// Arca built-in packages: bundled with arca binary
for _, ap := range arcaPackages {
if pkg == ap.GoModPath {
return true
}
}
// Same-module subpackage: always available
if r.isSameModule(pkg) {
return true
}
// Non-stdlib: check go.mod require entries, not just module cache
return r.isInGoMod(pkg)
}
// isSameModule checks if a package is a subpackage of the current module.
func (r *GoTypeResolver) isSameModule(pkg string) bool {
if r.dir == "" {
return false
}
moduleName := readGoModuleName(r.dir)
return moduleName != "" && strings.HasPrefix(pkg, moduleName+"/")
}
// isInGoMod checks if a package path is required in the project's go.mod.
func (r *GoTypeResolver) isInGoMod(pkg string) bool {
if r.dir == "" {
return false
}
data, err := os.ReadFile(filepath.Join(r.dir, "go.mod"))
if err != nil {
return false
}
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
// Match "require" lines: both single-line and block form
// Single: require github.com/foo/bar v1.0.0
// Block: \tgithub.com/foo/bar v1.0.0
if strings.HasPrefix(line, "require ") {
parts := strings.Fields(line)
if len(parts) >= 2 && isModuleMatch(pkg, parts[1]) {
return true
}
}
// Inside require block: lines start with module path
if strings.Contains(line, "/") && !strings.HasPrefix(line, "//") && !strings.HasPrefix(line, "module ") && !strings.HasPrefix(line, "go ") && !strings.HasPrefix(line, "require") && !strings.HasPrefix(line, ")") {
parts := strings.Fields(line)
if len(parts) >= 2 && isModuleMatch(pkg, parts[0]) {
return true
}
}
}
return false
}
// isModuleMatch checks if a package import path belongs to a module.
// e.g. "github.com/labstack/echo/v5" matches module "github.com/labstack/echo/v5"
// and "github.com/labstack/echo/v5/middleware" also matches.
func isModuleMatch(pkg, module string) bool {
return pkg == module || strings.HasPrefix(pkg, module+"/")
}
func sigToFuncInfo(sig *types.Signature) *FuncInfo {
info := &FuncInfo{
Variadic: sig.Variadic(),
}
// Extract type parameters for generic functions
if tparams := sig.TypeParams(); tparams != nil {
for i := 0; i < tparams.Len(); i++ {
info.TypeParams = append(info.TypeParams, tparams.At(i).Obj().Name())
}
}
params := sig.Params()
for i := 0; i < params.Len(); i++ {
p := params.At(i)
info.Params = append(info.Params, ParamInfo{
Name: p.Name(),
Type: p.Type().String(),
})
}
results := sig.Results()
for i := 0; i < results.Len(); i++ {
r := results.At(i)
info.Results = append(info.Results, ParamInfo{
Name: r.Name(),
Type: r.Type().String(),
})
}
return info
}