/
tpls.go
94 lines (84 loc) · 2.23 KB
/
tpls.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
package main
import (
"sync"
"text/template"
)
type onceTemplate struct {
tpl *template.Template
once sync.Once
}
type Template struct {
onceGen *onceTemplate
tpls []*template.Template
}
var (
basicTemplates = &Template{
tpls: []*template.Template{
structNameTpl,
structFieldsTpl,
structFieldsArrayTpl,
structFieldsMapTpl,
},
}
genjiTemplates = &Template{
onceGen: &onceTemplate{
tpl: genjiMainTpl,
},
tpls: []*template.Template{
genjiCastTpl,
},
}
)
var (
structNameTpl = template.Must(template.New("nameTpl").Parse(
`const {{.Name}}Name = "{{.CustomName}}"
`))
structFieldsTpl = template.Must(template.New("structFieldsTpl").Parse(
`var {{.Name}}Fields = struct {
{{range $field := .Fields -}}
{{$field.Name}} string
{{end}}
}{
{{range $field := .Fields -}}
{{$field.Name}}: "{{ .Value }}",
{{end}}
}
`))
structFieldsArrayTpl = template.Must(template.New("structFieldsArrayTpl").Parse(
`var {{.Name}}FieldsArray = []string{
{{range $field := .Fields -}}
{{$.Name}}Fields.{{$field.Name}},
{{end}}
}
`))
structFieldsMapTpl = template.Must(template.New("structFieldsMapTpl").Parse(
`var {{.Name}}FieldsMap = map[string]struct{}{
{{range $field := .Fields -}}
"{{ .Value }}": {},
{{end}}
}
`))
)
var (
genjiMainTpl = template.Must(template.New("genjiMainTpl").Parse(
`type castHelper struct{ field string }
func (w castHelper) AsBool() string { return "(CAST " + w.field + " AS BOOL)" }
func (w castHelper) AsInt() string { return "(CAST " + w.field + " AS INTEGER)" }
func (w castHelper) AsDouble() string { return "(CAST " + w.field + " AS DOUBLE)" }
func (w castHelper) AsBlob() string { return "(CAST " + w.field + " AS BLOB)" }
func (w castHelper) AsText() string { return "(CAST " + w.field + " AS TEXT)" }
func (w castHelper) AsArray() string { return "(CAST " + w.field + " AS ARRAY)" }
func (w castHelper) AsDoc() string { return "(CAST " + w.field + " AS DOCUMENT)" }
`))
genjiCastTpl = template.Must(template.New("genjiCastTpl").Parse(
`var {{.Name}}CastFields = struct {
{{range $field := .Fields -}}
{{$field.Name}} castHelper
{{end}}
}{
{{range $field := .Fields -}}
{{$field.Name}}: castHelper{field: "{{ .Value }}"},
{{end}}
}
`))
)