-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.go
120 lines (102 loc) · 2.58 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package sketch
import (
"fmt"
"io/fs"
"strings"
"text/template"
"github.com/lestrrat-go/multifs"
"github.com/lestrrat-go/sketch/schema"
)
type Template struct {
srcs map[string]fs.FS
}
func (tmpl *Template) AddFS(prefix string, src fs.FS) {
if tmpl.srcs == nil {
tmpl.srcs = make(map[string]fs.FS)
}
tmpl.srcs[prefix] = src
}
func (tmpl *Template) Build() (*template.Template, error) {
var mfs multifs.FS
for prefix, sub := range tmpl.srcs {
if err := mfs.Mount(prefix, sub); err != nil {
return nil, fmt.Errorf(`failed to mount file system on %q: %w`, prefix, err)
}
}
var files []string
fs.WalkDir(&mfs, ".", func(name string, d fs.DirEntry, err error) error {
name = "/" + name
if err != nil {
return nil
}
if d.IsDir() {
return nil
}
if strings.HasSuffix(name, ".tmpl") {
files = append(files, name)
}
return nil
})
var tt *template.Template
tt = template.New("").Funcs(tmpl.makeFuncs(&tt))
_, err := tt.ParseFS(&mfs, files...)
if err != nil {
return nil, fmt.Errorf(`failed to parse templates: %w`, err)
}
return tt, nil
}
func (tmpl *Template) makeFuncs(tt **template.Template) template.FuncMap {
return template.FuncMap{
"comment": tmpl.comment(tt),
"hasTemplate": tmpl.hasTemplate(tt),
"runTemplate": tmpl.runTemplate(tt),
"fieldByName": tmpl.fieldByName(tt),
"increment": tmpl.increment(tt),
}
}
func (tmpl *Template) comment(tt **template.Template) func(string, interface{}) string {
return func(src string, arg interface{}) string {
if src == "" {
return ""
}
tmpl, err := template.New("comment").Parse(src)
if err != nil {
panic(err)
}
var sb strings.Builder
sb.WriteString(`// `)
if err := tmpl.Execute(&sb, arg); err != nil {
panic(err)
}
return sb.String()
}
}
func (tmpl *Template) hasTemplate(tt **template.Template) func(string) bool {
return func(name string) bool {
return (*tt).Lookup(name) != nil
}
}
func (tmpl *Template) runTemplate(tt **template.Template) func(string, interface{}) (string, error) {
return func(name string, vars interface{}) (string, error) {
var sb strings.Builder
if err := (*tt).ExecuteTemplate(&sb, name, vars); err != nil {
return "", err
}
return sb.String(), nil
}
}
func (tmpl *Template) fieldByName(**template.Template) func(schema.Interface, string) *schema.FieldSpec {
return func(s schema.Interface, name string) *schema.FieldSpec {
for _, f := range s.Fields() {
if f.GetName() == name {
return f
}
}
return nil
}
}
func (tmpl *Template) increment(**template.Template) func(int) int {
return func(v int) int {
return v + 1
}
}