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 var totalCount = len(source)
32
33 emptyCon, _ := conMaker(edges, nodes, pageInfo, 0)
34
35 offset := 0
36
37 if input.After != nil {
38 for i, value := range source {
39 edge := edgeMaker(value, i)
40 if edge.GetCursor() == *input.After {
41 // remove all previous element including the "after" one
42 source = source[i+1:]
43 offset = i + 1
44 pageInfo.HasPreviousPage = true
45 break
46 }
47 }
48 }
49
50 if input.Before != nil {
51 for i, value := range source {
52 edge := edgeMaker(value, i+offset)
53
54 if edge.GetCursor() == *input.Before {
55 // remove all after element including the "before" one
56 pageInfo.HasNextPage = true
57 break
58 }
59
60 edges = append(edges, edge.(models.OperationEdge))
61 cursors = append(cursors, edge.GetCursor())
62 nodes = append(nodes, value)
63 }
64 } else {
65 edges = make([]models.OperationEdge, len(source))
66 cursors = make([]string, len(source))
67 nodes = source
68
69 for i, value := range source {
70 edge := edgeMaker(value, i+offset)
71 edges[i] = edge.(models.OperationEdge)
72 cursors[i] = edge.GetCursor()
73 }
74 }
75
76 if input.First != nil {
77 if *input.First < 0 {
78 return emptyCon, fmt.Errorf("first less than zero")
79 }
80
81 if len(edges) > *input.First {
82 // Slice result to be of length first by removing edges from the end
83 edges = edges[:*input.First]
84 cursors = cursors[:*input.First]
85 nodes = nodes[:*input.First]
86 pageInfo.HasNextPage = true
87 }
88 }
89
90 if input.Last != nil {
91 if *input.Last < 0 {
92 return emptyCon, fmt.Errorf("last less than zero")
93 }
94
95 if len(edges) > *input.Last {
96 // Slice result to be of length last by removing edges from the start
97 edges = edges[len(edges)-*input.Last:]
98 cursors = cursors[len(cursors)-*input.Last:]
99 nodes = nodes[len(nodes)-*input.Last:]
100 pageInfo.HasPreviousPage = true
101 }
102 }
103
104 // Fill up pageInfo cursors
105 if len(cursors) > 0 {
106 pageInfo.StartCursor = cursors[0]
107 pageInfo.EndCursor = cursors[len(cursors)-1]
108 }
109
110 return conMaker(edges, nodes, pageInfo, totalCount)
111}