gen_comment.go

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