-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackages.go
More file actions
164 lines (142 loc) · 3.26 KB
/
packages.go
File metadata and controls
164 lines (142 loc) · 3.26 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
package gogencli
import (
"fmt"
"go/ast"
"go/types"
"path/filepath"
"slices"
"golang.org/x/tools/go/packages"
)
func (g *Generator) FindPackageByPath(pkgPath string) *packages.Package {
for _, p := range g.packages {
if p.PkgPath == pkgPath {
return p
}
}
return nil
}
func (g *Generator) loadPackages() (err error) {
if g.addRootPattern && !slices.Contains(g.patterns, g.rootDir) {
g.patterns = append(g.patterns, g.rootDir)
}
patterns := slices.Concat(g.patterns, g.typePatterns)
if len(patterns) == 0 {
return fmt.Errorf("at least one pattern is required")
}
g.packages, err = packages.Load(g.packagesConfig, patterns...)
if err != nil {
return fmt.Errorf("error loading packages for inspection: %w", err)
}
if packages.PrintErrors(g.packages) > 0 {
return fmt.Errorf("packages contain errors")
}
if len(g.packages) == 0 {
return fmt.Errorf("no packages found")
}
err = g.loadRootPackage()
if err != nil {
return err
}
if g.rootRequired && (g.root == nil || g.root.ASTFile == nil || g.root.Object == nil) {
return fmt.Errorf("could not find root package and file")
}
return nil
}
func (g *Generator) loadRootPackage() (err error) {
for _, pkg := range g.packages {
if pkg.Dir == g.rootDir {
g.rootPackage = pkg
break
}
}
if g.rootPackage == nil {
return nil
}
if g.goFile == "" {
return nil
}
rootFile := filepath.Join(g.rootDir, g.goFile)
afile := g.astHelper.FindFileByFilename(g.rootPackage, rootFile)
if afile == nil {
return nil
}
g.root = &Root{
ASTFile: afile,
}
if g.rootPackage.Fset != nil {
g.root.TokenFile = g.rootPackage.Fset.File(g.root.ASTFile.Pos())
}
g.root.Decl = g.loadRootASTDecl()
g.root.Object = g.loadRootObject()
return nil
}
func (g *Generator) loadRootASTDecl() *ASTType {
for _, decl := range g.root.ASTFile.Decls {
switch d := decl.(type) {
case *ast.GenDecl:
if g.IsGoGenerateLine(d.Doc) {
ret := &ASTType{
Package: g.rootPackage,
File: g.root.ASTFile,
Decl: d,
}
if len(d.Specs) > 0 {
ret.Spec = d.Specs[0]
ret.Name = getSpecName(ret.Spec)
}
return ret
} else {
for _, spec := range d.Specs {
switch s := spec.(type) {
case *ast.ImportSpec:
if g.IsGoGenerateLine(s.Doc) {
return &ASTType{
Name: s.Name.Name,
Package: g.rootPackage,
File: g.root.ASTFile,
Decl: d,
Spec: s,
}
}
case *ast.TypeSpec:
if g.IsGoGenerateLine(s.Doc) {
return &ASTType{
Name: s.Name.Name,
Package: g.rootPackage,
File: g.root.ASTFile,
Decl: d,
Spec: s,
}
}
case *ast.ValueSpec:
if g.IsGoGenerateLine(s.Doc) {
return &ASTType{
Name: s.Names[0].Name,
Package: g.rootPackage,
File: g.root.ASTFile,
Decl: d,
Spec: s,
}
}
}
}
}
case *ast.FuncDecl:
if g.IsGoGenerateLine(d.Doc) {
return &ASTType{
Name: d.Name.Name,
Package: g.rootPackage,
File: g.root.ASTFile,
Decl: d,
}
}
}
}
return nil
}
func (g *Generator) loadRootObject() types.Object {
if g.root.Decl == nil {
return nil
}
return g.RootPackage().Types.Scope().Lookup(g.root.Decl.Name)
}