-
Notifications
You must be signed in to change notification settings - Fork 0
/
genMeta.go
54 lines (41 loc) · 920 Bytes
/
genMeta.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
package gen
import (
"html/template"
"os"
"path"
"github.com/macinnir/dvc/core/lib"
"github.com/macinnir/dvc/core/lib/schema"
)
// GenMeta returns a string for a model in golang
func (g *Gen) GenMeta(dir string, database *schema.Schema) (e error) {
lib.EnsureDir(dir)
p := path.Join(dir, "models.go")
// lib.Infof("Generating `%s`", g.Options, p)
tpl := `// Generated Code; DO NOT EDIT.
package meta
// Tables returns metaData about Tables
func Tables() map[string]map[string][]string {
return map[string]map[string][]string {
{{range .Tables}}
"{{.Name}}": {
{{range .Columns}}
"{{.Name}}": {"{{.FmtType}}", "{{.DataType}}", "{{.GoType}}"},
{{end}}
},
{{end}}
}
}
`
t := template.Must(template.New("meta-tables").Parse(tpl))
f, e := os.Create(p)
if e != nil {
panic(e)
}
e = t.Execute(f, database)
if e != nil {
panic(e)
}
f.Close()
lib.FmtGoCode(p)
return
}