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