Skip to content

Commit 3973ff0

Browse files
committed
docs: improve and add detailed comments to template functions
- Add detailed comments for the `NewTemplateByString` function - Add detailed comments for the `processTemplate` function - Improve comments for the `GetTemplateByBytes` function - Improve comments for the `LoadTemplates` function Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 2da9bbe commit 3973ff0

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

util/template.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ var (
1616
templatesDir = "templates"
1717
)
1818

19+
// NewTemplateByString parses a template from a string and executes it with the provided data.
20+
// It returns the resulting string or an error if the template parsing or execution fails.
1921
func NewTemplateByString(format string, data map[string]interface{}) (string, error) {
2022
t, err := template.New("message").Parse(format)
2123
if err != nil {
@@ -32,6 +34,7 @@ func NewTemplateByString(format string, data map[string]interface{}) (string, er
3234
}
3335

3436
// processTemplate processes the template with the given name and data.
37+
// It returns the resulting bytes.Buffer or an error if the template execution fails.
3538
func processTemplate(name string, data map[string]interface{}) (*bytes.Buffer, error) {
3639
t, ok := templates[name]
3740
if !ok {
@@ -48,18 +51,21 @@ func processTemplate(name string, data map[string]interface{}) (*bytes.Buffer, e
4851
}
4952

5053
// GetTemplateByString returns the parsed template as a string.
54+
// It returns an error if the template processing fails.
5155
func GetTemplateByString(name string, data map[string]interface{}) (string, error) {
5256
tpl, err := processTemplate(name, data)
5357
return tpl.String(), err
5458
}
5559

56-
// GetTemplateByBytes returns the parsed template as a byte.
60+
// GetTemplateByBytes returns the parsed template as a byte slice.
61+
// It returns an error if the template processing fails.
5762
func GetTemplateByBytes(name string, data map[string]interface{}) ([]byte, error) {
5863
tpl, err := processTemplate(name, data)
5964
return tpl.Bytes(), err
6065
}
6166

62-
// LoadTemplates loads all the templates found in the templates directory.
67+
// LoadTemplates loads all the templates found in the templates directory from the embedded filesystem.
68+
// It returns an error if reading the directory or parsing any template fails.
6369
func LoadTemplates(files embed.FS) error {
6470
if templates == nil {
6571
templates = make(map[string]*template.Template)

0 commit comments

Comments
 (0)