Skip to content

Commit

Permalink
clean this a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Dec 16, 2024
1 parent 4252c1f commit 9fd42c9
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (q *Query) One(ctx context.Context, out interface{}) error {
}

// If not, try a Query.
iter := q.Iter().(*queryIter)
iter := q.newIter(unmarshalItem)
var item Item
ok := iter.Next(ctx, &item)
if err := iter.Err(); err != nil {
Expand Down Expand Up @@ -300,6 +300,14 @@ func (q *Query) Count(ctx context.Context) (int, error) {
return count, nil
}

func (q *Query) newIter(unmarshal unmarshalFunc) *queryIter {
return &queryIter{
query: q,
unmarshal: unmarshal,
err: q.err,
}
}

// queryIter is the iterator for Query operations
type queryIter struct {
query *Query
Expand Down Expand Up @@ -451,11 +459,7 @@ func (itr *queryIter) LastEvaluatedKey(ctx context.Context) (PagingKey, error) {

// All executes this request and unmarshals all results to out, which must be a pointer to a slice.
func (q *Query) All(ctx context.Context, out interface{}) error {
iter := &queryIter{
query: q,
unmarshal: unmarshalAppendTo(out),
err: q.err,
}
iter := q.newIter(unmarshalAppendTo(out))
for iter.Next(ctx, out) {
}
return iter.Err()
Expand All @@ -464,11 +468,7 @@ func (q *Query) All(ctx context.Context, out interface{}) error {
// AllWithLastEvaluatedKey executes this request and unmarshals all results to out, which must be a pointer to a slice.
// This returns a PagingKey you can use with StartFrom to split up results.
func (q *Query) AllWithLastEvaluatedKey(ctx context.Context, out interface{}) (PagingKey, error) {
iter := &queryIter{
query: q,
unmarshal: unmarshalAppendTo(out),
err: q.err,
}
iter := q.newIter(unmarshalAppendTo(out))
for iter.Next(ctx, out) {
}
lek, err := iter.LastEvaluatedKey(ctx)
Expand All @@ -477,12 +477,7 @@ func (q *Query) AllWithLastEvaluatedKey(ctx context.Context, out interface{}) (P

// Iter returns a results iterator for this request.
func (q *Query) Iter() PagingIter {
iter := &queryIter{
query: q,
unmarshal: unmarshalItem,
err: q.err,
}
return iter
return q.newIter(unmarshalItem)
}

// can we use the get item API?
Expand Down

0 comments on commit 9fd42c9

Please sign in to comment.