From c24b62f19c78c3b9d92bf2b5432537254ea79571 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 18 Apr 2025 00:27:31 +0200 Subject: [PATCH] templates: add formatJSON func Move the function used to format as JSON to a separate function instead of definining it inline. Signed-off-by: Sebastiaan van Stijn --- templates/templates.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/templates/templates.go b/templates/templates.go index 4af4496d19..3f727b9399 100644 --- a/templates/templates.go +++ b/templates/templates.go @@ -13,18 +13,7 @@ import ( // basicFunctions are the set of initial // functions provided to every template. var basicFunctions = template.FuncMap{ - "json": func(v any) string { - buf := &bytes.Buffer{} - enc := json.NewEncoder(buf) - enc.SetEscapeHTML(false) - err := enc.Encode(v) - if err != nil { - panic(err) - } - - // Remove the trailing new line added by the encoder - return strings.TrimSpace(buf.String()) - }, + "json": formatJSON, "split": strings.Split, "join": strings.Join, "title": strings.Title, //nolint:nolintlint,staticcheck // strings.Title is deprecated, but we only use it for ASCII, so replacing with golang.org/x/text is out of scope @@ -103,3 +92,16 @@ func truncateWithLength(source string, length int) string { } return source[:length] } + +func formatJSON(v any) string { + buf := &bytes.Buffer{} + enc := json.NewEncoder(buf) + enc.SetEscapeHTML(false) + err := enc.Encode(v) + if err != nil { + panic(err) + } + + // Remove the trailing new line added by the encoder + return strings.TrimSpace(buf.String()) +}