Skip to content

Commit

Permalink
Use utils.IterateOrderedMap() as intended, via for:=range (require Go…
Browse files Browse the repository at this point in the history
… 1.23)
  • Loading branch information
Al2Klimov committed Aug 22, 2024
1 parent 114c1be commit 722b6ae
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/icinga/icinga-notifications

go 1.22
go 1.23

require (
github.com/creasty/defaults v1.7.0
Expand Down
7 changes: 2 additions & 5 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/icinga/icinga-go-library/types"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"iter"
"slices"
"strings"
)
Expand Down Expand Up @@ -149,11 +150,7 @@ func ToDBInt(value int64) types.Int {
//
// This function returns a func yielding key-value-pairs from a given map in the order of their keys, if their type
// is cmp.Ordered.
//
// Please note that currently - being at Go 1.22 - rangefuncs are still an experimental feature and cannot be directly
// used unless compiled with `GOEXPERIMENT=rangefunc`. However, they can still be invoked normally.
// https://go.dev/wiki/RangefuncExperiment
func IterateOrderedMap[K cmp.Ordered, V any](m map[K]V) func(func(K, V) bool) {
func IterateOrderedMap[K cmp.Ordered, V any](m map[K]V) iter.Seq2[K, V] {
keys := make([]K, 0, len(m))
for key := range m {
keys = append(keys, key)
Expand Down
12 changes: 2 additions & 10 deletions internal/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,10 @@ func TestIterateOrderedMap(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var outKeys []int

// Either run with GOEXPERIMENT=rangefunc or wait for rangefuncs to land in the next Go release.
// for k, _ := range IterateOrderedMap(tt.in) {
// outKeys = append(outKeys, k)
// }

// In the meantime, it can be invoked as follows.
IterateOrderedMap(tt.in)(func(k int, v string) bool {
for k, v := range IterateOrderedMap(tt.in) {
assert.Equal(t, tt.in[k], v)
outKeys = append(outKeys, k)
return true
})
}

assert.Equal(t, tt.outKeys, outKeys)
})
Expand Down
10 changes: 4 additions & 6 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,17 +299,15 @@ func FormatMessage(writer io.Writer, req *NotificationRequest) {
}
_, _ = fmt.Fprintf(writer, "Object: %s\n\n", req.Object.Url)
_, _ = writer.Write([]byte("Tags:\n"))
utils.IterateOrderedMap(req.Object.Tags)(func(k, v string) bool {
for k, v := range utils.IterateOrderedMap(req.Object.Tags) {
_, _ = fmt.Fprintf(writer, "%s: %s\n", k, v)
return true
})
}

if len(req.Object.ExtraTags) > 0 {
_, _ = writer.Write([]byte("\nExtra Tags:\n"))
utils.IterateOrderedMap(req.Object.ExtraTags)(func(k, v string) bool {
for k, v := range utils.IterateOrderedMap(req.Object.ExtraTags) {
_, _ = fmt.Fprintf(writer, "%s: %s\n", k, v)
return true
})
}
}

_, _ = fmt.Fprintf(writer, "\nIncident: %s", req.Incident.Url)
Expand Down

0 comments on commit 722b6ae

Please sign in to comment.