Go模板的if条件语句

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

Go Template if condition

问题

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

我写了这段代码:

  1. {{ define "opsgenie.default.tmpl" }}
  2. <font size="+0"><b>{{.CommonLabels.alertname }}</b></font>
  3. {{- range $i, $alert := .Alerts }}
  4. <font size="+0">{{ .Annotations.description }}</font>
  5. {{- end -}}
  6. {{- "\n" -}}
  7. {{- "\n" -}}
  8. {{- if and eq .CommonLabels.infoalert "true" eq .CommonLabels.topic "database" -}}
  9. Grafana: https://{{ .CommonLabels.url }}
  10. {{- "\n" -}}{{- end -}}
  11. {{- if and ne .CommonLabels.infoalert "true" eq .CommonLabels.topic "database" -}}
  12. Database:
  13. https://{{ .CommonLabels.url }}/
  14. https://{{ .CommonLabels.url }}/
  15. {{- "\n" -}}{{- end -}}
  16. {{- end -}}
  17. {{- end -}}
  18. {{- end -}}

目标是:

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

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

  1. 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

  1. {{ define &quot;opsgenie.default.tmpl&quot; }}
  2. &lt;font size=&quot;+0&quot;&gt;&lt;b&gt;{{.CommonLabels.alertname }}&lt;/b&gt;&lt;/font&gt;
  3. {{- range $i, $alert := .Alerts }}
  4. &lt;font size=&quot;+0&quot;&gt;{{ .Annotations.description }}&lt;/font&gt;
  5. {{- end -}}
  6. {{- &quot;\n&quot; -}}
  7. {{- &quot;\n&quot; -}}
  8. {{- if and eq .CommonLabels.infoalert &quot;true&quot; eq .CommonLabels.topic &quot;database&quot; -}}
  9. Grafana: https://{{ .CommonLabels.url }}
  10. {{- &quot;\n&quot; -}}{{- end -}}
  11. {{- if and ne .CommonLabels.infoalert &quot;true&quot; eq .CommonLabels.topic &quot;database&quot; -}}
  12. Database:
  13. https://{{ .CommonLabels.url }}/
  14. https://{{ .CommonLabels.url }}/
  15. {{- &quot;\n&quot; -}}{{- end -}}
  16. {{- end -}}
  17. {{- end -}}
  18. {{- 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:

  1. 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

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

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

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

  1. func main() {
  2. t := template.Must(template.New("").Parse(src))
  3. m := map[string]interface{}{
  4. "infoalert": "true",
  5. "topic": "database",
  6. }
  7. if err := t.Execute(os.Stdout, m); err != nil {
  8. panic(err)
  9. }
  10. fmt.Println("Second round")
  11. m["infoalert"] = "false"
  12. if err := t.Execute(os.Stdout, m); err != nil {
  13. panic(err)
  14. }
  15. }
  16. const src = `
  17. {{- if and (eq .infoalert "true") (eq .topic "database") -}}
  18. infoalert is true and topic is database
  19. {{- end -}}
  20. {{- if and (ne .infoalert "true") (eq .topic "database") -}}
  21. infoalert is NOT true and topic is database
  22. {{ end }}
  23. `

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

  1. infoalert is true and topic is database
  2. Second round
  3. infoalert is NOT true and topic is database
英文:

Simply use parenthesis to group the expressions:

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

See this testable example:

  1. func main() {
  2. t := template.Must(template.New(&quot;&quot;).Parse(src))
  3. m := map[string]any{
  4. &quot;infoalert&quot;: &quot;true&quot;,
  5. &quot;topic&quot;: &quot;database&quot;,
  6. }
  7. if err := t.Execute(os.Stdout, m); err != nil {
  8. panic(err)
  9. }
  10. fmt.Println(&quot;Second round&quot;)
  11. m[&quot;infoalert&quot;] = &quot;false&quot;
  12. if err := t.Execute(os.Stdout, m); err != nil {
  13. panic(err)
  14. }
  15. }
  16. const src = `
  17. {{- if and (eq .infoalert &quot;true&quot;) (eq .topic &quot;database&quot;) -}}
  18. infoalert is true and topic is database
  19. {{- end -}}
  20. {{- if and (ne .infoalert &quot;true&quot;) (eq .topic &quot;database&quot;) -}}
  21. infoalert is NOT true and topic is database
  22. {{ end }}
  23. `

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

  1. infoalert is true and topic is database
  2. Second round
  3. 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:

确定