1// go:generate genny -in=connection_template.go -out=gen_lazy_bug.go gen "Name=LazyBug NodeType=entity.Id EdgeType=LazyBugEdge ConnectionType=models.BugConnection"
2// go:generate genny -in=connection_template.go -out=gen_lazy_identity.go gen "Name=LazyIdentity NodeType=entity.Id EdgeType=LazyIdentityEdge ConnectionType=models.IdentityConnection"
3// go:generate genny -in=connection_template.go -out=gen_identity.go gen "Name=Identity NodeType=models.IdentityWrapper EdgeType=models.IdentityEdge ConnectionType=models.IdentityConnection"
4// go:generate genny -in=connection_template.go -out=gen_operation.go gen "Name=Operation NodeType=dag.Operation EdgeType=models.OperationEdge ConnectionType=models.OperationConnection"
5// go:generate genny -in=connection_template.go -out=gen_comment.go gen "Name=Comment NodeType=bug.Comment EdgeType=models.CommentEdge ConnectionType=models.CommentConnection"
6// go:generate genny -in=connection_template.go -out=gen_timeline.go gen "Name=TimelineItem NodeType=bug.TimelineItem EdgeType=models.TimelineItemEdge ConnectionType=models.TimelineItemConnection"
7// go:generate genny -in=connection_template.go -out=gen_label.go gen "Name=Label NodeType=bug.Label EdgeType=models.LabelEdge ConnectionType=models.LabelConnection"
8
9// Package connections implement a generic GraphQL relay connection
10package connections
11
12import (
13 "encoding/base64"
14 "fmt"
15 "strconv"
16 "strings"
17)
18
19const cursorPrefix = "cursor:"
20
21// OffsetToCursor create the cursor string from an offset
22func OffsetToCursor(offset int) string {
23 str := fmt.Sprintf("%v%v", cursorPrefix, offset)
24 return base64.StdEncoding.EncodeToString([]byte(str))
25}
26
27// CursorToOffset re-derives the offset from the cursor string.
28func CursorToOffset(cursor string) (int, error) {
29 str := ""
30 b, err := base64.StdEncoding.DecodeString(cursor)
31 if err == nil {
32 str = string(b)
33 }
34 str = strings.Replace(str, cursorPrefix, "", -1)
35 offset, err := strconv.Atoi(str)
36 if err != nil {
37 return 0, fmt.Errorf("Invalid cursor")
38 }
39 return offset, nil
40}