1//go:generate genny -in=connection_template.go -out=gen_bug.go gen "NodeType=string EdgeType=LazyBugEdge ConnectionType=models.BugConnection"
2//go:generate genny -in=connection_template.go -out=gen_operation.go gen "NodeType=bug.Operation EdgeType=models.OperationEdge ConnectionType=models.OperationConnection"
3//go:generate genny -in=connection_template.go -out=gen_comment.go gen "NodeType=bug.Comment EdgeType=models.CommentEdge ConnectionType=models.CommentConnection"
4
5package connections
6
7import (
8 "encoding/base64"
9 "fmt"
10 "strconv"
11 "strings"
12)
13
14const cursorPrefix = "cursor:"
15
16type Edge interface {
17 GetCursor() string
18}
19
20// Creates the cursor string from an offset
21func OffsetToCursor(offset int) string {
22 str := fmt.Sprintf("%v%v", cursorPrefix, offset)
23 return base64.StdEncoding.EncodeToString([]byte(str))
24}
25
26// Re-derives the offset from the cursor string.
27func CursorToOffset(cursor string) (int, error) {
28 str := ""
29 b, err := base64.StdEncoding.DecodeString(cursor)
30 if err == nil {
31 str = string(b)
32 }
33 str = strings.Replace(str, cursorPrefix, "", -1)
34 offset, err := strconv.Atoi(str)
35 if err != nil {
36 return 0, fmt.Errorf("Invalid cursor")
37 }
38 return offset, nil
39}