forked from mattatcha/scaneo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scaneo.go
410 lines (333 loc) · 9.1 KB
/
scaneo.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
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
package main
import (
"errors"
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"path/filepath"
"strings"
"text/template"
)
const (
usageText = `SCANEO
Generate Go code to convert database rows into arbitrary structs.
USAGE
scaneo [options] paths...
OPTIONS
-o, -output
Set the name of the generated file. Default is scans.go.
-p, -package
Set the package name for the generated file. Default is current
directory name.
-u, -unexport
Generate unexported functions. Default is export all.
-w, -whitelist
Only include structs specified in case-sensitive, comma-delimited
string.
-f, -funcs
Generate SQL helper functions.
-i, -import
Override package to import type from.
-v, -version
Print version and exit.
-h, -help
Print help and exit.
EXAMPLES
tables.go is a file that contains one or more struct declarations.
Generate scan functions based on structs in tables.go.
scaneo tables.go
Generate scan functions and name the output file funcs.go
scaneo -o funcs.go tables.go
Generate scans.go with unexported functions.
scaneo -u tables.go
Generate scans.go with only struct Post and struct user.
scaneo -w "Post,user" tables.go
NOTES
Struct field names don't have to match database column names at all.
However, the order of the types must match.
Integrate this with go generate by adding this line to the top of your
tables.go file.
//go:generate scaneo $GOFILE
`
)
type fieldToken struct {
Name string
Type string
}
type structToken struct {
Name string
Fields []fieldToken
}
func main() {
log.SetFlags(0)
outFilename := flag.String("o", "scans.go", "")
packName := flag.String("p", "current directory", "")
unexport := flag.Bool("u", false, "")
whitelist := flag.String("w", "", "")
genFuncs := flag.Bool("f", false, "")
pkgImport := flag.String("i", "", "")
version := flag.Bool("v", false, "")
help := flag.Bool("h", false, "")
flag.StringVar(outFilename, "output", "scans.go", "")
flag.StringVar(packName, "package", "current directory", "")
flag.BoolVar(unexport, "unexport", false, "")
flag.StringVar(whitelist, "whitelist", "", "")
flag.BoolVar(genFuncs, "funcs", false, "")
flag.StringVar(pkgImport, "import", "", "")
flag.BoolVar(version, "version", false, "")
flag.BoolVar(help, "help", false, "")
flag.Usage = func() { log.Println(usageText) } // call on flag error
flag.Parse()
if *help {
// not an error, send to stdout
// that way people can: scaneo -h | less
fmt.Println(usageText)
return
}
if *version {
fmt.Println("scaneo version 1.2.0")
return
}
if *packName == "current directory" {
wd, err := os.Getwd()
if err != nil {
log.Fatal("couldn't get working directory:", err)
}
*packName = filepath.Base(wd)
}
files, err := findFiles(flag.Args())
if err != nil {
log.Println("couldn't find files:", err)
log.Fatal(usageText)
}
structToks := make([]structToken, 0, 8)
for _, file := range files {
toks, err := parseCode(file, *whitelist)
if err != nil {
log.Println(`"syntax error" - parser probably`)
log.Fatal(err)
}
structToks = append(structToks, toks...)
}
if err := genFile(*outFilename, *packName, *unexport, structToks, *genFuncs, *pkgImport); err != nil {
log.Fatal("couldn't generate file:", err)
}
}
func findFiles(paths []string) ([]string, error) {
if len(paths) < 1 {
return nil, errors.New("no starting paths")
}
// using map to prevent duplicate file path entries
// in case the user accidently passes the same file path more than once
// probably because of autocomplete
files := make(map[string]struct{})
for _, path := range paths {
info, err := os.Stat(path)
if err != nil {
return nil, err
}
if !info.IsDir() {
// add file path to files
files[path] = struct{}{}
continue
}
filepath.Walk(path, func(fp string, fi os.FileInfo, _ error) error {
if fi.IsDir() {
// will still enter directory
return nil
} else if fi.Name()[0] == '.' {
return nil
}
// add file path to files
files[fp] = struct{}{}
return nil
})
}
deduped := make([]string, 0, len(files))
for f := range files {
deduped = append(deduped, f)
}
return deduped, nil
}
func parseCode(source string, commaList string) ([]structToken, error) {
wlist := make(map[string]struct{})
if commaList != "" {
wSplits := strings.Split(commaList, ",")
for _, s := range wSplits {
wlist[s] = struct{}{}
}
}
structToks := make([]structToken, 0, 8)
fset := token.NewFileSet()
astf, err := parser.ParseFile(fset, source, nil, 0)
if err != nil {
return nil, err
}
var filter bool
if len(wlist) > 0 {
filter = true
}
//ast.Print(fset, astf)
for _, decl := range astf.Decls {
genDecl, isGeneralDeclaration := decl.(*ast.GenDecl)
if !isGeneralDeclaration {
continue
}
for _, spec := range genDecl.Specs {
typeSpec, isTypeDeclaration := spec.(*ast.TypeSpec)
if !isTypeDeclaration {
continue
}
structType, isStructTypeDeclaration := typeSpec.Type.(*ast.StructType)
if !isStructTypeDeclaration {
continue
}
// found a struct in the source code!
var structTok structToken
// filter logic
if structName := typeSpec.Name.Name; !filter {
// no filter, collect everything
structTok.Name = structName
} else if _, exists := wlist[structName]; filter && !exists {
// if structName not in whitelist, continue
continue
} else if filter && exists {
// structName exists in whitelist
structTok.Name = structName
}
structTok.Fields = make([]fieldToken, 0, len(structType.Fields.List))
// iterate through struct fields (1 line at a time)
for _, fieldLine := range structType.Fields.List {
fieldToks := make([]fieldToken, len(fieldLine.Names))
// get field name (or names because multiple vars can be declared in 1 line)
for i, fieldName := range fieldLine.Names {
fieldToks[i].Name = parseIdent(fieldName)
}
var fieldType string
// get field type
switch typeToken := fieldLine.Type.(type) {
case *ast.Ident:
// simple types, e.g. bool, int
fieldType = parseIdent(typeToken)
case *ast.SelectorExpr:
// struct fields, e.g. time.Time, sql.NullString
fieldType = parseSelector(typeToken)
case *ast.ArrayType:
// arrays
fieldType = parseArray(typeToken)
case *ast.StarExpr:
// pointers
fieldType = parseStar(typeToken)
}
if fieldType == "" {
continue
}
// apply type to all variables declared in this line
for i := range fieldToks {
fieldToks[i].Type = fieldType
}
structTok.Fields = append(structTok.Fields, fieldToks...)
}
structToks = append(structToks, structTok)
}
}
return structToks, nil
}
func parseIdent(fieldType *ast.Ident) string {
// return like byte, string, int
return fieldType.Name
}
func parseSelector(fieldType *ast.SelectorExpr) string {
// return like time.Time, sql.NullString
ident, isIdent := fieldType.X.(*ast.Ident)
if !isIdent {
return ""
}
return fmt.Sprintf("%s.%s", parseIdent(ident), fieldType.Sel.Name)
}
func parseArray(fieldType *ast.ArrayType) string {
// return like []byte, []time.Time, []*byte, []*sql.NullString
var arrayType string
switch typeToken := fieldType.Elt.(type) {
case *ast.Ident:
arrayType = parseIdent(typeToken)
case *ast.SelectorExpr:
arrayType = parseSelector(typeToken)
case *ast.StarExpr:
arrayType = parseStar(typeToken)
}
if arrayType == "" {
return ""
}
return fmt.Sprintf("[]%s", arrayType)
}
func parseStar(fieldType *ast.StarExpr) string {
// return like *bool, *time.Time, *[]byte, and other array stuff
var starType string
switch typeToken := fieldType.X.(type) {
case *ast.Ident:
starType = parseIdent(typeToken)
case *ast.SelectorExpr:
starType = parseSelector(typeToken)
case *ast.ArrayType:
starType = parseArray(typeToken)
}
if starType == "" {
return ""
}
return fmt.Sprintf("*%s", starType)
}
func genFile(outFile, pkg string, unexport bool, toks []structToken, genFuncs bool, pkgImport string) error {
if len(toks) < 1 {
return errors.New("no structs found")
}
fout, err := os.Create(outFile)
if err != nil {
return err
}
defer fout.Close()
data := struct {
PackageName string
Tokens []structToken
Visibility string
Funcs bool
ImportPkg string
}{
PackageName: pkg,
Visibility: "S",
Tokens: toks,
Funcs: genFuncs,
ImportPkg: pkgImport,
}
if unexport {
// func name will be scanFoo instead of ScanFoo
data.Visibility = "s"
}
// Construct type prefix from pkgImport
var typePrefix string
if pkgImport != "" {
pkgPathParts := strings.Split(pkgImport, "/")
typePrefix = pkgPathParts[len(pkgPathParts)-1]
}
fnMap := template.FuncMap{
"title": strings.Title,
"pkg": func(s string) string {
if typePrefix != "" {
return fmt.Sprintf("%s.%s", typePrefix, s)
}
return s
},
}
scansTmpl, err := template.New("scans").Funcs(fnMap).Parse(scansText)
if err != nil {
return err
}
if err := scansTmpl.Execute(fout, data); err != nil {
return err
}
return nil
}