Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use utils.IterateOrderedMap() as intended, via for:=range (require Go 1.23) #276

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading