Skip to content

Commit 4697c79

Browse files
authored
Merge pull request #9 from tstromberg/main
improve mock accuracy
2 parents 525db1b + ffa7fb2 commit 4697c79

File tree

4 files changed

+789
-157
lines changed

4 files changed

+789
-157
lines changed

pkg/datastore/iterator.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,10 @@ func (it *Iterator) fetch() error {
145145
it.index = 0
146146

147147
// Check if there are more results
148+
// MORE_RESULTS_AFTER_LIMIT means we hit the query limit - don't auto-fetch more
149+
// NOT_FINISHED and MORE_RESULTS_AFTER_CURSOR mean we should continue fetching
148150
moreResults := result.Batch.MoreResults
149-
it.fetchNext = moreResults == "NOT_FINISHED" || moreResults == "MORE_RESULTS_AFTER_LIMIT" || moreResults == "MORE_RESULTS_AFTER_CURSOR"
151+
it.fetchNext = moreResults == "NOT_FINISHED" || moreResults == "MORE_RESULTS_AFTER_CURSOR"
150152

151153
if result.Batch.EndCursor != "" {
152154
it.cursor = Cursor(result.Batch.EndCursor)

pkg/datastore/query.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ func (c *Client) AllKeys(ctx context.Context, q *Query) ([]*Key, error) {
384384
}
385385

386386
// GetAll retrieves all entities matching the query and stores them in dst.
387-
// dst must be a pointer to a slice of structs.
387+
// dst must be a pointer to a slice of structs, or nil for KeysOnly queries.
388388
// Returns the keys of the retrieved entities and any error.
389389
// This matches the API of cloud.google.com/go/datastore.
390390
func (c *Client) GetAll(ctx context.Context, query *Query, dst any) ([]*Key, error) {
@@ -431,6 +431,21 @@ func (c *Client) GetAll(ctx context.Context, query *Query, dst any) ([]*Key, err
431431
return nil, fmt.Errorf("failed to parse response: %w", err)
432432
}
433433

434+
// For KeysOnly queries, dst can be nil - just return keys
435+
if query.keysOnly && dst == nil {
436+
keys := make([]*Key, 0, len(result.Batch.EntityResults))
437+
for _, er := range result.Batch.EntityResults {
438+
key, err := keyFromJSON(er.Entity["key"])
439+
if err != nil {
440+
c.logger.ErrorContext(ctx, "failed to parse key from response", "error", err)
441+
return nil, err
442+
}
443+
keys = append(keys, key)
444+
}
445+
c.logger.DebugContext(ctx, "keys-only query completed successfully", "kind", query.kind, "keys_found", len(keys))
446+
return keys, nil
447+
}
448+
434449
// Verify dst is a pointer to slice
435450
v := reflect.ValueOf(dst)
436451
if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Slice {

0 commit comments

Comments
 (0)