template: passed var and conditional names don't include braces

pass []string{"username"}, rather than []string{"{username}"}. Tests
have been updated.
This commit is contained in:
Harvey Tindall
2025-08-23 14:59:04 +01:00
parent 94efe9f746
commit 0b43ad4ed5
2 changed files with 10 additions and 11 deletions

View File

@@ -18,6 +18,7 @@ func truthy(val interface{}) bool {
}
// Templater for custom emails.
// Slices "variables", "conditionals", and map "values" should NOT wrap names in { and }.
// Variables should be written as {varName}.
// If statements should be written as {if (!)varName}...{endif}.
// Strings are true if != "", ints are true if != 0.
@@ -95,8 +96,7 @@ func templateEmail(content string, variables []string, conditionals []string, va
positive = false
varName = varName[1:]
}
wrappedVarName := "{" + varName + "}"
validVar := slices.Contains(conditionals, wrappedVarName)
validVar := slices.Contains(conditionals, varName)
if validVar {
ifTrue = positive == truthy(values[varName])
} else {
@@ -123,10 +123,9 @@ func templateEmail(content string, variables []string, conditionals []string, va
if ifStart != -1 {
continue
}
wrappedVarName := "{" + varName + "}"
validVar := slices.Contains(variables, wrappedVarName)
validVar := slices.Contains(variables, varName)
if !validVar {
out += wrappedVarName
out += "{" + varName + "}"
continue
}
out += fmt.Sprint(values[varName])

View File

@@ -23,7 +23,7 @@ func TestBlankTemplate(t *testing.T) {
func testConditional(isTrue bool, t *testing.T) {
in := `Success, {username}! Your account has been created. {if myCondition}Log in at {myAccountURL} with username {username} to get started.{endif}`
vars := []string{"{username}", "{myAccountURL}", "{myCondition}"}
vars := []string{"username", "myAccountURL", "myCondition"}
conds := vars
vals := map[string]any{
"username": "TemplateUsername",
@@ -64,7 +64,7 @@ func TestConditionalFalse(t *testing.T) {
func TestTemplateDoubleBraceGracefulHandling(t *testing.T) {
in := `Success, {{username}}! Your account has been created. Log in at {myAccountURL} with username {username} to get started.`
vars := []string{"{username}", "{myAccountURL}"}
vars := []string{"username", "myAccountURL"}
vals := map[string]any{
"username": "TemplateUsername",
"myAccountURL": "TemplateURL",
@@ -87,16 +87,16 @@ func TestTemplateDoubleBraceGracefulHandling(t *testing.T) {
func TestVarAtAnyPosition(t *testing.T) {
in := `Success, user! Your account has been created. Log in at myAccountURL with your username to get started.`
vars := []string{"{username}", "{myAccountURL}"}
vars := []string{"username", "myAccountURL"}
vals := map[string]any{
"username": "TemplateUsername",
"myAccountURL": "TemplateURL",
}
for i := range in {
newIn := in[0:i] + vars[0] + in[i:]
newIn := in[0:i] + "{" + vars[0] + "}" + in[i:]
target := strings.ReplaceAll(newIn, vars[0], vals["username"].(string))
target := strings.ReplaceAll(newIn, "{"+vars[0]+"}", vals["username"].(string))
out, err := templateEmail(newIn, vars, []string{}, vals)
@@ -105,7 +105,7 @@ func TestVarAtAnyPosition(t *testing.T) {
}
if out != target {
t.Fatalf(`returned string doesn't match desired output: "%+v" != "%+v"`, out, target)
t.Fatalf(`returned string doesn't match desired output: "%+v" != "%+v, from "%+v""`, out, target, newIn)
}
}
}