Skip to content

Commit 722b6ae

Browse files
committed
Use utils.IterateOrderedMap() as intended, via for:=range (require Go 1.23)
1 parent 114c1be commit 722b6ae

File tree

4 files changed

+9
-22
lines changed

4 files changed

+9
-22
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/icinga/icinga-notifications
22

3-
go 1.22
3+
go 1.23
44

55
require (
66
github.com/creasty/defaults v1.7.0

internal/utils/utils.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/icinga/icinga-go-library/types"
1010
"github.com/jmoiron/sqlx"
1111
"github.com/pkg/errors"
12+
"iter"
1213
"slices"
1314
"strings"
1415
)
@@ -149,11 +150,7 @@ func ToDBInt(value int64) types.Int {
149150
//
150151
// This function returns a func yielding key-value-pairs from a given map in the order of their keys, if their type
151152
// is cmp.Ordered.
152-
//
153-
// Please note that currently - being at Go 1.22 - rangefuncs are still an experimental feature and cannot be directly
154-
// used unless compiled with `GOEXPERIMENT=rangefunc`. However, they can still be invoked normally.
155-
// https://go.dev/wiki/RangefuncExperiment
156-
func IterateOrderedMap[K cmp.Ordered, V any](m map[K]V) func(func(K, V) bool) {
153+
func IterateOrderedMap[K cmp.Ordered, V any](m map[K]V) iter.Seq2[K, V] {
157154
keys := make([]K, 0, len(m))
158155
for key := range m {
159156
keys = append(keys, key)

internal/utils/utils_test.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,10 @@ func TestIterateOrderedMap(t *testing.T) {
3636
for _, tt := range tests {
3737
t.Run(tt.name, func(t *testing.T) {
3838
var outKeys []int
39-
40-
// Either run with GOEXPERIMENT=rangefunc or wait for rangefuncs to land in the next Go release.
41-
// for k, _ := range IterateOrderedMap(tt.in) {
42-
// outKeys = append(outKeys, k)
43-
// }
44-
45-
// In the meantime, it can be invoked as follows.
46-
IterateOrderedMap(tt.in)(func(k int, v string) bool {
39+
for k, v := range IterateOrderedMap(tt.in) {
4740
assert.Equal(t, tt.in[k], v)
4841
outKeys = append(outKeys, k)
49-
return true
50-
})
42+
}
5143

5244
assert.Equal(t, tt.outKeys, outKeys)
5345
})

pkg/plugin/plugin.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -299,17 +299,15 @@ func FormatMessage(writer io.Writer, req *NotificationRequest) {
299299
}
300300
_, _ = fmt.Fprintf(writer, "Object: %s\n\n", req.Object.Url)
301301
_, _ = writer.Write([]byte("Tags:\n"))
302-
utils.IterateOrderedMap(req.Object.Tags)(func(k, v string) bool {
302+
for k, v := range utils.IterateOrderedMap(req.Object.Tags) {
303303
_, _ = fmt.Fprintf(writer, "%s: %s\n", k, v)
304-
return true
305-
})
304+
}
306305

307306
if len(req.Object.ExtraTags) > 0 {
308307
_, _ = writer.Write([]byte("\nExtra Tags:\n"))
309-
utils.IterateOrderedMap(req.Object.ExtraTags)(func(k, v string) bool {
308+
for k, v := range utils.IterateOrderedMap(req.Object.ExtraTags) {
310309
_, _ = fmt.Fprintf(writer, "%s: %s\n", k, v)
311-
return true
312-
})
310+
}
313311
}
314312

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

0 commit comments

Comments
 (0)