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