-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
106 lines (91 loc) · 2.37 KB
/
render.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
package tmpl
import (
"bytes"
"errors"
"fmt"
"html/template"
"io"
)
type RenderProcess struct {
Targets []string
Template *template.Template
}
type RenderOption func(p *RenderProcess)
// WithName copies the Template's default parse.Tree and adds it back
// to the Template under the given name, effectively aliasing the Template.
func WithName(name string) RenderOption {
return func(p *RenderProcess) {
p.Template = template.Must(p.Template.AddParseTree(name, p.Template.Tree.Copy()))
}
}
// WithTarget sets the render Target to the given Template name.
func WithTarget(target ...string) RenderOption {
return func(p *RenderProcess) {
p.Targets = append(p.Targets, target...)
}
}
// WithFuncs appends the given Template.FuncMap to the Template's internal
// func map. These functions become available in the Template during execution
func WithFuncs(funcs template.FuncMap) RenderOption {
return func(p *RenderProcess) {
p.Template = p.Template.Funcs(funcs)
}
}
func (tmpl *managedTemplate[T]) Render(wr io.Writer, data T, opts ...RenderOption) error {
tmpl.mu.RLock()
t, err := tmpl.template.Clone()
tmpl.mu.RUnlock()
if err != nil {
return err
}
// defer a panic boundary to catch errors thrown by any of the
// given visitor functions
defer func() {
if r := recover(); r != nil {
switch t := r.(type) {
case string:
err = errors.New(t)
case error:
err = t
default:
err = fmt.Errorf("recovered panic during Template render option: %v", t)
}
}
}()
p := &RenderProcess{
Template: t,
Targets: []string{},
}
for _, opt := range opts {
opt(p)
}
// render the default template if no targets are provided.
if len(p.Targets) == 0 {
p.Targets = append(p.Targets, t.Tree.ParseName)
}
buf := bytes.Buffer{}
for _, target := range p.Targets {
if err := p.Template.ExecuteTemplate(&buf, target, data); err != nil {
return err
}
}
_, err = wr.Write(buf.Bytes())
return err
}
func (tmpl *managedTemplate[T]) RenderToChan(ch chan string, data T, opts ...RenderOption) error {
buf := bytes.Buffer{}
err := tmpl.Render(&buf, data, opts...)
if err != nil {
return err
}
ch <- buf.String()
return nil
}
func (tmpl *managedTemplate[T]) RenderToString(data T, opts ...RenderOption) (string, error) {
buf := bytes.Buffer{}
err := tmpl.Render(&buf, data, opts...)
if err != nil {
return "", err
}
return buf.String(), nil
}