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