-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
66 lines (62 loc) · 1.33 KB
/
template.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
package template
import (
_ "embed"
"fmt"
"github.com/iancoleman/strcase"
"io"
"strings"
"text/template"
)
var (
//go:embed constants.tmpl
TemplateConstants string
//go:embed db.tmpl
TemplateDB string
//go:embed db_all.tmpl
TemplateDBAll string
//go:embed delete.tmpl
TemplateDelete string
//go:embed insert.tmpl
TemplateInsert string
//go:embed model.tmpl
TemplateModel string
//go:embed order.tmpl
TemplateOrder string
//go:embed runtime.tmpl
TemplateRuntime string
//go:embed select.tmpl
TemplateSelect string
//go:embed update.tmpl
TemplateUpdate string
//go:embed variable.tmpl
TemplateVariable string
//go:embed where.tmpl
TemplateWhere string
)
var funcMap = template.FuncMap{
"snakeCase": func(s string) string {
return strcase.ToSnake(s)
},
"escape": func(s string) string {
return fmt.Sprintf("`%s`", s)
},
"camelCase": func(s string) string {
return strcase.ToLowerCamel(s)
},
"titleCase": func(s string) string {
return strcase.ToCamel(s)
},
"stringsJoin": func(elems []string, sep string) string {
return strings.Join(elems, sep)
},
"newSlice": func(args ...interface{}) interface{} {
return args
},
}
func Execute(text string, wr io.Writer, data interface{}) error {
tmpl, err := template.New("").Funcs(funcMap).Parse(text)
if err != nil {
return err
}
return tmpl.Execute(wr, data)
}