diff --git a/go.mod b/go.mod index f3503da4..02a218dd 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/icinga/icinga-notifications -go 1.22 +go 1.23 require ( github.com/creasty/defaults v1.7.0 diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 40f1898a..bdcd3a45 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -9,6 +9,7 @@ import ( "github.com/icinga/icinga-go-library/types" "github.com/jmoiron/sqlx" "github.com/pkg/errors" + "iter" "slices" "strings" ) @@ -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) diff --git a/internal/utils/utils_test.go b/internal/utils/utils_test.go index d3b474a5..3c9f10e8 100644 --- a/internal/utils/utils_test.go +++ b/internal/utils/utils_test.go @@ -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) }) diff --git a/pkg/plugin/plugin.go b/pkg/plugin/plugin.go index 9e28237d..b9a49d2c 100644 --- a/pkg/plugin/plugin.go +++ b/pkg/plugin/plugin.go @@ -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)