mirror of
https://github.com/coder/coder.git
synced 2025-07-15 22:20:27 +00:00
chore: improve notification template tests' resilience (#14196)
This commit is contained in:
@ -9,10 +9,19 @@ import (
|
||||
"github.com/coder/coder/v2/coderd/notifications/types"
|
||||
)
|
||||
|
||||
// NoValue is used when a template variable is not found.
|
||||
// This string is not exported as a const from the text/template.
|
||||
const NoValue = "<no value>"
|
||||
|
||||
// GoTemplate attempts to substitute the given payload into the given template using Go's templating syntax.
|
||||
// TODO: memoize templates for memory efficiency?
|
||||
func GoTemplate(in string, payload types.MessagePayload, extraFuncs template.FuncMap) (string, error) {
|
||||
tmpl, err := template.New("text").Funcs(extraFuncs).Parse(in)
|
||||
tmpl, err := template.New("text").
|
||||
Funcs(extraFuncs).
|
||||
// text/template substitutes a missing label with "<no value>".
|
||||
// NOTE: html/template does not, for obvious reasons.
|
||||
Option("missingkey=invalid").
|
||||
Parse(in)
|
||||
if err != nil {
|
||||
return "", xerrors.Errorf("template parse: %w", err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user