-
Notifications
You must be signed in to change notification settings - Fork 1
/
set.go
130 lines (114 loc) · 2.99 KB
/
set.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
package html
// Set is a collection of template sources.
// It allows to create a View from its sources.
type Set struct {
loader *Loader
sources []*Source
funcs map[string]interface{}
}
func newSet(l *Loader) *Set {
set := &Set{
loader: l,
funcs: createFuncMap(l),
}
return set
}
// Add appends a template source, identified by its file path, to the set.
// A copy of the set is returned.
func (s *Set) Add(files ...string) *Set {
ret := s.clone()
for _, file := range files {
ret.sources = append(ret.sources, &Source{FilePath: file})
}
return ret
}
// AddSet appends the template sources of another set to this one.
// A copy of the set is returned.
func (s *Set) AddSet(set *Set) *Set {
return s.AddSets(set)
}
// AddSets appends the template sources of other sets to this one.
// It returns a copy of the Set.
func (s *Set) AddSets(sets ...*Set) *Set {
ret := s.clone()
for _, set := range sets {
for _, src := range set.Sources() {
ret.sources = append(ret.sources, &src)
}
for name, fn := range set.Funcs() {
ret.funcs[name] = fn
}
}
return ret
}
// Set appends a template source, identified by its file path and name,
// to the set. A copy of the set is returned.
func (s *Set) Set(name, file string) *Set {
ret := s.clone()
ret.sources = append(ret.sources, &Source{Name: name, FilePath: file})
return ret
}
// AddFunc appends a named function to the set's function map. It is legal to
// overwrite elements of the map. A copy of the set is returned.
func (s *Set) AddFunc(name string, fn interface{}) *Set {
ret := s.clone()
ret.funcs[name] = fn
return ret
}
// AddFuncs appends the functions of the passed-in function map to
// the set's function map. It is legal to overwrite elements of the map.
// A copy of the set is returned.
func (s *Set) AddFuncs(funcMap map[string]interface{}) *Set {
ret := s.clone()
for name, fn := range funcMap {
ret.funcs[name] = fn
}
return ret
}
// Funcs returns a copy of the set's function map.
func (s *Set) Funcs() map[string]interface{} {
ret := make(map[string]interface{}, len(s.funcs))
for name, fn := range s.funcs {
ret[name] = fn
}
return ret
}
// Sources returns a copy of the set's template sources.
func (s *Set) Sources() []Source {
ret := make([]Source, len(s.sources))
for i, src := range s.sources {
ret[i] = *src
}
return ret
}
// View creates a new view from the set's template sources.
// If an error occurs, the returned view is nil.
func (s *Set) View() (*View, error) {
view := &View{set: s}
err := s.create(view)
return view, err
}
// ViewMust creates a new view from the set's template sources.
// It panics if an error occurs.
func (s *Set) ViewMust() *View {
view, err := s.View()
if err != nil {
panic(err)
}
return view
}
func (s *Set) clone() *Set {
copy := *s
return ©
}
func (s *Set) create(v *View) error {
if v.template != nil && !s.loader.conf.AutoReload {
return nil
}
parsedTmpl, err := s.loader.buildTemplate(s)
if err != nil {
return err
}
v.template = parsedTmpl
return nil
}