-
-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Seq and SeqLEK (go 1.23 iterators)
- Loading branch information
Showing
4 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//go:build go1.23 | ||
|
||
package dynamo | ||
|
||
import ( | ||
"context" | ||
"iter" | ||
) | ||
|
||
// Seq returns an item iterator compatible with Go 1.23 `for ... range` loops. | ||
func Seq[V any](ctx context.Context, iter Iter) iter.Seq[V] { | ||
return func(yield func(V) bool) { | ||
item := new(V) | ||
for iter.Next(ctx, item) { | ||
if !yield(*item) { | ||
break | ||
} | ||
item = new(V) | ||
} | ||
} | ||
} | ||
|
||
// SeqLEK returns a LastEvaluatedKey and item iterator compatible with Go 1.23 `for ... range` loops. | ||
func SeqLEK[V any](ctx context.Context, iter PagingIter) iter.Seq2[PagingKey, V] { | ||
return func(yield func(PagingKey, V) bool) { | ||
item := new(V) | ||
for iter.Next(ctx, item) { | ||
lek, err := iter.LastEvaluatedKey(ctx) | ||
if err != nil { | ||
if setter, ok := iter.(interface{ SetError(error) }); ok { | ||
setter.SetError(err) | ||
} | ||
} | ||
if !yield(lek, *item) { | ||
break | ||
} | ||
item = new(V) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
//go:build go1.23 | ||
|
||
package dynamo | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestSeq(t *testing.T) { | ||
if testDB == nil { | ||
t.Skip(offlineSkipMsg) | ||
} | ||
ctx := context.Background() | ||
table := testDB.Table(testTableWidgets) | ||
|
||
widgets := []any{ | ||
widget{ | ||
UserID: 1971, | ||
Time: time.Date(1971, 4, 00, 0, 0, 0, 0, time.UTC), | ||
Msg: "Seq1", | ||
}, | ||
widget{ | ||
UserID: 1971, | ||
Time: time.Date(1971, 4, 10, 0, 0, 0, 0, time.UTC), | ||
Msg: "Seq1", | ||
}, | ||
widget{ | ||
UserID: 1971, | ||
Time: time.Date(1971, 4, 20, 0, 0, 0, 0, time.UTC), | ||
Msg: "Seq1", | ||
}, | ||
} | ||
|
||
t.Run("prepare data", func(t *testing.T) { | ||
if _, err := table.Batch().Write().Put(widgets...).Run(ctx); err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
|
||
iter := testDB.Table(testTableWidgets).Get("UserID", 1971).Iter() | ||
var got []*widget | ||
var count int | ||
for item := range Seq[*widget](ctx, iter) { | ||
t.Log(item) | ||
item.Count = count | ||
got = append(got, item) | ||
count++ | ||
} | ||
|
||
if iter.Err() != nil { | ||
t.Fatal(iter.Err()) | ||
} | ||
|
||
t.Run("results match", func(t *testing.T) { | ||
for i, item := range got { | ||
want := widgets[i].(widget) | ||
if !item.Time.Equal(want.Time) { | ||
t.Error("bad result. want:", want.Time, "got:", item.Time) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("result item isolation", func(t *testing.T) { | ||
// make sure that when mutating the result in the `for ... range` loop | ||
// it only affects one item | ||
t.Log("got", got) | ||
for i, item := range got { | ||
if item.Count != i { | ||
t.Error("unexpected count. got:", item.Count, "want:", i) | ||
} | ||
} | ||
}) | ||
} |