-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
167 lines (131 loc) · 3.19 KB
/
main.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
package main
import (
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/printer"
"go/token"
"os"
"path/filepath"
"golang.org/x/tools/go/ast/astutil"
)
// TODO: generate tests using each modifier name and generate tests?
const panicFuncName = "panicIfReadOnly"
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go <directory>")
os.Exit(1)
}
dir := os.Args[1]
// read the files in the specified directory
files, err := filepath.Glob(filepath.Join(dir, "*.go"))
if err != nil {
fmt.Println("Error reading directory:", err)
os.Exit(1)
}
// parse the expression to easily insert
generatedExpr, err := parser.ParseExpr(fmt.Sprintf("a.%s()\n", panicFuncName))
if err != nil {
fmt.Println("Error reading directory:", err)
os.Exit(1)
}
// parse each Go source file
for _, file := range files {
err = addReadOnlyPanicToFile(file, generatedExpr)
if err != nil {
fmt.Println("error parsing/editing file:", err)
}
}
}
func addReadOnlyPanicToFile(filename string, generatedExpr ast.Expr) error {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
if err != nil {
return fmt.Errorf("error parsing file: %w", err)
}
astutil.Apply(node, nil, func(c *astutil.Cursor) bool {
funcDecl, ok := c.Node().(*ast.FuncDecl)
if !ok {
return true
}
recvType := getFuncReceiverType(funcDecl)
returnType := getFuncReturnType(funcDecl)
// exit if there is no receiver or return
if recvType == nil || returnType == nil {
return true
}
if !(isAPIType(recvType) && isAPIType(returnType)) {
return true
}
if alreadyGenerated(funcDecl.Body.List) {
return true
}
funcDecl.Body.List = append([]ast.Stmt{&ast.ExprStmt{
X: generatedExpr,
}}, funcDecl.Body.List...)
c.Replace(funcDecl)
return true
})
outputFile, err := os.OpenFile(filename, os.O_RDWR, 0644)
if err != nil {
return fmt.Errorf("error opening output file: %w", err)
}
defer outputFile.Close()
err = format.Node(outputFile, fset, &printer.CommentedNode{Node: node})
if err != nil {
return fmt.Errorf("error writing file: %w", err)
}
return nil
}
func getFuncReturnType(f *ast.FuncDecl) *ast.StarExpr {
if f.Type == nil || f.Type.Results == nil {
return nil
}
returnType, _ := f.Type.Results.List[0].Type.(*ast.StarExpr)
return returnType
}
func getFuncReceiverType(f *ast.FuncDecl) *ast.StarExpr {
if f.Recv == nil || len(f.Recv.List) != 1 {
return nil
}
recvType, _ := f.Recv.List[0].Type.(*ast.StarExpr)
return recvType
}
func alreadyGenerated(bodyList []ast.Stmt) bool {
for _, item := range bodyList {
exprStmt, ok := item.(*ast.ExprStmt)
if !ok {
continue
}
callExpr, ok := exprStmt.X.(*ast.CallExpr)
if !ok {
continue
}
sel, ok := callExpr.Fun.(*ast.SelectorExpr)
if !ok {
continue
}
if sel.Sel.Name == panicFuncName {
return true
}
}
return false
}
func isAPIType(input *ast.StarExpr) bool {
idx, ok := input.X.(*ast.IndexExpr)
if !ok {
return false
}
ident, ok := idx.X.(*ast.Ident)
if !ok {
return false
}
genIdent, ok := idx.Index.(*ast.Ident)
if !ok {
return false
}
recvType := ident.Name
genType := genIdent.Name
return recvType == "API" && genType == "T"
}