Skip to content

Commit

Permalink
docs: improve and add detailed comments to template functions
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
appleboy committed Jan 23, 2025
1 parent 2da9bbe commit 3973ff0
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions util/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ var (
templatesDir = "templates"
)

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

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

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

// GetTemplateByBytes returns the parsed template as a byte.
// GetTemplateByBytes returns the parsed template as a byte slice.
// It returns an error if the template processing fails.
func GetTemplateByBytes(name string, data map[string]interface{}) ([]byte, error) {
tpl, err := processTemplate(name, data)
return tpl.Bytes(), err
}

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

0 comments on commit 3973ff0

Please sign in to comment.