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/git-bug/git-bug/api/graphql/models"
11 "github.com/git-bug/git-bug/entity"
12)
13
14// LazyIdentityEdgeMaker define a function that take a entity.Id and an offset and
15// create an Edge.
16type LazyIdentityEdgeMaker func(value entity.Id, offset int) Edge
17
18// LazyIdentityConMaker define a function that create a models.IdentityConnection
19type LazyIdentityConMaker func(
20 edges []*LazyIdentityEdge,
21 nodes []entity.Id,
22 info *models.PageInfo,
23 totalCount int) (*models.IdentityConnection, error)
24
25// LazyIdentityCon will paginate a source according to the input of a relay connection
26func LazyIdentityCon(source []entity.Id, edgeMaker LazyIdentityEdgeMaker, conMaker LazyIdentityConMaker, input models.ConnectionInput) (*models.IdentityConnection, error) {
27 var nodes []entity.Id
28 var edges []*LazyIdentityEdge
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 e := edge.(LazyIdentityEdge)
61 edges = append(edges, &e)
62 cursors = append(cursors, edge.GetCursor())
63 nodes = append(nodes, value)
64 }
65 } else {
66 edges = make([]*LazyIdentityEdge, len(source))
67 cursors = make([]string, len(source))
68 nodes = source
69
70 for i, value := range source {
71 edge := edgeMaker(value, i+offset)
72 e := edge.(LazyIdentityEdge)
73 edges[i] = &e
74 cursors[i] = edge.GetCursor()
75 }
76 }
77
78 if input.First != nil {
79 if *input.First < 0 {
80 return emptyCon, fmt.Errorf("first less than zero")
81 }
82
83 if len(edges) > *input.First {
84 // Slice result to be of length first by removing edges from the end
85 edges = edges[:*input.First]
86 cursors = cursors[:*input.First]
87 nodes = nodes[:*input.First]
88 pageInfo.HasNextPage = true
89 }
90 }
91
92 if input.Last != nil {
93 if *input.Last < 0 {
94 return emptyCon, fmt.Errorf("last less than zero")
95 }
96
97 if len(edges) > *input.Last {
98 // Slice result to be of length last by removing edges from the start
99 edges = edges[len(edges)-*input.Last:]
100 cursors = cursors[len(cursors)-*input.Last:]
101 nodes = nodes[len(nodes)-*input.Last:]
102 pageInfo.HasPreviousPage = true
103 }
104 }
105
106 // Fill up pageInfo cursors
107 if len(cursors) > 0 {
108 pageInfo.StartCursor = cursors[0]
109 pageInfo.EndCursor = cursors[len(cursors)-1]
110 }
111
112 return conMaker(edges, nodes, pageInfo, totalCount)
113}