Skip to content

Commit

Permalink
Push: fix missing template variables (#13917)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed May 15, 2024
1 parent 72ab555 commit f69a0d7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
10 changes: 5 additions & 5 deletions push/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
25 changes: 14 additions & 11 deletions util/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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%.]+))?}`)
Expand Down Expand Up @@ -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

Check failure on line 80 in util/format.go

View workflow job for this annotation

GitHub Actions / Lint

exporting a pointer for the loop variable v (exportloopref)

Check failure on line 80 in util/format.go

View workflow job for this annotation

GitHub Actions / Lint

exporting a pointer for the loop variable v (exportloopref)
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
}
1 change: 1 addition & 0 deletions util/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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%"},
Expand Down

0 comments on commit f69a0d7

Please sign in to comment.