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