1//go:generate genny -in=pagers_template.go -out=pager_bug.go gen "NodeType=bug.Snapshot EdgeType=BugEdge"
2//go:generate genny -in=pagers_template.go -out=pager_operation.go gen "NodeType=bug.Operation EdgeType=OperationEdge"
3//go:generate genny -in=pagers_template.go -out=pager_comment.go gen "NodeType=bug.Comment EdgeType=CommentEdge"
4
5package resolvers
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}
40
41func (e OperationEdge) GetCursor() string {
42 return e.Cursor
43}
44
45func (e BugEdge) GetCursor() string {
46 return e.Cursor
47}
48
49func (e CommentEdge) GetCursor() string {
50 return e.Cursor
51}