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

Support Go 1.23 iterators for the ZADD command #652

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 11 additions & 1 deletion internal/cmds/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

package cmds

import "iter"
import (
"fmt"
"iter"
)

func (c HmsetFieldValue) FieldValueIter(seq iter.Seq2[string, string]) HmsetFieldValue {
for field, value := range seq {
Expand All @@ -24,3 +27,10 @@ func (c XaddFieldValue) FieldValueIter(seq iter.Seq2[string, string]) XaddFieldV
}
return c
}

func (c ZaddScoreMember) ScoreMemberIter(seq iter.Seq2[float64, string]) ZaddScoreMember {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (c ZaddScoreMember) ScoreMemberIter(seq iter.Seq2[float64, string]) ZaddScoreMember {
func (c ZaddScoreMember) ScoreMemberIter(seq iter.Seq2[string, float64]) ZaddScoreMember {

Using [string, float64] will be much more convenient for users because the first value usually refers to a unique key.

And Redis sorted set allows elements with the same score, so we better put float64 at the second value.

for score, member := range seq {
c.cs.s = append(c.cs.s, fmt.Sprintf("%f", score), member)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
c.cs.s = append(c.cs.s, fmt.Sprintf("%f", score), member)
c.cs.s = append(c.cs.s, strconv.FormatFloat(score, 'f', -1, 64), member)

strconv.FormatFloat is much more performant than fmt.Sprintf

}
return c
}
1 change: 1 addition & 0 deletions internal/cmds/iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func iter0(s Builder) {
s.Hmset().Key("1").FieldValue().FieldValueIter(maps.All(map[string]string{"1": "1"})).Build()
s.Hset().Key("1").FieldValue().FieldValueIter(maps.All(map[string]string{"1": "1"})).Build()
s.Xadd().Key("1").Id("*").FieldValue().FieldValueIter(maps.All(map[string]string{"1": "1"})).Build()
s.Zadd().Key("1").ScoreMember().ScoreMemberIter(maps.All(map[float64]string{float64(1): "1"})).Build()
}

func TestIter(t *testing.T) {
Expand Down