From a67681b5cd539222182fd41942db0d291044e6a5 Mon Sep 17 00:00:00 2001 From: andig Date: Sat, 18 Feb 2023 14:28:01 +0100 Subject: [PATCH] chore: cleanup event templates (#6304) --- push/hub.go | 37 +++++++++---------------------------- push/pushover.go | 1 - util/format.go | 3 +-- util/format_test.go | 2 ++ 4 files changed, 12 insertions(+), 31 deletions(-) diff --git a/push/hub.go b/push/hub.go index 0663dbc8d..07c8697d0 100644 --- a/push/hub.go +++ b/push/hub.go @@ -1,6 +1,7 @@ package push import ( + "fmt" "strings" "text/template" @@ -19,41 +20,27 @@ type EventTemplateConfig struct { Title, Msg string } -// EventTemplate is the push message template for an event -type EventTemplate struct { - Title, Msg *template.Template -} - // Hub subscribes to event notifications and sends them to client devices type Hub struct { - definitions map[string]EventTemplate + definitions map[string]EventTemplateConfig sender []Messenger cache *util.Cache } // NewHub creates push hub with definitions and receiver func NewHub(cc map[string]EventTemplateConfig, cache *util.Cache) (*Hub, error) { - definitions := make(map[string]EventTemplate) - // instantiate all event templates for k, v := range cc { - var def EventTemplate - var err error - - def.Title, err = template.New("out").Funcs(template.FuncMap(sprig.FuncMap())).Parse(v.Title) - if err == nil { - def.Msg, err = template.New("out").Funcs(template.FuncMap(sprig.FuncMap())).Parse(v.Msg) + if _, err := template.New("out").Funcs(template.FuncMap(sprig.FuncMap())).Parse(v.Title); err != nil { + return nil, fmt.Errorf("invalid event title: %s (%w)", k, err) } - - if err != nil { - return nil, err + if _, err := template.New("out").Funcs(template.FuncMap(sprig.FuncMap())).Parse(v.Msg); err != nil { + return nil, fmt.Errorf("invalid event message: %s (%w)", k, err) } - - definitions[k] = def } h := &Hub{ - definitions: definitions, + definitions: cc, cache: cache, } @@ -66,7 +53,7 @@ func (h *Hub) Add(sender Messenger) { } // apply applies the event template to the content to produce the actual message -func (h *Hub) apply(ev Event, tmpl *template.Template) (string, error) { +func (h *Hub) apply(ev Event, tmpl string) (string, error) { attr := make(map[string]interface{}) // loadpoint id @@ -81,13 +68,7 @@ func (h *Hub) apply(ev Event, tmpl *template.Template) (string, error) { } } - // apply data attributes to template using sprig functions - applied := new(strings.Builder) - if err := tmpl.Execute(applied, attr); err != nil { - return "", err - } - - return util.ReplaceFormatted(applied.String(), attr) + return util.ReplaceFormatted(tmpl, attr) } // Run is the Hub's main publishing loop diff --git a/push/pushover.go b/push/pushover.go index 09832b7c1..502e0b1da 100644 --- a/push/pushover.go +++ b/push/pushover.go @@ -26,7 +26,6 @@ func NewPushOverFromConfig(other map[string]interface{}) (Messenger, error) { App string Recipients []string Devices []string - Events map[string]EventTemplate } if err := util.DecodeOther(other, &cc); err != nil { diff --git a/util/format.go b/util/format.go index e8149762e..abcfea839 100644 --- a/util/format.go +++ b/util/format.go @@ -61,8 +61,7 @@ func ReplaceFormatted(s string, kv map[string]interface{}) (string, error) { } var rs bytes.Buffer - err = tpl.Execute(&rs, kv) - if err != nil { + if err := tpl.Execute(&rs, kv); err != nil { return s, err } s = rs.String() diff --git a/util/format_test.go b/util/format_test.go index dcb5e3bfe..7cc23bcd8 100644 --- a/util/format_test.go +++ b/util/format_test.go @@ -38,6 +38,8 @@ func TestReplace(t *testing.T) { {"foo", true, "${foo}", "true"}, {"foo", "1", "abc${foo}${foo}", "abc11"}, {"foo", math.Pi, "${foo:%.2f}", "3.14"}, + {"foo", math.Pi, "${foo:%.0f}%", "3%"}, + {"foo", 3, "${foo}%", "3%"}, } for _, c := range cases {