forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
91 lines (75 loc) · 1.67 KB
/
file.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
package gencel
import (
"fmt"
"go/ast"
"log"
"regexp"
)
var blacklistedFuncs = []regexp.Regexp{
*regexp.MustCompile("^Create"),
*regexp.MustCompile("^init"),
}
type File struct {
pkg *Package
file *ast.File
// name of the file
name string
// path is the absolute path of this file.
path string
// decls is the list of all function declarations in this file.
decls []FuncDecl
}
// visitor visits all the ast nodes
// and extracts function declarations that're suitable
// for conversion.
func (t *File) visitor(n ast.Node) bool {
switch v := n.(type) {
case *ast.FuncDecl:
return t.handleFuncDecl(v)
default:
return true
}
}
func (t *File) handleFuncDecl(n *ast.FuncDecl) bool {
fmt.Printf("handle: %s", n.Name.Name)
for _, blf := range blacklistedFuncs {
if blf.MatchString(n.Name.Name) {
log.Printf("Ignoring func [%s]. Blacklisted pattern", n.Name.Name)
return false
}
}
if n.Type.Results == nil || len(n.Type.Results.List) == 0 {
log.Printf("Ignoring func [%s]. Returns nothing", n.Name.Name)
return false
}
decl := FuncDecl{
Name: n.Name.Name,
}
if n.Type.Params != nil {
for _, l := range n.Type.Params.List {
for range l.Names {
decl.Args = append(decl.Args, l.Type)
}
}
}
for _, l := range n.Type.Results.List {
decl.ReturnTypes = append(decl.ReturnTypes, l.Type)
}
if n.Recv != nil && n.Recv.List != nil {
for _, x := range n.Recv.List {
switch v := x.Type.(type) {
case *ast.Ident:
decl.RecvType = v.Name
case *ast.StarExpr:
switch y := v.X.(type) {
case *ast.Ident:
decl.RecvType = y.Name
}
}
}
}
if decl.RecvType != "" {
t.decls = append(t.decls, decl)
}
return true
}