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// Edge define the contract for an edge in a relay connection
22type Edge interface {
23 GetCursor() string
24}
25
26// OffsetToCursor create the cursor string from an offset
27func OffsetToCursor(offset int) string {
28 str := fmt.Sprintf("%v%v", cursorPrefix, offset)
29 return base64.StdEncoding.EncodeToString([]byte(str))
30}
31
32// CursorToOffset re-derives the offset from the cursor string.
33func CursorToOffset(cursor string) (int, error) {
34 str := ""
35 b, err := base64.StdEncoding.DecodeString(cursor)
36 if err == nil {
37 str = string(b)
38 }
39 str = strings.Replace(str, cursorPrefix, "", -1)
40 offset, err := strconv.Atoi(str)
41 if err != nil {
42 return 0, fmt.Errorf("Invalid cursor")
43 }
44 return offset, nil
45}