Skip to content

Commit 6bedcd9

Browse files
committed
refactor: refactor template methods and structs for improved readability
- Refactor `buildTemplate` method to separate template creation and delimiter setting for better readability - Reformat `AddFromStringsFuncsWithOptions` and `AddFromFilesFuncsWithOptions` functions to use multi-line parameters for improved readability - Group related struct fields in `templateBuilder` initialization for better code organization - Convert type definitions to a single `type` block in `multitemplate.go` for better structure - Reformat `AddFromStringsFuncsWithOptions` and `AddFromFilesFuncsWithOptions` methods in `multitemplate.go` to use multi-line parameters and template creation for better readability - Update `Renderer` interface to use multi-line parameters for `AddFromStringsFuncsWithOptions` and `AddFromFilesFuncsWithOptions` methods for improved readability Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
1 parent 0502f3a commit 6bedcd9

File tree

3 files changed

+83
-23
lines changed

3 files changed

+83
-23
lines changed

dynamic.go

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,36 @@ func (tb templateBuilder) buildTemplate() *template.Template {
6666
case templateType:
6767
return tb.tmpl.Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter)
6868
case filesTemplateType:
69-
return template.Must(template.ParseFiles(tb.files...)).Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter)
69+
tmpl := template.Must(template.ParseFiles(tb.files...))
70+
return tmpl.Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter)
7071
case globTemplateType:
71-
return template.Must(template.ParseGlob(tb.glob)).Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter)
72+
tmpl := template.Must(template.ParseGlob(tb.glob))
73+
return tmpl.Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter)
7274
case fsTemplateType:
73-
return template.Must(template.ParseFS(tb.fsys, tb.files...)).Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter)
75+
tmpl := template.Must(template.ParseFS(tb.fsys, tb.files...))
76+
return tmpl.Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter)
7477
case fsFuncTemplateType:
75-
return template.Must(template.New(tb.templateName).Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter).Funcs(tb.funcMap).ParseFS(tb.fsys, tb.files...))
78+
tmpl := template.New(tb.templateName).
79+
Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter).
80+
Funcs(tb.funcMap)
81+
return template.Must(tmpl.ParseFS(tb.fsys, tb.files...))
7682
case stringTemplateType:
77-
return template.Must(template.New(tb.templateName).Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter).Parse(tb.templateString))
83+
tmpl := template.New(tb.templateName).
84+
Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter)
85+
return template.Must(tmpl.Parse(tb.templateString))
7886
case stringFuncTemplateType:
79-
tmpl := template.New(tb.templateName).Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter).Funcs(tb.funcMap)
87+
tmpl := template.New(tb.templateName).
88+
Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter).
89+
Funcs(tb.funcMap)
8090
for _, ts := range tb.templateStrings {
8191
tmpl = template.Must(tmpl.Parse(ts))
8292
}
8393
return tmpl
8494
case filesFuncTemplateType:
85-
return template.Must(template.New(tb.templateName).Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter).Funcs(tb.funcMap).ParseFiles(tb.files...))
95+
tmpl := template.New(tb.templateName).
96+
Delims(tb.options.LeftDelimiter, tb.options.RightDelimiter).
97+
Funcs(tb.funcMap)
98+
return template.Must(tmpl.ParseFiles(tb.files...))
8699
default:
87100
panic("Invalid builder type for dynamic template")
88101
}
@@ -188,9 +201,15 @@ func (r DynamicRender) AddFromStringsFuncs(
188201
}
189202

190203
// AddFromStringsFuncsWithOptions supply add template from strings with options
191-
func (r DynamicRender) AddFromStringsFuncsWithOptions(name string, funcMap template.FuncMap, options TemplateOptions, templateStrings ...string) *template.Template {
204+
func (r DynamicRender) AddFromStringsFuncsWithOptions(
205+
name string,
206+
funcMap template.FuncMap,
207+
options TemplateOptions,
208+
templateStrings ...string,
209+
) *template.Template {
192210
builder := &templateBuilder{
193-
templateName: name, funcMap: funcMap,
211+
templateName: name,
212+
funcMap: funcMap,
194213
templateStrings: templateStrings,
195214
options: options,
196215
}
@@ -209,9 +228,19 @@ func (r DynamicRender) AddFromFilesFuncs(name string, funcMap template.FuncMap,
209228
}
210229

211230
// AddFromFilesFuncs supply add template from file callback func
212-
func (r DynamicRender) AddFromFilesFuncsWithOptions(name string, funcMap template.FuncMap, options TemplateOptions, files ...string) *template.Template {
231+
func (r DynamicRender) AddFromFilesFuncsWithOptions(
232+
name string,
233+
funcMap template.FuncMap,
234+
options TemplateOptions,
235+
files ...string,
236+
) *template.Template {
213237
tname := filepath.Base(files[0])
214-
builder := &templateBuilder{templateName: tname, funcMap: funcMap, files: files, options: options}
238+
builder := &templateBuilder{
239+
templateName: tname,
240+
funcMap: funcMap,
241+
files: files,
242+
options: options,
243+
}
215244
builder.buildType = filesFuncTemplateType
216245
r[name] = builder
217246
return builder.buildTemplate()

multitemplate.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import (
1010
)
1111

1212
// Render type
13-
type Render map[string]*template.Template
14-
type TemplateOptions struct {
15-
LeftDelimiter string
16-
RightDelimiter string
17-
}
13+
type (
14+
Render map[string]*template.Template
15+
TemplateOptions struct {
16+
LeftDelimiter string
17+
RightDelimiter string
18+
}
19+
)
1820

1921
type TemplateOption func(*TemplateOptions)
2022

@@ -132,11 +134,20 @@ func (r Render) AddFromStringsFuncs(
132134
}
133135

134136
// AddFromStringsFuncsWithOptions supply add template from strings with options
135-
func (r Render) AddFromStringsFuncsWithOptions(name string, funcMap template.FuncMap, options TemplateOptions, templateStrings ...string) *template.Template {
136-
tmpl := template.New(name).Delims(options.LeftDelimiter, options.RightDelimiter).Funcs(funcMap)
137+
func (r Render) AddFromStringsFuncsWithOptions(
138+
name string,
139+
funcMap template.FuncMap,
140+
options TemplateOptions,
141+
templateStrings ...string,
142+
) *template.Template {
143+
tmpl := template.New(name).
144+
Delims(options.LeftDelimiter, options.RightDelimiter).
145+
Funcs(funcMap)
137146

138147
for _, ts := range templateStrings {
139-
tmpl = template.Must(tmpl.Parse(ts)).Delims(options.LeftDelimiter, options.RightDelimiter)
148+
tmpl = template.Must(
149+
tmpl.Parse(ts),
150+
).Delims(options.LeftDelimiter, options.RightDelimiter)
140151
}
141152

142153
r.Add(name, tmpl)
@@ -152,9 +163,19 @@ func (r Render) AddFromFilesFuncs(name string, funcMap template.FuncMap, files .
152163
}
153164

154165
// AddFromFilesFuncsWithOptions supply add template from file callback func with options
155-
func (r Render) AddFromFilesFuncsWithOptions(name string, funcMap template.FuncMap, options TemplateOptions, files ...string) *template.Template {
166+
func (r Render) AddFromFilesFuncsWithOptions(
167+
name string,
168+
funcMap template.FuncMap,
169+
options TemplateOptions,
170+
files ...string,
171+
) *template.Template {
156172
tname := filepath.Base(files[0])
157-
tmpl := template.Must(template.New(tname).Delims(options.LeftDelimiter, options.RightDelimiter).Funcs(funcMap).ParseFiles(files...))
173+
tmpl := template.Must(
174+
template.New(tname).
175+
Delims(options.LeftDelimiter, options.RightDelimiter).
176+
Funcs(funcMap).
177+
ParseFiles(files...),
178+
)
158179
r.Add(name, tmpl)
159180
return tmpl
160181
}

renderer.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,17 @@ type Renderer interface {
2020
AddFromFSFuncs(name string, funcMap template.FuncMap, fsys fs.FS, files ...string) *template.Template
2121
AddFromString(name, templateString string) *template.Template
2222
AddFromStringsFuncs(name string, funcMap template.FuncMap, templateStrings ...string) *template.Template
23-
AddFromStringsFuncsWithOptions(name string, funcMap template.FuncMap, options TemplateOptions, templateStrings ...string) *template.Template
23+
AddFromStringsFuncsWithOptions(
24+
name string,
25+
funcMap template.FuncMap,
26+
options TemplateOptions,
27+
templateStrings ...string,
28+
) *template.Template
2429
AddFromFilesFuncs(name string, funcMap template.FuncMap, files ...string) *template.Template
25-
AddFromFilesFuncsWithOptions(name string, funcMap template.FuncMap, options TemplateOptions, files ...string) *template.Template
30+
AddFromFilesFuncsWithOptions(
31+
name string,
32+
funcMap template.FuncMap,
33+
options TemplateOptions,
34+
files ...string,
35+
) *template.Template
2636
}

0 commit comments

Comments
 (0)