-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.go
166 lines (139 loc) · 3.9 KB
/
compile.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package tmpl
import (
"fmt"
"html/template"
"reflect"
"sync"
)
// CompilerOptions holds options that control the template compiler
type CompilerOptions struct {
// analyzers is a list of analyzers that are run before compilation
analyzers []Analyzer
// parseOpts are the options passed to the template parser
parseOpts ParseOptions
}
// CompilerOption is a function that can be used to modify the CompilerOptions
type CompilerOption func(opts *CompilerOptions)
func UseFuncs(funcs FuncMap) CompilerOption {
return func(opts *CompilerOptions) {
if opts.parseOpts.Funcs == nil {
opts.parseOpts.Funcs = funcs
} else {
for k, v := range funcs {
opts.parseOpts.Funcs[k] = v
}
}
}
}
func UseAnalyzers(analyzers ...Analyzer) CompilerOption {
return func(opts *CompilerOptions) {
opts.analyzers = append(opts.analyzers, analyzers...)
}
}
// UseParseOptions sets the ParseOptions for the template CompilerOptions. These
// options are used internally with the html/template package.
func UseParseOptions(parseOpts ParseOptions) CompilerOption {
return func(opts *CompilerOptions) {
opts.parseOpts = parseOpts
}
}
func compile(tp TemplateProvider, opts ParseOptions, analyzers ...Analyzer) (*template.Template, error) {
var (
err error
t *template.Template
)
helper, err := Analyze(tp, opts, analyzers)
if err != nil {
return nil, err
}
// recursively parse all templates into a single template instance
// this block is responsible for constructing the template that
// will be rendered by the user
err = recurseFieldsImplementing[TemplateProvider](tp, func(tp TemplateProvider, field reflect.StructField) error {
var (
funcMap = make(FuncMap)
templateText string
)
templateName, ok := field.Tag.Lookup("tmpl")
if !ok {
templateName = field.Name
}
if t == nil {
// if t is nil, that means this is the recursive entrypoint
// and some construction needs to happen
t = template.New(templateName)
templateText = tp.TemplateText()
t = t.Delims(opts.LeftDelim, opts.RightDelim)
// Analyzers can provide functions to be used in templates
for key, fn := range helper.FuncMap() {
funcMap[key] = fn
}
// FuncMapProvider can also be implemented and provide functions
if fmp, ok := tp.(FuncMapProvider); ok {
for key, fn := range fmp.TemplateFuncMap() {
funcMap[key] = fn
}
}
} else {
// if this is a nested template wrap its text in a {{ define }}
// statement, so it may be referenced by the "parent" template
// ex: {{define %q -}}\n%s{{end}}
templateText = fmt.Sprintf("%[1]sdefine %[3]q -%[2]s\n%[4]s%[1]send%[2]s\n", opts.LeftDelim, opts.RightDelim, templateName, tp.TemplateText())
}
t, err = t.Funcs(funcMap).Parse(templateText)
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to compile template: %+v", err)
}
return t, nil
}
// Compile takes the given TemplateProvider, parses the templateProvider text and then
// recursively compiles all nested templates into one managed Template instance.
func Compile[T TemplateProvider](tp T, opts ...CompilerOption) (Template[T], error) {
var (
c = &CompilerOptions{
analyzers: builtinAnalyzers,
parseOpts: ParseOptions{
LeftDelim: "{{",
RightDelim: "}}",
},
}
)
for _, opt := range opts {
opt(c)
}
m := &managedTemplate[T]{
mu: &sync.RWMutex{},
}
doCompile := func() (err error) {
var (
t *template.Template
)
t, err = compile(tp, c.parseOpts, c.analyzers...)
if err != nil {
return
}
m.mu.Lock()
m.template = t
m.mu.Unlock()
return
}
err := doCompile()
if err != nil {
return nil, err
}
return m, nil
}
// MustCompile is a helper function that wraps Compile and panics if the template
// fails to compile.
func MustCompile[T TemplateProvider](p T, opts ...CompilerOption) Template[T] {
tmpl, err := Compile(p, opts...)
if err != nil {
panic(err)
}
return tmpl
}