-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.go
192 lines (177 loc) · 3.9 KB
/
loader.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
package internal
import (
"errors"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
)
// Loader ...
type Loader struct {
StructList func(*ast.File) []*Struct
FuncList func(*ast.File) []*Func
}
// NewLoader
func NewLoader() *Loader {
return &Loader{}
}
func (l Loader) generateCodeFromDB(args *ArgType) error {
params := []string{args.DSN}
if args.Out != "" {
params = append(params, "-o", args.XoPath)
}
if args.TemplatePath != "" {
params = append(params, "--template-path", args.TemplatePath)
}
output, err := exec.Command("xo", params...).CombinedOutput()
if err != nil {
return errors.New(string(output))
}
return nil
}
// loadStructList ...
func (l Loader) loadStructList(f *ast.File) []*Struct {
ss := []*Struct{}
ast.Inspect(f, func(n ast.Node) bool {
gd, ok := n.(*ast.GenDecl)
if !ok {
return true
}
for _, spec := range gd.Specs {
if ts, ok := spec.(*ast.TypeSpec); ok {
if _, ok := ts.Type.(*ast.StructType); ok {
s := &Struct{
Name: ts.Name.Name,
Package: f.Name.Name,
}
ss = append(ss, s)
}
}
}
return true
})
return ss
}
// loadFuncList ...
func (l Loader) loadFuncList(f *ast.File) []*Func {
funcList := []*ast.FuncDecl{}
ast.Inspect(f, func(n ast.Node) bool {
if fd, ok := n.(*ast.FuncDecl); ok && fd.Recv == nil {
funcList = append(funcList, fd)
}
return true
})
fns := []*Func{}
for _, fd := range funcList {
// get param fields
params := []*Field{}
for _, p := range fd.Type.Params.List {
for _, n := range p.Names {
param := &Field{}
param.Name = n.Name
// field type
switch p.Type.(type) {
case *ast.Ident:
param.Type = p.Type.(*ast.Ident).Name
// FIXME: for xo
if param.Type == "XODB" {
param.Type = fmt.Sprintf("%s.%s", f.Name.Name, param.Type)
}
case *ast.SelectorExpr:
x := p.Type.(*ast.SelectorExpr).X.(*ast.Ident).Name
sel := p.Type.(*ast.SelectorExpr).Sel.Name
param.Type = fmt.Sprintf("%s.%s", x, sel)
}
params = append(params, param)
}
}
// get return field
var res *Field
for _, p := range fd.Type.Results.List {
r := &Field{}
// field type
switch p.Type.(type) {
case *ast.StarExpr:
r.Type = p.Type.(*ast.StarExpr).X.(*ast.Ident).Name
r.IsPtr = true
r.NilType = "nil"
case *ast.ArrayType:
elt := p.Type.(*ast.ArrayType).Elt
switch elt.(type) {
case *ast.StarExpr:
r.Type = elt.(*ast.StarExpr).X.(*ast.Ident).Name
r.IsPtr = true
}
r.IsArray = true
r.NilType = "nil"
}
if r.Type != "" {
res = r
}
}
fn := &Func{
Name: fd.Name.Name,
Package: f.Name.Name,
Params: params,
Return: res,
}
fns = append(fns, fn)
}
return fns
}
// LoadFile ...
func (l Loader) LoadCodes(args *ArgType) error {
if err := l.generateCodeFromDB(args); err != nil {
return err
}
fis, err := ioutil.ReadDir(args.XoPath)
if err != nil {
return err
}
fset := token.NewFileSet()
for _, fi := range fis {
if fi.IsDir() {
continue
}
// delete table files in black list
shouldDeleted := false
for _, b := range args.BlackList {
if strings.Index(fi.Name(), b) != -1 {
shouldDeleted = true
break
}
}
if shouldDeleted {
if err := os.Remove(filepath.Join(args.XoPath, fi.Name())); err != nil {
return err
}
continue
}
// parse file
name := strings.Split(fi.Name(), ".")[0]
f, err := parser.ParseFile(fset, filepath.Join(args.XoPath, fi.Name()), nil, 0)
if err != nil {
return err
}
// load struct
ss := l.loadStructList(f)
for _, s := range ss {
if err := args.ExecuteTemplate(StructTemplate, FileEditableType, name, s); err != nil {
return err
}
}
// load func
fns := l.loadFuncList(f)
for _, fn := range fns {
if err := args.ExecuteTemplate(FuncTemplate, FileNotEditableType, name, fn); err != nil {
return err
}
}
}
return nil
}