1package connections
2
3import (
4 "fmt"
5
6 "github.com/cheekybits/genny/generic"
7
8 "github.com/MichaelMure/git-bug/graphql/models"
9)
10
11// Name define the name of the connection
12type Name generic.Type
13
14// NodeType define the node type handled by this relay connection
15type NodeType generic.Type
16
17// EdgeType define the edge type handled by this relay connection
18type EdgeType generic.Type
19
20// ConnectionType define the connection type handled by this relay connection
21type ConnectionType generic.Type
22
23// NodeTypeEdgeMaker define a function that take a NodeType and an offset and
24// create an Edge.
25type NameEdgeMaker func(value NodeType, offset int) Edge
26
27// NameConMaker define a function that create a ConnectionType
28type NameConMaker func(
29 edges []EdgeType,
30 nodes []NodeType,
31 info models.PageInfo,
32 totalCount int) (ConnectionType, error)
33
34// NameCon will paginate a source according to the input of a relay connection
35func NameCon(source []NodeType, edgeMaker NameEdgeMaker, conMaker NameConMaker, input models.ConnectionInput) (ConnectionType, error) {
36 var nodes []NodeType
37 var edges []EdgeType
38 var cursors []string
39 var pageInfo models.PageInfo
40 var totalCount = len(source)
41
42 emptyCon, _ := conMaker(edges, nodes, pageInfo, 0)
43
44 offset := 0
45
46 if input.After != nil {
47 for i, value := range source {
48 edge := edgeMaker(value, i)
49 if edge.GetCursor() == *input.After {
50 // remove all previous element including the "after" one
51 source = source[i+1:]
52 offset = i + 1
53 pageInfo.HasPreviousPage = true
54 break
55 }
56 }
57 }
58
59 if input.Before != nil {
60 for i, value := range source {
61 edge := edgeMaker(value, i+offset)
62
63 if edge.GetCursor() == *input.Before {
64 // remove all after element including the "before" one
65 pageInfo.HasNextPage = true
66 break
67 }
68
69 edges = append(edges, edge.(EdgeType))
70 cursors = append(cursors, edge.GetCursor())
71 nodes = append(nodes, value)
72 }
73 } else {
74 edges = make([]EdgeType, len(source))
75 cursors = make([]string, len(source))
76 nodes = source
77
78 for i, value := range source {
79 edge := edgeMaker(value, i+offset)
80 edges[i] = edge.(EdgeType)
81 cursors[i] = edge.GetCursor()
82 }
83 }
84
85 if input.First != nil {
86 if *input.First < 0 {
87 return emptyCon, fmt.Errorf("first less than zero")
88 }
89
90 if len(edges) > *input.First {
91 // Slice result to be of length first by removing edges from the end
92 edges = edges[:*input.First]
93 cursors = cursors[:*input.First]
94 nodes = nodes[:*input.First]
95 pageInfo.HasNextPage = true
96 }
97 }
98
99 if input.Last != nil {
100 if *input.Last < 0 {
101 return emptyCon, fmt.Errorf("last less than zero")
102 }
103
104 if len(edges) > *input.Last {
105 // Slice result to be of length last by removing edges from the start
106 edges = edges[len(edges)-*input.Last:]
107 cursors = cursors[len(cursors)-*input.Last:]
108 nodes = nodes[len(nodes)-*input.Last:]
109 pageInfo.HasPreviousPage = true
110 }
111 }
112
113 // Fill up pageInfo cursors
114 if len(cursors) > 0 {
115 pageInfo.StartCursor = cursors[0]
116 pageInfo.EndCursor = cursors[len(cursors)-1]
117 }
118
119 return conMaker(edges, nodes, pageInfo, totalCount)
120}