-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
233 lines (206 loc) · 5.24 KB
/
util.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
package caplit
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
"unicode"
)
type CapnpStructParams struct {
Name string
Template string
}
type CapnpStruct struct {
Name string
Path string
Keys []CapnpStructParams
Parent string
}
type CapnpFuncDecl struct {
FuncDecl *ast.FuncDecl
Path string
}
// return the name of param type from FunDecl
func paramType(fn *ast.FuncDecl) string {
if fn.Type.Params == nil || len(fn.Type.Params.List) == 0 {
return ""
}
switch e := fn.Type.Params.List[0].Type.(type) {
case *ast.Ident:
return e.Name
case *ast.StarExpr:
switch es := e.X.(type) {
case *ast.Ident:
return es.Name
case *ast.SelectorExpr:
return es.X.(*ast.Ident).Name + "." + es.Sel.String()
default:
panic(fmt.Sprintf("unknown param type %T", e.X))
}
case *ast.SelectorExpr:
switch sel := e.Sel; e.X.(type) {
case *ast.Ident:
// Name is not neccesary ( shared.MagicStat -> MagicStat )
return sel.Name
case *ast.SelectorExpr:
panic("do not enter this phase")
default:
panic(fmt.Sprintf("unknown param type %T", e.X))
}
case *ast.ArrayType:
switch es := e.Elt.(type) {
case *ast.Ident:
return "[]" + es.Name
default:
panic(fmt.Sprintf("unknown param type %T", es))
}
}
panic(fmt.Sprintf("unknown method receiver AST node type %T", fn.Type.Params.List[0].Type))
}
// return the name of type of receiver from FunDecl
func receiverType(fn *ast.FuncDecl) string {
if fn.Recv == nil || len(fn.Recv.List) == 0 {
return ""
}
switch e := fn.Recv.List[0].Type.(type) {
case *ast.Ident:
return e.Name
case *ast.StarExpr:
return e.X.(*ast.Ident).Name
}
fmt.Println("fn ", fn)
panic(fmt.Sprintf("unknown method receiver AST node type %T", fn.Recv.List[0].Type))
}
// return the return type from FunDecl
func returnType(fn *ast.FuncDecl) string {
if fn.Type.Results == nil || len(fn.Type.Results.List) == 0 {
return ""
}
switch e := fn.Type.Results.List[0].Type.(type) {
case *ast.Ident:
return e.Name
case *ast.StarExpr:
return e.X.(*ast.Ident).Name
case *ast.ArrayType:
switch es := e.Elt.(type) {
case *ast.Ident:
return "[]" + es.Name
default:
panic(fmt.Sprintf("unknown method receiver AST node type %T", es))
}
case *ast.SelectorExpr:
switch sel := e.Sel; e.X.(type) {
case *ast.Ident:
return sel.Name
default:
panic(fmt.Sprintf("unknown method receiver AST node type %T", e.X))
}
}
panic(fmt.Sprintf("unknown method receiver AST node type %T", fn.Type.Results.List[0].Type))
}
func check(err error) {
if err != nil {
panic(err)
}
}
type pathSourcePair struct {
path string
file *ast.File
}
type parsedSources []pathSourcePair
func parseCapnpSources(filePattern string) parsedSources {
parsedFiles := make(parsedSources, 0)
fset := token.NewFileSet()
matches, err := filepath.Glob(filePattern)
check(err)
for _, path := range matches {
if strings.Contains(path, "generated") {
continue
}
parsed, err := parser.ParseFile(fset, path, nil, 0)
check(err)
parsedFiles = append(parsedFiles, pathSourcePair{path: path, file: parsed})
}
return parsedFiles
}
func (files parsedSources) FilterFuncDecl(filter func(*ast.FuncDecl) bool) []CapnpFuncDecl {
funcDecls := make([]CapnpFuncDecl, 0)
for _, pair := range files {
for _, d := range pair.file.Decls {
switch t := d.(type) {
case *ast.FuncDecl:
if filter(t) {
funcDecls = append(funcDecls, CapnpFuncDecl{t, strings.TrimSuffix(pair.path, ".go")})
}
}
}
}
return funcDecls
}
func CapnpStructs(capnpDir string) []CapnpStruct {
funcDecls := parseCapnpSources(capnpDir + "/*.capnp.go").FilterFuncDecl(func(t *ast.FuncDecl) bool { return strings.HasPrefix(t.Name.Name, "NewRoot") })
capnpStructs := make([]CapnpStruct, 0)
for _, funcDecl := range funcDecls {
capnpStruct := CapnpStruct{
Name: returnType(funcDecl.FuncDecl),
Path: funcDecl.Path,
}
capnpStructs = append(capnpStructs, capnpStruct)
}
return capnpStructs
}
func firstToLower(s string) string {
a := []rune(s)
a[0] = unicode.ToLower(a[0])
return string(a)
}
func elemInList(elem string, l []string) bool {
for _, s := range l {
if s == elem {
return true
}
}
return false
}
func generate(tmplStr, filepath string, params map[string]interface{}) {
tmpl, err := template.New("capnp").Parse(tmplStr)
check(err)
// generate
{
file, err := os.Create(filepath)
check(err)
defer file.Close()
err = tmpl.Execute(file, params)
check(err)
}
// do formatting
cmd := exec.Command("gofmt", "-w", filepath)
err = cmd.Run()
check(err)
}
func CapnpEnums(capnpDir string) []CapnpStruct {
funcDecls := parseCapnpSources(capnpDir + "/*.capnp.go").FilterFuncDecl(func(t *ast.FuncDecl) bool { return strings.HasSuffix(t.Name.Name, "FromString") })
capnpStructs := make([]CapnpStruct, 0)
for _, funcDecl := range funcDecls {
capnpStruct := CapnpStruct{
Name: returnType(funcDecl.FuncDecl),
Path: funcDecl.Path,
}
capnpStructs = append(capnpStructs, capnpStruct)
}
return capnpStructs
}
// return list of name of Enums in targetDir
func GetEnumList(targetDir string) []string {
enums := CapnpEnums(targetDir)
ret_val := make([]string, len(enums))
for i, enumStruct := range enums {
ret_val[i] = enumStruct.Name
}
return ret_val
}