-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcursor.go
45 lines (36 loc) · 1.5 KB
/
cursor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// This file contains the CursorCodec interface and a default implementation.
package minquery
import (
"encoding/base64"
"github.com/globalsign/mgo/bson"
)
// CursorCodec represents a symmetric pair of functions that can be used to
// convert cursor data of type bson.D to a string and vice versa.
type CursorCodec interface {
// CreateCursor returns a cursor string from the specified fields.
CreateCursor(cursorData bson.D) (string, error)
// ParseCursor parses the cursor string and returns the cursor data.
ParseCursor(c string) (cursorData bson.D, err error)
}
// cursorCodec is a default implementation of CursorCodec which produces
// web-safe cursor strings by first marshaling the cursor data using
// bson.Marshal(), then using base64.RawURLEncoding.
type cursorCodec struct{}
// CreateCursor implements CursorCodec.CreateCursor().
// The returned cursor string is web-safe, and so it's safe to include
// in URL queries without escaping.
func (cursorCodec) CreateCursor(cursorData bson.D) (string, error) {
// bson.Marshal() never returns error, so I skip a check and early return
// (but I do return the error if it would ever happen)
data, err := bson.Marshal(cursorData)
return base64.RawURLEncoding.EncodeToString(data), err
}
// ParseCursor implements CursorCodec.ParseCursor().
func (cursorCodec) ParseCursor(c string) (cursorData bson.D, err error) {
var data []byte
if data, err = base64.RawURLEncoding.DecodeString(c); err != nil {
return
}
err = bson.Unmarshal(data, &cursorData)
return
}