@@ -71,25 +71,29 @@ type CursorResponse struct {
7171 postBatchResumeToken bsoncore.Document
7272}
7373
74- // NewCursorResponse constructs a cursor response from the given response and
75- // server. If the provided database response does not contain a cursor, it
76- // returns ErrNoCursor.
77- //
78- // NewCursorResponse can be used within the ProcessResponse method for an operation.
79- func NewCursorResponse (info ResponseInfo ) (CursorResponse , error ) {
80- response := info .ServerResponse
74+ // ExtractCursorDocument retrieves cursor document from a database response. If the
75+ // provided response does not contain a cursor, it returns ErrNoCursor.
76+ func ExtractCursorDocument (response bsoncore.Document ) (bsoncore.Document , error ) {
8177 cur , err := response .LookupErr ("cursor" )
8278 if errors .Is (err , bsoncore .ErrElementNotFound ) {
83- return CursorResponse {} , ErrNoCursor
79+ return nil , ErrNoCursor
8480 }
8581 if err != nil {
86- return CursorResponse {} , fmt .Errorf ("error getting cursor from database response: %w" , err )
82+ return nil , fmt .Errorf ("error getting cursor from database response: %w" , err )
8783 }
8884 curDoc , ok := cur .DocumentOK ()
8985 if ! ok {
90- return CursorResponse {} , fmt .Errorf ("cursor should be an embedded document but is BSON type %s" , cur .Type )
86+ return nil , fmt .Errorf ("cursor should be an embedded document but is BSON type %s" , cur .Type )
9187 }
92- elems , err := curDoc .Elements ()
88+ return curDoc , nil
89+ }
90+
91+ // NewCursorResponse constructs a cursor response from the given cursor document
92+ // extracted from a database response.
93+ //
94+ // NewCursorResponse can be used within the ProcessResponse method for an operation.
95+ func NewCursorResponse (response bsoncore.Document , info ResponseInfo ) (CursorResponse , error ) {
96+ elems , err := response .Elements ()
9397 if err != nil {
9498 return CursorResponse {}, fmt .Errorf ("error getting elements from cursor: %w" , err )
9599 }
@@ -115,15 +119,17 @@ func NewCursorResponse(info ResponseInfo) (CursorResponse, error) {
115119 curresp .Database = database
116120 curresp .Collection = collection
117121 case "id" :
118- curresp . ID , ok = elem .Value ().Int64OK ()
122+ id , ok : = elem .Value ().Int64OK ()
119123 if ! ok {
120124 return CursorResponse {}, fmt .Errorf ("id should be an int64 but it is a BSON %s" , elem .Value ().Type )
121125 }
126+ curresp .ID = id
122127 case "postBatchResumeToken" :
123- curresp . postBatchResumeToken , ok = elem .Value ().DocumentOK ()
128+ token , ok : = elem .Value ().DocumentOK ()
124129 if ! ok {
125130 return CursorResponse {}, fmt .Errorf ("post batch resume token should be a document but it is a BSON %s" , elem .Value ().Type )
126131 }
132+ curresp .postBatchResumeToken = token
127133 }
128134 }
129135
@@ -393,8 +399,8 @@ func (bc *BatchCursor) getMore(ctx context.Context) {
393399 },
394400 Database : bc .database ,
395401 Deployment : bc .getOperationDeployment (),
396- ProcessResponseFn : func (_ context.Context , info ResponseInfo ) error {
397- response := info .ServerResponse
402+ ProcessResponseFn : func (_ context.Context , response bsoncore. Document , info ResponseInfo ) error {
403+ // response := info.ServerResponse
398404 id , ok := response .Lookup ("cursor" , "id" ).Int64OK ()
399405 if ! ok {
400406 return fmt .Errorf ("cursor.id should be an int64 but is a BSON %s" , response .Lookup ("cursor" , "id" ).Type )
0 commit comments