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