guregu / dynamo

expressive DynamoDB library for Go
Other
1.3k stars 179 forks source link

Add Seq and SeqLEK (Go 1.23 iterators) #244

Open guregu opened 1 month ago

guregu commented 1 month ago

Here's an initial implementation of Go 1.23-style iterators for Iter and PagingIter. Not sure yet if this would be the best API, or if it's useful 🤔

It lets you write code like this

iter := table.Get("UserID", 1971).Iter()
for item := range dynamo.Seq[Widget](ctx, iter) {
    // do something with item
}

instead of this:

iter := table.Get("UserID", 1971).Iter()
var item Widget
for iter.Next(ctx, &item) {
    // do something with item
}

dynamo.SeqLEK lets you do for lastEvaluatedKey, item := range dynamo.SeqLEK(...).

My thoughts are that it's kind of useful, but would probably be more useful as a method of a potential Schema[T any] type that stores information about the primary keys, etc (which is something I'd like to add eventually).

Comments or ideas are welcome.

guregu commented 1 month ago

An iterator for BatchGet that returns the table and item would be nice too (see also: #242). Although, you'd have to manually deserialize items if you have multiple types and need to switch on the table name, so it won't change things too much.