From f69a0d796c15851cf3b55339054705cc0d721297 Mon Sep 17 00:00:00 2001 From: andig Date: Wed, 15 May 2024 21:40:13 +0200 Subject: [PATCH] Push: fix missing template variables (#13917) --- push/hub.go | 10 +++++----- util/format.go | 25 ++++++++++++++----------- util/format_test.go | 1 + 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/push/hub.go b/push/hub.go index 9c5150eab6..61f858fddb 100644 --- a/push/hub.go +++ b/push/hub.go @@ -124,12 +124,12 @@ func (h *Hub) Run(events <-chan Event, valueChan chan util.Param) { continue } + if strings.TrimSpace(msg) == "" { + continue + } + for _, sender := range h.sender { - if strings.TrimSpace(msg) != "" { - go sender.Send(title, msg) - } else { - log.DEBUG.Printf("did not send empty message template for %s: %v", ev.Event, err) - } + go sender.Send(title, msg) } } } diff --git a/util/format.go b/util/format.go index 8f1cb5dc6a..4a26739b2e 100644 --- a/util/format.go +++ b/util/format.go @@ -9,6 +9,7 @@ import ( "time" "github.com/42atomys/sprout" + "golang.org/x/exp/maps" ) var re = regexp.MustCompile(`(?i)\${(\w+)(:([a-zA-Z0-9%.]+))?}`) @@ -73,27 +74,29 @@ func ReplaceFormatted(s string, kv map[string]interface{}) (string, error) { match, key, format := m[0], m[1], m[3] // find key and replacement value - val, ok := kv[strings.ToLower(key)] - if !ok { + var val *any + for k, v := range kv { + if strings.EqualFold(k, key) { + val = &v + break + } + } + + if val == nil { wanted = append(wanted, key) format = "%s" - val = "?" + val = PtrTo(any("?")) } // update all literal matches - new := FormatValue(format, val) + new := FormatValue(format, *val) s = strings.ReplaceAll(s, match, new) } // return missing keys if len(wanted) > 0 { - got := make([]string, 0) - for k := range kv { - got = append(got, k) - } - - err = fmt.Errorf("wanted: %v, got: %v", wanted, got) + return "", fmt.Errorf("wanted: %v, got: %v", wanted, maps.Keys(kv)) } - return s, err + return s, nil } diff --git a/util/format_test.go b/util/format_test.go index da21fdcd39..561da2bc37 100644 --- a/util/format_test.go +++ b/util/format_test.go @@ -37,6 +37,7 @@ func TestReplace(t *testing.T) { // regex tests {"foo", true, "${foo}", "true"}, {"foo", true, "${Foo}", "true"}, + {"Foo", true, "${foo}", "true"}, {"foo", "1", "abc${foo}${foo}", "abc11"}, {"foo", math.Pi, "${foo:%.2f}", "3.14"}, {"foo", math.Pi, "${foo:%.0f}%", "3%"},