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