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
|
package parseutil
import (
"fmt"
aster "github.com/szyhf/go-aster"
"html/template"
_ "io/ioutil"
"os"
"strings"
)
func ParseWith(path string,inputTableName string ,inputStructName string ) {
pkgs, err := aster.ParseDir(path, nil)
if err != nil {
panic(err)
}
for _, pkg := range pkgs {
var pkgName = pkg.Name
fmt.Println("pkg name =", pkgName)
models := pkg.Structs
for _, model := range models {
fmt.Println(model.Docs)
if inputStructName == model.Name {
generateRepo(model,pkg,path,inputTableName)
}
//generateRepo(model,pkg,path)
}
}
}
func generateRepo(model *aster.StructType,pkg *aster.PackageType,path string ,inputTableName string ) {
var (
modelName string = model.Name
tableName string = inputTableName
fseq string
scanFields string
packageName string
fields string
question string
)
packageName = pkg.Name
var dbFields []string
var structFields []string
for _,f := range model.Fields {
dbname := f.Tag.Get("db")
if dbname == "" {
dbname = f.Name
}
dbFields = append(dbFields,dbname)
structFields = append(structFields,f.Name)
}
fields = strings.Join(dbFields,",")
{
var arr []string
for _,f := range dbFields {
arr = append(arr,fmt.Sprintf("%s = ?",f))
}
fseq = strings.Join(arr,",")
for i,v := range model.Fields {
name := v.Name
arr[i] = "&u."+name
}
scanFields = strings.Join(arr,",")
//填充??
for i,_ := range model.Fields {
arr[i] = "?"
}
question = strings.Join(arr,",")
}
//var fileName = fmt.Sprintf("%s/%s_impl.go",path,modelName)
tpl, err := template.ParseFiles("./template/dao.tpl")
if err != nil {
panic(err)
}
//f, err := os.OpenFile(fileName, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
panic(err)
}
tpl.Execute(os.Stdout,map[string]interface{}{
"modelName":modelName,
"tableName":tableName,
"fseq":fseq,
"scanFields":template.HTML(scanFields),
"packageName":packageName,
"fields":fields,
"question":question,
})
//model.Methods[0].
// id
//ioutil.WriteFile(fileName,[]byte("afsa"),0640)
}
|