Skip to content

Commit

Permalink
add ItemTableIter
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Aug 22, 2024
1 parent 46a3891 commit 38c1867
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions batchget.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func (bg *BatchGet) Iter() Iter {

// IterWithTable is like [BatchGet.Iter], but will update the value pointed by tablePtr after each iteration.
// This can be useful when getting from multiple tables to determine which table the latest item came from.
// See: [BatchGet.ItemTableIter] for a nicer way to do this.
//
// For example, you can utilize this iterator to read the results into different structs.
//
Expand Down
83 changes: 83 additions & 0 deletions batchget_go123.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//go:build go1.23

package dynamo

import (
"context"
"iter"
)

type ItemTableIter[V any] interface {
// Items is a sequence of item and table names.
// This is a single use iterator.
// Be sure to check for errors with Err afterwards.
Items(context.Context) iter.Seq2[V, string]
// Err must be checked after iterating.
Err() error
}

// ItemTableIter returns an iterator of (raw item, table name).
// To specify a type, use [BatchGetIter] instead.
//
// For example, you can utilize this iterator to read the results into different structs.
//
// widgetBatch := widgetsTable.Batch("UserID").Get(dynamo.Keys{userID})
// sprocketBatch := sprocketsTable.Batch("UserID").Get(dynamo.Keys{userID})
//
// iter := widgetBatch.Merge(sprocketBatch).ItemTableIter(&table)
//
// // now we will use the table iterator to unmarshal the values into their respective types
// var s sprocket
// var w widget
// for raw, table := range iter.Items {
// if table == "Widgets" {
// err := dynamo.UnmarshalItem(raw, &w)
// if err != nil {
// fmt.Println(err)
// }
// } else if table == "Sprockets" {
// err := dynamo.UnmarshalItem(raw, &s)
// if err != nil {
// fmt.Println(err)
// }
// } else {
// fmt.Printf("Unexpected Table: %s\n", table)
// }
// }
//
// if iter.Err() != nil {
// fmt.Println(iter.Err())
// }
func (bg *BatchGet) ItemTableIter() ItemTableIter[Item] {
return newBgIter2[Item](bg)
}

type bgIter2[V any] struct {
Iter
table string
}

func newBgIter2[V any](bg *BatchGet) *bgIter2[V] {
iter := new(bgIter2[V])
iter.Iter = bg.IterWithTable(&iter.table)
return iter
}

// Items is a sequence of item and table names.
// This is a single use iterator.
// Be sure to check for errors with Err afterwards.
func (iter *bgIter2[V]) Items(ctx context.Context) iter.Seq2[V, string] {
return func(yield func(V, string) bool) {
item := new(V)
for iter.Next(ctx, item) {
if !yield(*item, iter.table) {
break
}
item = new(V)
}
}
}

func BatchGetIter[V any](bg *BatchGet) ItemTableIter[V] {
return newBgIter2[V](bg)
}

0 comments on commit 38c1867

Please sign in to comment.