Go模板的if条件语句

huangapple go评论139阅读模式
英文:

Go Template if condition

问题

如何将andeq/ne函数组合在一起?

我写了这段代码:

{{ define "opsgenie.default.tmpl" }}
  <font size="+0"><b>{{.CommonLabels.alertname }}</b></font>
  {{- range $i, $alert := .Alerts }}
  <font size="+0">{{ .Annotations.description }}</font>
  {{- end -}}
  {{- "\n" -}}
  {{- "\n" -}}
  {{- if and eq .CommonLabels.infoalert "true" eq .CommonLabels.topic "database" -}}
  Grafana: https://{{ .CommonLabels.url }}
  {{- "\n" -}}{{- end -}}
  {{- if and ne .CommonLabels.infoalert "true" eq .CommonLabels.topic "database" -}}
  Database:
    • https://{{ .CommonLabels.url }}/
    • https://{{ .CommonLabels.url }}/
  {{- "\n" -}}{{- end -}}
  {{- end -}}
  {{- end -}}
{{- end -}}

目标是:

  • 如果我的警报同时包含infoalert: truetopic: database标签,则只显示Grafana链接。
  • 如果我的警报只包含topic: database标签,但不包含infoalert: true标签,则只显示数据库链接。

看起来条件语句{{- if and eq .CommonLabels.infoalert "true" eq .CommonLabels.topic "database" -}}的语法不正确,因为当触发警报时,我在alertmanager.log中收到以下错误:

notify retry canceled due to unrecoverable error after 1 attempts: templating error: template: email.tmpl:24:17: executing "opsgenie.default.tmpl" at <eq>: wrong number of args for eq: want at least 1 got 0
英文:

how can i combine and and eq/ne functions together?

I wrote this snippet

{{ define &quot;opsgenie.default.tmpl&quot; }}
  &lt;font size=&quot;+0&quot;&gt;&lt;b&gt;{{.CommonLabels.alertname }}&lt;/b&gt;&lt;/font&gt;
  {{- range $i, $alert := .Alerts }}
  &lt;font size=&quot;+0&quot;&gt;{{ .Annotations.description }}&lt;/font&gt;
  {{- end -}}
  {{- &quot;\n&quot; -}}
  {{- &quot;\n&quot; -}}
  {{- if and eq .CommonLabels.infoalert &quot;true&quot; eq .CommonLabels.topic &quot;database&quot; -}}
  Grafana: https://{{ .CommonLabels.url }}
  {{- &quot;\n&quot; -}}{{- end -}}
  {{- if and ne .CommonLabels.infoalert &quot;true&quot; eq .CommonLabels.topic &quot;database&quot; -}}
  Database:
    • https://{{ .CommonLabels.url }}/
    • https://{{ .CommonLabels.url }}/
  {{- &quot;\n&quot; -}}{{- end -}}
  {{- end -}}
  {{- end -}}
{{- end -}}

The goal is:

  • if my alert contains both labels infoalert: true and topic: database then show only Grafana link
  • if my alert contains only label topic: database but not infoalert: true then show only Databsse link

It looks like the syntax for condition {{- if and eq .CommonLabels.infoalert &quot;true&quot; eq .CommonLabels.topic &quot;database&quot; -}} is not correct because i get this error in alertmanager.log when alert is fired:

notify retry canceled due to unrecoverable error after 1 attempts: templating error: template: email.tmpl:24:17: executing \&quot;opsgenie.default.tmpl\&quot; at &lt;eq&gt;: wrong number of args for eq: want at least 1 got 0

答案1

得分: 1

只需使用括号将表达式分组:

{{- if and (eq .CommonLabels.infoalert "true") (eq .CommonLabels.topic "database") -}}

{{- if and (ne .CommonLabels.infoalert "true") (eq .CommonLabels.topic "database") -}}

请参考以下可测试的示例:

func main() {
    t := template.Must(template.New("").Parse(src))

    m := map[string]interface{}{
        "infoalert": "true",
        "topic":     "database",
    }
    if err := t.Execute(os.Stdout, m); err != nil {
        panic(err)
    }

    fmt.Println("Second round")
    m["infoalert"] = "false"
    if err := t.Execute(os.Stdout, m); err != nil {
        panic(err)
    }
}

const src = `
{{- if and (eq .infoalert "true") (eq .topic "database") -}}
    infoalert is true and topic is database
{{- end -}}
{{- if and (ne .infoalert "true") (eq .topic "database") -}}
    infoalert is NOT true and topic is database
{{ end }}
`

这将输出以下内容(在Go Playground上尝试):

infoalert is true and topic is database
Second round
infoalert is NOT true and topic is database
英文:

Simply use parenthesis to group the expressions:

{{- if and (eq .CommonLabels.infoalert &quot;true&quot;) (eq .CommonLabels.topic &quot;database&quot;) -}}

{{- if and (ne .CommonLabels.infoalert &quot;true&quot;) (eq .CommonLabels.topic &quot;database&quot;) -}}

See this testable example:

func main() {
	t := template.Must(template.New(&quot;&quot;).Parse(src))

	m := map[string]any{
		&quot;infoalert&quot;: &quot;true&quot;,
		&quot;topic&quot;:     &quot;database&quot;,
	}
	if err := t.Execute(os.Stdout, m); err != nil {
		panic(err)
	}

	fmt.Println(&quot;Second round&quot;)
	m[&quot;infoalert&quot;] = &quot;false&quot;
	if err := t.Execute(os.Stdout, m); err != nil {
		panic(err)
	}
}

const src = `
{{- if and (eq .infoalert &quot;true&quot;) (eq .topic &quot;database&quot;) -}}
    infoalert is true and topic is database
{{- end -}}
{{- if and (ne .infoalert &quot;true&quot;) (eq .topic &quot;database&quot;) -}}
    infoalert is NOT true and topic is database
{{ end }}
`

This will output (try it on the Go Playground):

infoalert is true and topic is database
Second round
infoalert is NOT true and topic is database

huangapple
  • 本文由 发表于 2023年4月13日 17:29:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定