template: add the default template function stringSlice#2101
Merged
Conversation
Since it's impossible to create a string slice in a Go template by default, add a function to work around this problem. The use case is to make it easy to call KV.Remove with an arbitrary slice inside a template. Signed-off-by: Vincent Rischmann <vincent@rischmann.fr>
Contributor
|
Thanks! The docs over in the docs repo will also need updating. |
gburek-fastly
added a commit
to gburek-fastly/jiralert
that referenced
this pull request
Mar 20, 2020
This commit ports template functions added to alertmanager in prometheus/alertmanager#1452 and prometheus/alertmanager#2101 and brings jiralert up to date (without safeHtml) with the alertmanger code in https://github.com/prometheus/alertmanager/blob/0c0c6bdb0133e399c1a9fe4c1a170ce49ec67b58/template/template.go#L127-L147 The `match` function allows for prometheus like substring matching for more complex templates. The `stringSlice` function renders a `string` into a `[]string` that is suitable for use with `Remove`. My original use case that led to this patch was this template: ```yaml receivers: - name: jira summary: '{{ if .CommonLabels.jira_summary }}{{ .CommonLabels.jira_summary }}{{ else }}{{ .GroupLabels.SortedPairs.Values | join " " }}{{ end }}' ``` I thought, 'wouldn't it be nice to remove the name of the jira project from the title of the issue?', so I tried this template: ```yaml receivers: - name: jira summary: '{{ if .CommonLabels.jira_summary }}{{ .CommonLabels.jira_summary }}{{ else }}{{ .GroupLabels.Remove "jira_project" }}{ .SortedPairs.Values | join " " }}{{ end }}' ``` This failed with: ``` err="template: :1:105: executing \"\" at <\"jira_project\">: can't handle \"jira_project\" for arg of type []string" receiver=jira groupLabels="unsupported value type" ``` After some research, I found prometheus/alertmanager#2101, with the description: > Since it's impossible to create a string slice in a Go template by > default, add a function to work around this problem. With this patch, we should be able to pass a string directly to the template Remove function, as alertmanager does.
gburek-fastly
added a commit
to gburek-fastly/jiralert
that referenced
this pull request
Apr 10, 2020
This commit ports template functions added to alertmanager in prometheus/alertmanager#1452 and prometheus/alertmanager#2101 and brings jiralert up to date (without safeHtml) with the alertmanger code in https://github.com/prometheus/alertmanager/blob/0c0c6bdb0133e399c1a9fe4c1a170ce49ec67b58/template/template.go#L127-L147 The `match` function allows for prometheus like substring matching for more complex templates. The `stringSlice` function renders a `string` into a `[]string` that is suitable for use with `Remove`. My original use case that led to this patch was this template: ```yaml receivers: - name: jira summary: '{{ if .CommonLabels.jira_summary }}{{ .CommonLabels.jira_summary }}{{ else }}{{ .GroupLabels.SortedPairs.Values | join " " }}{{ end }}' ``` I thought, 'wouldn't it be nice to remove the name of the jira project from the title of the issue?', so I tried this template: ```yaml receivers: - name: jira summary: '{{ if .CommonLabels.jira_summary }}{{ .CommonLabels.jira_summary }}{{ else }}{{ .GroupLabels.Remove "jira_project" }}{ .SortedPairs.Values | join " " }}{{ end }}' ``` This failed with: ``` err="template: :1:105: executing \"\" at <\"jira_project\">: can't handle \"jira_project\" for arg of type []string" receiver=jira groupLabels="unsupported value type" ``` After some research, I found prometheus/alertmanager#2101, with the description: > Since it's impossible to create a string slice in a Go template by > default, add a function to work around this problem. With this patch, we should be able to pass a string directly to the template Remove function, as alertmanager does. Signed-off-by: Greg Burek <gburek@fastly.com>
free
added a commit
to prometheus-community/jiralert
that referenced
this pull request
May 14, 2020
This commit ports template functions added to alertmanager in prometheus/alertmanager#1452 and prometheus/alertmanager#2101 and brings jiralert up to date (without safeHtml) with the alertmanger code in https://github.com/prometheus/alertmanager/blob/0c0c6bdb0133e399c1a9fe4c1a170ce49ec67b58/template/template.go#L127-L147 The `match` function allows for prometheus like substring matching for more complex templates. The `stringSlice` function renders a `string` into a `[]string` that is suitable for use with `Remove`. My original use case that led to this patch was this template: ```yaml receivers: - name: jira summary: '{{ if .CommonLabels.jira_summary }}{{ .CommonLabels.jira_summary }}{{ else }}{{ .GroupLabels.SortedPairs.Values | join " " }}{{ end }}' ``` I thought, 'wouldn't it be nice to remove the name of the jira project from the title of the issue?', so I tried this template: ```yaml receivers: - name: jira summary: '{{ if .CommonLabels.jira_summary }}{{ .CommonLabels.jira_summary }}{{ else }}{{ .GroupLabels.Remove "jira_project" }}{ .SortedPairs.Values | join " " }}{{ end }}' ``` This failed with: ``` err="template: :1:105: executing \"\" at <\"jira_project\">: can't handle \"jira_project\" for arg of type []string" receiver=jira groupLabels="unsupported value type" ``` After some research, I found prometheus/alertmanager#2101, with the description: > Since it's impossible to create a string slice in a Go template by > default, add a function to work around this problem. With this patch, we should be able to pass a string directly to the template Remove function, as alertmanager does. Signed-off-by: Greg Burek <gburek@fastly.com> Co-authored-by: Alin Sinpalean <alin.sinpalean@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since it's impossible to create a string slice in a Go template by
default, add a function to work around this problem.
The use case is to make it easy to call KV.Remove with an arbitrary
slice inside a template.
This is a follow up to #2094