Skip to content

Commit

Permalink
Added an example to the IterWithTable godocs (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamccall authored Jul 20, 2024
1 parent f21d832 commit eed9493
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions batchget.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,38 @@ 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.
//
// 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})
//
// var table string
// iter := widgetBatch.Merge(sprocketBatch).IterWithTable(&table)
//
// // now we will use the table iterator to unmarshal the values into their respective types
// var s sprocket
// var w widget
// var tmp map[string]types.AttributeValue
// for iter.Next(ctx, &tmp) {
// if table == "Widgets" {
// err := dynamo.UnmarshalItem(tmp, &w)
// if err != nil {
// fmt.Println(err)
// }
// } else if table == "Sprockets" {
// err := dynamo.UnmarshalItem(tmp, &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) IterWithTable(tablePtr *string) Iter {
return newBGIter(bg, unmarshalItem, tablePtr, bg.err)
}
Expand Down

0 comments on commit eed9493

Please sign in to comment.