-
Notifications
You must be signed in to change notification settings - Fork 10
/
model.go
68 lines (54 loc) · 1.25 KB
/
model.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
package gii
import (
"bufio"
"fmt"
"os"
)
type Options struct {
name string
path string
column []column
packet map[string]string
namespace string
modelsNamespace string
}
func createModel(options Options) {
content := `package `
if options.namespace == ""{
content += "models"
}else {
content += options.namespace
}
content +=`
import (
"github.com/astaxie/beego/orm"`
for _,v := range options.packet {
content += "\n "+`"`+v+`"`
}
content += "\n" + `)`
content += "\n\n" +`type ` + options.name + ` struct {`
for _,vlues := range options.column {
content += "\n" + ` `+ vlues.name + ` ` + vlues.value
}
content += "\n}\n"
content += `
func init() {
// 需要在init中注册定义的model
orm.RegisterModel(new(` + options.name + `))
}`
//fmt.Printf("%s",content)
dir, _ := os.Getwd()
if options.path == "" {
options.path = "/models"
}
path := dir + options.path + `/` + options.name + ".go"
outputFile, outputError := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666)
if outputError != nil {
fmt.Printf("An error occurred with file opening or creation\n")
return
}
defer outputFile.Close()
outputWriter := bufio.NewWriter(outputFile)
outputWriter.WriteString(content)
outputWriter.Flush()
}