Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions docs/notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,21 @@ templating.

## Strings

| Name | Arguments | Returns | Notes |
| ------------- | ------------- | -------- | -------- |
| title | string |[strings.Title](http://golang.org/pkg/strings/#Title), capitalises first character of each word. |
| toUpper | string | [strings.ToUpper](http://golang.org/pkg/strings/#ToUpper), converts all characters to upper case. |
| toLower | string | [strings.ToLower](http://golang.org/pkg/strings/#ToLower), converts all characters to lower case. |
| trimSpace | string | [strings.TrimSpace](https://pkg.go.dev/strings#TrimSpace), removes leading and trailing white spaces. |
| match | pattern, string | [Regexp.MatchString](https://golang.org/pkg/regexp/#MatchString). Match a string using Regexp. |
| reReplaceAll | pattern, replacement, text | [Regexp.ReplaceAllString](http://golang.org/pkg/regexp/#Regexp.ReplaceAllString) Regexp substitution, unanchored. |
| join | sep string, s []string | [strings.Join](http://golang.org/pkg/strings/#Join), concatenates the elements of s to create a single string. The separator string sep is placed between elements in the resulting string. (note: argument order inverted for easier pipelining in templates.) |
| safeHtml | text string | [html/template.HTML](https://golang.org/pkg/html/template/#HTML), Marks string as HTML not requiring auto-escaping. |
| safeUrl | text string | [html/template.URL](https://golang.org/pkg/html/template/#URL), Marks string as URL not requiring auto-escaping. |
| urlUnescape | text string | [url.QueryUnescape](https://pkg.go.dev/net/url#QueryUnescape), unescapes a URL with % encoding |
| stringSlice | ...string | Returns the passed strings as a slice of strings. |
| date | string, time.Time | Returns the text representation of the time in the specified format. For documentation on formats refer to [pkg.go.dev/time](https://pkg.go.dev/time#pkg-constants). |
| tz | string, time.Time | Returns the time in the timezone. For example, Europe/Paris. |
| since | time.Time | [time.Duration](https://pkg.go.dev/time#Since), returns the duration of how much time passed from the provided time till the current system time. |
| humanizeDuration | number or string | Returns a human-readable string representing the duration, and the error if it happened. |
| Name | Arguments | Description |
| ---------------- | -------------------------- | ----------- |
| date | string, time.Time | Returns the text representation of the time in the specified format. For documentation on formats refer to [pkg.go.dev/time](https://pkg.go.dev/time#pkg-constants). |
| humanizeDuration | number or string | Returns a human-readable string representing the duration, and the error if it happened. |
| join | sep string, s []string | [strings.Join](http://golang.org/pkg/strings/#Join), concatenates the elements of s to create a single string. The separator string sep is placed between elements in the resulting string. (note: argument order inverted for easier pipelining in templates.) |
| match | pattern, string | [Regexp.MatchString](https://golang.org/pkg/regexp/#MatchString). Match a string using Regexp. |
| reReplaceAll | pattern, replacement, text | [Regexp.ReplaceAllString](http://golang.org/pkg/regexp/#Regexp.ReplaceAllString) Regexp substitution, unanchored. |
| safeHtml | text string | [html/template.HTML](https://golang.org/pkg/html/template/#HTML), Marks string as HTML not requiring auto-escaping. |
| safeUrl | text string | [html/template.URL](https://golang.org/pkg/html/template/#URL), Marks string as URL not requiring auto-escaping. |
| since | time.Time | [time.Since](https://pkg.go.dev/time#Since), returns the duration of how much time passed from the provided time till the current system time. |
| stringSlice | ...string | Returns the passed strings as a slice of strings. |
| title | string | [strings.Title](http://golang.org/pkg/strings/#Title), capitalises first character of each word. |
| toJson | any | [json.Marshal](https://pkg.go.dev/encoding/json#Marshal), returns the JSON encoding of the value. |
| toLower | string | [strings.ToLower](http://golang.org/pkg/strings/#ToLower), converts all characters to lower case. |
| toUpper | string | [strings.ToUpper](http://golang.org/pkg/strings/#ToUpper), converts all characters to upper case. |
| trimSpace | string | [strings.TrimSpace](https://pkg.go.dev/strings#TrimSpace), removes leading and trailing white spaces. |
| tz | string, time.Time | Returns the time in the timezone. For example, Europe/Paris. |
| urlUnescape | text string | [url.QueryUnescape](https://pkg.go.dev/net/url#QueryUnescape), unescapes a URL with % encoding |
8 changes: 8 additions & 0 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package template

import (
"bytes"
"encoding/json"
tmplhtml "html/template"
"io"
"net/url"
Expand Down Expand Up @@ -213,6 +214,13 @@ var DefaultFuncs = FuncMap{
},
"since": time.Since,
"humanizeDuration": commonTemplates.HumanizeDuration,
"toJson": func(v any) (string, error) {
bytes, err := json.Marshal(v)
if err != nil {
return "", err
}
return string(bytes), nil
},
}

// Pair is a key/value string pair.
Expand Down
78 changes: 78 additions & 0 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,65 @@ func TestTemplateExpansion(t *testing.T) {
},
exp: "[key2 key4]",
},
{
title: "Template using toJson with string",
in: `{{ "test" | toJson }}`,
exp: `"test"`,
},
{
title: "Template using toJson with number",
in: `{{ 42 | toJson }}`,
exp: `42`,
},
{
title: "Template using toJson with boolean",
in: `{{ true | toJson }}`,
exp: `true`,
},
{
title: "Template using toJson with map",
in: `{{ . | toJson }}`,
data: map[string]any{"key": "value", "number": 123},
exp: `{"key":"value","number":123}`,
},
{
title: "Template using toJson with slice",
in: `{{ . | toJson }}`,
data: []string{"a", "b", "c"},
exp: `["a","b","c"]`,
},
{
title: "Template using toJson with KV",
in: `{{ .CommonLabels | toJson }}`,
data: Data{
CommonLabels: KV{"severity": "critical", "job": "foo"},
},
exp: `{"job":"foo","severity":"critical"}`,
},
{
title: "Template using toJson with Alerts",
in: `{{ .Alerts | toJson }}`,
data: Data{
Alerts: Alerts{
{
Status: "firing",
Labels: KV{"alertname": "test"},
},
},
},
exp: `[{"status":"firing","labels":{"alertname":"test"},"annotations":null,"startsAt":"0001-01-01T00:00:00Z","endsAt":"0001-01-01T00:00:00Z","generatorURL":"","fingerprint":""}]`,
},
{
title: "Template using toJson with Alerts.Firing()",
in: `{{ .Alerts.Firing | toJson }}`,
data: Data{
Alerts: Alerts{
{Status: "firing"},
{Status: "resolved"},
},
},
exp: `[{"status":"firing","labels":null,"annotations":null,"startsAt":"0001-01-01T00:00:00Z","endsAt":"0001-01-01T00:00:00Z","generatorURL":"","fingerprint":""}]`,
},
} {
t.Run(tc.title, func(t *testing.T) {
f := tmpl.ExecuteTextString
Expand Down Expand Up @@ -575,6 +634,25 @@ func TestTemplateFuncs(t *testing.T) {
in: "{{ . | since | humanizeDuration }}",
data: time.Now().Add(-1 * time.Hour),
exp: "1h 0m 0s",
}, {
title: "Template using toJson with string",
in: `{{ "hello" | toJson }}`,
exp: `"hello"`,
}, {
title: "Template using toJson with map",
in: `{{ . | toJson }}`,
data: map[string]string{"key": "value"},
exp: `{"key":"value"}`,
}, {
title: "Template using toJson with Alerts.Firing()",
in: `{{ .Alerts.Firing | toJson }}`,
data: Data{
Alerts: Alerts{
{Status: "firing", Labels: KV{"alertname": "test"}},
{Status: "resolved"},
},
},
exp: `[{"status":"firing","labels":{"alertname":"test"},"annotations":null,"startsAt":"0001-01-01T00:00:00Z","endsAt":"0001-01-01T00:00:00Z","generatorURL":"","fingerprint":""}]`,
}} {
t.Run(tc.title, func(t *testing.T) {
wg := sync.WaitGroup{}
Expand Down
Loading