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// StringEdger define a function that take a string and an offset and
14// create an Edge.
15type StringEdger 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, edger StringEdger, conMaker StringConMaker, input models.ConnectionInput) (models.BugConnection, error) {
26 var nodes []string
27 var edges []LazyBugEdge
28 var pageInfo models.PageInfo
29
30 emptyCon, _ := conMaker(edges, nodes, pageInfo, 0)
31
32 offset := 0
33
34 if input.After != nil {
35 for i, value := range source {
36 edge := edger(value, i)
37 if edge.GetCursor() == *input.After {
38 // remove all previous element including the "after" one
39 source = source[i+1:]
40 offset = i + 1
41 break
42 }
43 }
44 }
45
46 if input.Before != nil {
47 for i, value := range source {
48 edge := edger(value, i+offset)
49
50 if edge.GetCursor() == *input.Before {
51 // remove all after element including the "before" one
52 break
53 }
54
55 edges = append(edges, edge.(LazyBugEdge))
56 nodes = append(nodes, value)
57 }
58 } else {
59 edges = make([]LazyBugEdge, len(source))
60 nodes = source
61
62 for i, value := range source {
63 edges[i] = edger(value, i+offset).(LazyBugEdge)
64 }
65 }
66
67 if input.First != nil {
68 if *input.First < 0 {
69 return emptyCon, fmt.Errorf("first less than zero")
70 }
71
72 if len(edges) > *input.First {
73 // Slice result to be of length first by removing edges from the end
74 edges = edges[:*input.First]
75 nodes = nodes[:*input.First]
76 pageInfo.HasNextPage = true
77 }
78 }
79
80 if input.Last != nil {
81 if *input.Last < 0 {
82 return emptyCon, fmt.Errorf("last less than zero")
83 }
84
85 if len(edges) > *input.Last {
86 // Slice result to be of length last by removing edges from the start
87 edges = edges[len(edges)-*input.Last:]
88 nodes = nodes[len(nodes)-*input.Last:]
89 pageInfo.HasPreviousPage = true
90 }
91 }
92
93 return conMaker(edges, nodes, pageInfo, len(source))
94}