diff options
Diffstat (limited to 'api/graphql/connections')
-rw-r--r-- | api/graphql/connections/connection_template.go | 122 | ||||
-rw-r--r-- | api/graphql/connections/connections.go | 45 | ||||
-rw-r--r-- | api/graphql/connections/edges.go | 25 | ||||
-rw-r--r-- | api/graphql/connections/gen_comment.go | 113 | ||||
-rw-r--r-- | api/graphql/connections/gen_identity.go | 112 | ||||
-rw-r--r-- | api/graphql/connections/gen_label.go | 113 | ||||
-rw-r--r-- | api/graphql/connections/gen_lazy_bug.go | 113 | ||||
-rw-r--r-- | api/graphql/connections/gen_lazy_identity.go | 113 | ||||
-rw-r--r-- | api/graphql/connections/gen_operation.go | 113 | ||||
-rw-r--r-- | api/graphql/connections/gen_timeline.go | 113 |
10 files changed, 982 insertions, 0 deletions
diff --git a/api/graphql/connections/connection_template.go b/api/graphql/connections/connection_template.go new file mode 100644 index 00000000..935a0a77 --- /dev/null +++ b/api/graphql/connections/connection_template.go @@ -0,0 +1,122 @@ +package connections + +import ( + "fmt" + + "github.com/cheekybits/genny/generic" + + "github.com/MichaelMure/git-bug/api/graphql/models" +) + +// Name define the name of the connection +type Name generic.Type + +// NodeType define the node type handled by this relay connection +type NodeType generic.Type + +// EdgeType define the edge type handled by this relay connection +type EdgeType generic.Type + +// ConnectionType define the connection type handled by this relay connection +type ConnectionType generic.Type + +// NodeTypeEdgeMaker define a function that take a NodeType and an offset and +// create an Edge. +type NameEdgeMaker func(value NodeType, offset int) Edge + +// NameConMaker define a function that create a ConnectionType +type NameConMaker func( + edges []*EdgeType, + nodes []NodeType, + info *models.PageInfo, + totalCount int) (*ConnectionType, error) + +// NameCon will paginate a source according to the input of a relay connection +func NameCon(source []NodeType, edgeMaker NameEdgeMaker, conMaker NameConMaker, input models.ConnectionInput) (*ConnectionType, error) { + var nodes []NodeType + var edges []*EdgeType + var cursors []string + var pageInfo = &models.PageInfo{} + var totalCount = len(source) + + emptyCon, _ := conMaker(edges, nodes, pageInfo, 0) + + offset := 0 + + if input.After != nil { + for i, value := range source { + edge := edgeMaker(value, i) + if edge.GetCursor() == *input.After { + // remove all previous element including the "after" one + source = source[i+1:] + offset = i + 1 + pageInfo.HasPreviousPage = true + break + } + } + } + + if input.Before != nil { + for i, value := range source { + edge := edgeMaker(value, i+offset) + + if edge.GetCursor() == *input.Before { + // remove all after element including the "before" one + pageInfo.HasNextPage = true + break + } + + e := edge.(EdgeType) + edges = append(edges, &e) + cursors = append(cursors, edge.GetCursor()) + nodes = append(nodes, value) + } + } else { + edges = make([]*EdgeType, len(source)) + cursors = make([]string, len(source)) + nodes = source + + for i, value := range source { + edge := edgeMaker(value, i+offset) + e := edge.(EdgeType) + edges[i] = &e + cursors[i] = edge.GetCursor() + } + } + + if input.First != nil { + if *input.First < 0 { + return emptyCon, fmt.Errorf("first less than zero") + } + + if len(edges) > *input.First { + // Slice result to be of length first by removing edges from the end + edges = edges[:*input.First] + cursors = cursors[:*input.First] + nodes = nodes[:*input.First] + pageInfo.HasNextPage = true + } + } + + if input.Last != nil { + if *input.Last < 0 { + return emptyCon, fmt.Errorf("last less than zero") + } + + if len(edges) > *input.Last { + // Slice result to be of length last by removing edges from the start + edges = edges[len(edges)-*input.Last:] + cursors = cursors[len(cursors)-*input.Last:] + nodes = nodes[len(nodes)-*input.Last:] + pageInfo.HasPreviousPage = true + } + } + + // Fill up pageInfo cursors + if len(cursors) > 0 { + pageInfo.StartCursor = cursors[0] + pageInfo.EndCursor = cursors[len(cursors)-1] + } + + return conMaker(edges, nodes, pageInfo, totalCount) +} diff --git a/api/graphql/connections/connections.go b/api/graphql/connections/connections.go new file mode 100644 index 00000000..0083f8b2 --- /dev/null +++ b/api/graphql/connections/connections.go @@ -0,0 +1,45 @@ +//go:generate genny -in=connection_template.go -out=gen_lazy_bug.go gen "Name=LazyBug NodeType=entity.Id EdgeType=LazyBugEdge ConnectionType=models.BugConnection" +//go:generate genny -in=connection_template.go -out=gen_lazy_identity.go gen "Name=LazyIdentity NodeType=entity.Id EdgeType=LazyIdentityEdge ConnectionType=models.IdentityConnection" +//go:generate genny -in=connection_template.go -out=gen_identity.go gen "Name=Identity NodeType=models.IdentityWrapper EdgeType=models.IdentityEdge ConnectionType=models.IdentityConnection" +//go:generate genny -in=connection_template.go -out=gen_operation.go gen "Name=Operation NodeType=bug.Operation EdgeType=models.OperationEdge ConnectionType=models.OperationConnection" +//go:generate genny -in=connection_template.go -out=gen_comment.go gen "Name=Comment NodeType=bug.Comment EdgeType=models.CommentEdge ConnectionType=models.CommentConnection" +//go:generate genny -in=connection_template.go -out=gen_timeline.go gen "Name=TimelineItem NodeType=bug.TimelineItem EdgeType=models.TimelineItemEdge ConnectionType=models.TimelineItemConnection" +//go:generate genny -in=connection_template.go -out=gen_label.go gen "Name=Label NodeType=bug.Label EdgeType=models.LabelEdge ConnectionType=models.LabelConnection" + +// Package connections implement a generic GraphQL relay connection +package connections + +import ( + "encoding/base64" + "fmt" + "strconv" + "strings" +) + +const cursorPrefix = "cursor:" + +// Edge define the contract for an edge in a relay connection +type Edge interface { + GetCursor() string +} + +// OffsetToCursor create the cursor string from an offset +func OffsetToCursor(offset int) string { + str := fmt.Sprintf("%v%v", cursorPrefix, offset) + return base64.StdEncoding.EncodeToString([]byte(str)) +} + +// CursorToOffset re-derives the offset from the cursor string. +func CursorToOffset(cursor string) (int, error) { + str := "" + b, err := base64.StdEncoding.DecodeString(cursor) + if err == nil { + str = string(b) + } + str = strings.Replace(str, cursorPrefix, "", -1) + offset, err := strconv.Atoi(str) + if err != nil { + return 0, fmt.Errorf("Invalid cursor") + } + return offset, nil +} diff --git a/api/graphql/connections/edges.go b/api/graphql/connections/edges.go new file mode 100644 index 00000000..4e37fcd9 --- /dev/null +++ b/api/graphql/connections/edges.go @@ -0,0 +1,25 @@ +package connections + +import "github.com/MichaelMure/git-bug/entity" + +// LazyBugEdge is a special relay edge used to implement a lazy loading connection +type LazyBugEdge struct { + Id entity.Id + Cursor string +} + +// GetCursor return the cursor of a LazyBugEdge +func (lbe LazyBugEdge) GetCursor() string { + return lbe.Cursor +} + +// LazyIdentityEdge is a special relay edge used to implement a lazy loading connection +type LazyIdentityEdge struct { + Id entity.Id + Cursor string +} + +// GetCursor return the cursor of a LazyIdentityEdge +func (lbe LazyIdentityEdge) GetCursor() string { + return lbe.Cursor +} diff --git a/api/graphql/connections/gen_comment.go b/api/graphql/connections/gen_comment.go new file mode 100644 index 00000000..bae74030 --- /dev/null +++ b/api/graphql/connections/gen_comment.go @@ -0,0 +1,113 @@ +// This file was automatically generated by genny. +// Any changes will be lost if this file is regenerated. +// see https://github.com/cheekybits/genny + +package connections + +import ( + "fmt" + + "github.com/MichaelMure/git-bug/api/graphql/models" + "github.com/MichaelMure/git-bug/bug" +) + +// BugCommentEdgeMaker define a function that take a bug.Comment and an offset and +// create an Edge. +type CommentEdgeMaker func(value bug.Comment, offset int) Edge + +// CommentConMaker define a function that create a models.CommentConnection +type CommentConMaker func( + edges []*models.CommentEdge, + nodes []bug.Comment, + info *models.PageInfo, + totalCount int) (*models.CommentConnection, error) + +// CommentCon will paginate a source according to the input of a relay connection +func CommentCon(source []bug.Comment, edgeMaker CommentEdgeMaker, conMaker CommentConMaker, input models.ConnectionInput) (*models.CommentConnection, error) { + var nodes []bug.Comment + var edges []*models.CommentEdge + var cursors []string + var pageInfo = &models.PageInfo{} + var totalCount = len(source) + + emptyCon, _ := conMaker(edges, nodes, pageInfo, 0) + + offset := 0 + + if input.After != nil { + for i, value := range source { + edge := edgeMaker(value, i) + if edge.GetCursor() == *input.After { + // remove all previous element including the "after" one + source = source[i+1:] + offset = i + 1 + pageInfo.HasPreviousPage = true + break + } + } + } + + if input.Before != nil { + for i, value := range source { + edge := edgeMaker(value, i+offset) + + if edge.GetCursor() == *input.Before { + // remove all after element including the "before" one + pageInfo.HasNextPage = true + break + } + + e := edge.(models.CommentEdge) + edges = append(edges, &e) + cursors = append(cursors, edge.GetCursor()) + nodes = append(nodes, value) + } + } else { + edges = make([]*models.CommentEdge, len(source)) + cursors = make([]string, len(source)) + nodes = source + + for i, value := range source { + edge := edgeMaker(value, i+offset) + e := edge.(models.CommentEdge) + edges[i] = &e + cursors[i] = edge.GetCursor() + } + } + + if input.First != nil { + if *input.First < 0 { + return emptyCon, fmt.Errorf("first less than zero") + } + + if len(edges) > *input.First { + // Slice result to be of length first by removing edges from the end + edges = edges[:*input.First] + cursors = cursors[:*input.First] + nodes = nodes[:*input.First] + pageInfo.HasNextPage = true + } + } + + if input.Last != nil { + if *input.Last < 0 { + return emptyCon, fmt.Errorf("last less than zero") + } + + if len(edges) > *input.Last { + // Slice result to be of length last by removing edges from the start + edges = edges[len(edges)-*input.Last:] + cursors = cursors[len(cursors)-*input.Last:] + nodes = nodes[len(nodes)-*input.Last:] + pageInfo.HasPreviousPage = true + } + } + + // Fill up pageInfo cursors + if len(cursors) > 0 { + pageInfo.StartCursor = cursors[0] + pageInfo.EndCursor = cursors[len(cursors)-1] + } + + return conMaker(edges, nodes, pageInfo, totalCount) +} diff --git a/api/graphql/connections/gen_identity.go b/api/graphql/connections/gen_identity.go new file mode 100644 index 00000000..2138b17e --- /dev/null +++ b/api/graphql/connections/gen_identity.go @@ -0,0 +1,112 @@ +// This file was automatically generated by genny. +// Any changes will be lost if this file is regenerated. +// see https://github.com/cheekybits/genny + +package connections + +import ( + "fmt" + + "github.com/MichaelMure/git-bug/api/graphql/models" +) + +// ModelsIdentityWrapperEdgeMaker define a function that take a models.IdentityWrapper and an offset and +// create an Edge. +type IdentityEdgeMaker func(value models.IdentityWrapper, offset int) Edge + +// IdentityConMaker define a function that create a models.IdentityConnection +type IdentityConMaker func( + edges []*models.IdentityEdge, + nodes []models.IdentityWrapper, + info *models.PageInfo, + totalCount int) (*models.IdentityConnection, error) + +// IdentityCon will paginate a source according to the input of a relay connection +func IdentityCon(source []models.IdentityWrapper, edgeMaker IdentityEdgeMaker, conMaker IdentityConMaker, input models.ConnectionInput) (*models.IdentityConnection, error) { + var nodes []models.IdentityWrapper + var edges []*models.IdentityEdge + var cursors []string + var pageInfo = &models.PageInfo{} + var totalCount = len(source) + + emptyCon, _ := conMaker(edges, nodes, pageInfo, 0) + + offset := 0 + + if input.After != nil { + for i, value := range source { + edge := edgeMaker(value, i) + if edge.GetCursor() == *input.After { + // remove all previous element including the "after" one + source = source[i+1:] + offset = i + 1 + pageInfo.HasPreviousPage = true + break + } + } + } + + if input.Before != nil { + for i, value := range source { + edge := edgeMaker(value, i+offset) + + if edge.GetCursor() == *input.Before { + // remove all after element including the "before" one + pageInfo.HasNextPage = true + break + } + + e := edge.(models.IdentityEdge) + edges = append(edges, &e) + cursors = append(cursors, edge.GetCursor()) + nodes = append(nodes, value) + } + } else { + edges = make([]*models.IdentityEdge, len(source)) + cursors = make([]string, len(source)) + nodes = source + + for i, value := range source { + edge := edgeMaker(value, i+offset) + e := edge.(models.IdentityEdge) + edges[i] = &e + cursors[i] = edge.GetCursor() + } + } + + if input.First != nil { + if *input.First < 0 { + return emptyCon, fmt.Errorf("first less than zero") + } + + if len(edges) > *input.First { + // Slice result to be of length first by removing edges from the end + edges = edges[:*input.First] + cursors = cursors[:*input.First] + nodes = nodes[:*input.First] + pageInfo.HasNextPage = true + } + } + + if input.Last != nil { + if *input.Last < 0 { + return emptyCon, fmt.Errorf("last less than zero") + } + + if len(edges) > *input.Last { + // Slice result to be of length last by removing edges from the start + edges = edges[len(edges)-*input.Last:] + cursors = cursors[len(cursors)-*input.Last:] + nodes = nodes[len(nodes)-*input.Last:] + pageInfo.HasPreviousPage = true + } + } + + // Fill up pageInfo cursors + if len(cursors) > 0 { + pageInfo.StartCursor = cursors[0] + pageInfo.EndCursor = cursors[len(cursors)-1] + } + + return conMaker(edges, nodes, pageInfo, totalCount) +} diff --git a/api/graphql/connections/gen_label.go b/api/graphql/connections/gen_label.go new file mode 100644 index 00000000..39b1c536 --- /dev/null +++ b/api/graphql/connections/gen_label.go @@ -0,0 +1,113 @@ +// This file was automatically generated by genny. +// Any changes will be lost if this file is regenerated. +// see https://github.com/cheekybits/genny + +package connections + +import ( + "fmt" + + "github.com/MichaelMure/git-bug/api/graphql/models" + "github.com/MichaelMure/git-bug/bug" +) + +// BugLabelEdgeMaker define a function that take a bug.Label and an offset and +// create an Edge. +type LabelEdgeMaker func(value bug.Label, offset int) Edge + +// LabelConMaker define a function that create a models.LabelConnection +type LabelConMaker func( + edges []*models.LabelEdge, + nodes []bug.Label, + info *models.PageInfo, + totalCount int) (*models.LabelConnection, error) + +// LabelCon will paginate a source according to the input of a relay connection +func LabelCon(source []bug.Label, edgeMaker LabelEdgeMaker, conMaker LabelConMaker, input models.ConnectionInput) (*models.LabelConnection, error) { + var nodes []bug.Label + var edges []*models.LabelEdge + var cursors []string + var pageInfo = &models.PageInfo{} + var totalCount = len(source) + + emptyCon, _ := conMaker(edges, nodes, pageInfo, 0) + + offset := 0 + + if input.After != nil { + for i, value := range source { + edge := edgeMaker(value, i) + if edge.GetCursor() == *input.After { + // remove all previous element including the "after" one + source = source[i+1:] + offset = i + 1 + pageInfo.HasPreviousPage = true + break + } + } + } + + if input.Before != nil { + for i, value := range source { + edge := edgeMaker(value, i+offset) + + if edge.GetCursor() == *input.Before { + // remove all after element including the "before" one + pageInfo.HasNextPage = true + break + } + + e := edge.(models.LabelEdge) + edges = append(edges, &e) + cursors = append(cursors, edge.GetCursor()) + nodes = append(nodes, value) + } + } else { + edges = make([]*models.LabelEdge, len(source)) + cursors = make([]string, len(source)) + nodes = source + + for i, value := range source { + edge := edgeMaker(value, i+offset) + e := edge.(models.LabelEdge) + edges[i] = &e + cursors[i] = edge.GetCursor() + } + } + + if input.First != nil { + if *input.First < 0 { + return emptyCon, fmt.Errorf("first less than zero") + } + + if len(edges) > *input.First { + // Slice result to be of length first by removing edges from the end + edges = edges[:*input.First] + cursors = cursors[:*input.First] + nodes = nodes[:*input.First] + pageInfo.HasNextPage = true + } + } + + if input.Last != nil { + if *input.Last < 0 { + return emptyCon, fmt.Errorf("last less than zero") + } + + if len(edges) > *input.Last { + // Slice result to be of length last by removing edges from the start + edges = edges[len(edges)-*input.Last:] + cursors = cursors[len(cursors)-*input.Last:] + nodes = nodes[len(nodes)-*input.Last:] + pageInfo.HasPreviousPage = true + } + } + + // Fill up pageInfo cursors + if len(cursors) > 0 { + pageInfo.StartCursor = cursors[0] + pageInfo.EndCursor = cursors[len(cursors)-1] + } + + return conMaker(edges, nodes, pageInfo, totalCount) +} diff --git a/api/graphql/connections/gen_lazy_bug.go b/api/graphql/connections/gen_lazy_bug.go new file mode 100644 index 00000000..1dc4692e --- /dev/null +++ b/api/graphql/connections/gen_lazy_bug.go @@ -0,0 +1,113 @@ +// This file was automatically generated by genny. +// Any changes will be lost if this file is regenerated. +// see https://github.com/cheekybits/genny + +package connections + +import ( + "fmt" + + "github.com/MichaelMure/git-bug/api/graphql/models" + "github.com/MichaelMure/git-bug/entity" +) + +// EntityIdEdgeMaker define a function that take a entity.Id and an offset and +// create an Edge. +type LazyBugEdgeMaker func(value entity.Id, offset int) Edge + +// LazyBugConMaker define a function that create a models.BugConnection +type LazyBugConMaker func( + edges []*LazyBugEdge, + nodes []entity.Id, + info *models.PageInfo, + totalCount int) (*models.BugConnection, error) + +// LazyBugCon will paginate a source according to the input of a relay connection +func LazyBugCon(source []entity.Id, edgeMaker LazyBugEdgeMaker, conMaker LazyBugConMaker, input models.ConnectionInput) (*models.BugConnection, error) { + var nodes []entity.Id + var edges []*LazyBugEdge + var cursors []string + var pageInfo = &models.PageInfo{} + var totalCount = len(source) + + emptyCon, _ := conMaker(edges, nodes, pageInfo, 0) + + offset := 0 + + if input.After != nil { + for i, value := range source { + edge := edgeMaker(value, i) + if edge.GetCursor() == *input.After { + // remove all previous element including the "after" one + source = source[i+1:] + offset = i + 1 + pageInfo.HasPreviousPage = true + break + } + } + } + + if input.Before != nil { + for i, value := range source { + edge := edgeMaker(value, i+offset) + + if edge.GetCursor() == *input.Before { + // remove all after element including the "before" one + pageInfo.HasNextPage = true + break + } + + e := edge.(LazyBugEdge) + edges = append(edges, &e) + cursors = append(cursors, edge.GetCursor()) + nodes = append(nodes, value) + } + } else { + edges = make([]*LazyBugEdge, len(source)) + cursors = make([]string, len(source)) + nodes = source + + for i, value := range source { + edge := edgeMaker(value, i+offset) + e := edge.(LazyBugEdge) + edges[i] = &e + cursors[i] = edge.GetCursor() + } + } + + if input.First != nil { + if *input.First < 0 { + return emptyCon, fmt.Errorf("first less than zero") + } + + if len(edges) > *input.First { + // Slice result to be of length first by removing edges from the end + edges = edges[:*input.First] + cursors = cursors[:*input.First] + nodes = nodes[:*input.First] + pageInfo.HasNextPage = true + } + } + + if input.Last != nil { + if *input.Last < 0 { + return emptyCon, fmt.Errorf("last less than zero") + } + + if len(edges) > *input.Last { + // Slice result to be of length last by removing edges from the start + edges = edges[len(edges)-*input.Last:] + cursors = cursors[len(cursors)-*input.Last:] + nodes = nodes[len(nodes)-*input.Last:] + pageInfo.HasPreviousPage = true + } + } + + // Fill up pageInfo cursors + if len(cursors) > 0 { + pageInfo.StartCursor = cursors[0] + pageInfo.EndCursor = cursors[len(cursors)-1] + } + + return conMaker(edges, nodes, pageInfo, totalCount) +} diff --git a/api/graphql/connections/gen_lazy_identity.go b/api/graphql/connections/gen_lazy_identity.go new file mode 100644 index 00000000..4996e219 --- /dev/null +++ b/api/graphql/connections/gen_lazy_identity.go @@ -0,0 +1,113 @@ +// This file was automatically generated by genny. +// Any changes will be lost if this file is regenerated. +// see https://github.com/cheekybits/genny + +package connections + +import ( + "fmt" + + "github.com/MichaelMure/git-bug/api/graphql/models" + "github.com/MichaelMure/git-bug/entity" +) + +// EntityIdEdgeMaker define a function that take a entity.Id and an offset and +// create an Edge. +type LazyIdentityEdgeMaker func(value entity.Id, offset int) Edge + +// LazyIdentityConMaker define a function that create a models.IdentityConnection +type LazyIdentityConMaker func( + edges []*LazyIdentityEdge, + nodes []entity.Id, + info *models.PageInfo, + totalCount int) (*models.IdentityConnection, error) + +// LazyIdentityCon will paginate a source according to the input of a relay connection +func LazyIdentityCon(source []entity.Id, edgeMaker LazyIdentityEdgeMaker, conMaker LazyIdentityConMaker, input models.ConnectionInput) (*models.IdentityConnection, error) { + var nodes []entity.Id + var edges []*LazyIdentityEdge + var cursors []string + var pageInfo = &models.PageInfo{} + var totalCount = len(source) + + emptyCon, _ := conMaker(edges, nodes, pageInfo, 0) + + offset := 0 + + if input.After != nil { + for i, value := range source { + edge := edgeMaker(value, i) + if edge.GetCursor() == *input.After { + // remove all previous element including the "after" one + source = source[i+1:] + offset = i + 1 + pageInfo.HasPreviousPage = true + break + } + } + } + + if input.Before != nil { + for i, value := range source { + edge := edgeMaker(value, i+offset) + + if edge.GetCursor() == *input.Before { + // remove all after element including the "before" one + pageInfo.HasNextPage = true + break + } + + e := edge.(LazyIdentityEdge) + edges = append(edges, &e) + cursors = append(cursors, edge.GetCursor()) + nodes = append(nodes, value) + } + } else { + edges = make([]*LazyIdentityEdge, len(source)) + cursors = make([]string, len(source)) + nodes = source + + for i, value := range source { + edge := edgeMaker(value, i+offset) + e := edge.(LazyIdentityEdge) + edges[i] = &e + cursors[i] = edge.GetCursor() + } + } + + if input.First != nil { + if *input.First < 0 { + return emptyCon, fmt.Errorf("first less than zero") + } + + if len(edges) > *input.First { + // Slice result to be of length first by removing edges from the end + edges = edges[:*input.First] + cursors = cursors[:*input.First] + nodes = nodes[:*input.First] + pageInfo.HasNextPage = true + } + } + + if input.Last != nil { + if *input.Last < 0 { + return emptyCon, fmt.Errorf("last less than zero") + } + + if len(edges) > *input.Last { + // Slice result to be of length last by removing edges from the start + edges = edges[len(edges)-*input.Last:] + cursors = cursors[len(cursors)-*input.Last:] + nodes = nodes[len(nodes)-*input.Last:] + pageInfo.HasPreviousPage = true + } + } + + // Fill up pageInfo cursors + if len(cursors) > 0 { + pageInfo.StartCursor = cursors[0] + pageInfo.EndCursor = cursors[len(cursors)-1] + } + + return conMaker(edges, nodes, pageInfo, totalCount) +} diff --git a/api/graphql/connections/gen_operation.go b/api/graphql/connections/gen_operation.go new file mode 100644 index 00000000..4bd84895 --- /dev/null +++ b/api/graphql/connections/gen_operation.go @@ -0,0 +1,113 @@ +// This file was automatically generated by genny. +// Any changes will be lost if this file is regenerated. +// see https://github.com/cheekybits/genny + +package connections + +import ( + "fmt" + + "github.com/MichaelMure/git-bug/api/graphql/models" + "github.com/MichaelMure/git-bug/bug" +) + +// BugOperationEdgeMaker define a function that take a bug.Operation and an offset and +// create an Edge. +type OperationEdgeMaker func(value bug.Operation, offset int) Edge + +// OperationConMaker define a function that create a models.OperationConnection +type OperationConMaker func( + edges []*models.OperationEdge, + nodes []bug.Operation, + info *models.PageInfo, + totalCount int) (*models.OperationConnection, error) + +// OperationCon will paginate a source according to the input of a relay connection +func OperationCon(source []bug.Operation, edgeMaker OperationEdgeMaker, conMaker OperationConMaker, input models.ConnectionInput) (*models.OperationConnection, error) { + var nodes []bug.Operation + var edges []*models.OperationEdge + var cursors []string + var pageInfo = &models.PageInfo{} + var totalCount = len(source) + + emptyCon, _ := conMaker(edges, nodes, pageInfo, 0) + + offset := 0 + + if input.After != nil { + for i, value := range source { + edge := edgeMaker(value, i) + if edge.GetCursor() == *input.After { + // remove all previous element including the "after" one + source = source[i+1:] + offset = i + 1 + pageInfo.HasPreviousPage = true + break + } + } + } + + if input.Before != nil { + for i, value := range source { + edge := edgeMaker(value, i+offset) + + if edge.GetCursor() == *input.Before { + // remove all after element including the "before" one + pageInfo.HasNextPage = true + break + } + + e := edge.(models.OperationEdge) + edges = append(edges, &e) + cursors = append(cursors, edge.GetCursor()) + nodes = append(nodes, value) + } + } else { + edges = make([]*models.OperationEdge, len(source)) + cursors = make([]string, len(source)) + nodes = source + + for i, value := range source { + edge := edgeMaker(value, i+offset) + e := edge.(models.OperationEdge) + edges[i] = &e + cursors[i] = edge.GetCursor() + } + } + + if input.First != nil { + if *input.First < 0 { + return emptyCon, fmt.Errorf("first less than zero") + } + + if len(edges) > *input.First { + // Slice result to be of length first by removing edges from the end + edges = edges[:*input.First] + cursors = cursors[:*input.First] + nodes = nodes[:*input.First] + pageInfo.HasNextPage = true + } + } + + if input.Last != nil { + if *input.Last < 0 { + return emptyCon, fmt.Errorf("last less than zero") + } + + if len(edges) > *input.Last { + // Slice result to be of length last by removing edges from the start + edges = edges[len(edges)-*input.Last:] + cursors = cursors[len(cursors)-*input.Last:] + nodes = nodes[len(nodes)-*input.Last:] + pageInfo.HasPreviousPage = true + } + } + + // Fill up pageInfo cursors + if len(cursors) > 0 { + pageInfo.StartCursor = cursors[0] + pageInfo.EndCursor = cursors[len(cursors)-1] + } + + return conMaker(edges, nodes, pageInfo, totalCount) +} diff --git a/api/graphql/connections/gen_timeline.go b/api/graphql/connections/gen_timeline.go new file mode 100644 index 00000000..952d095c --- /dev/null +++ b/api/graphql/connections/gen_timeline.go @@ -0,0 +1,113 @@ +// This file was automatically generated by genny. +// Any changes will be lost if this file is regenerated. +// see https://github.com/cheekybits/genny + +package connections + +import ( + "fmt" + + "github.com/MichaelMure/git-bug/api/graphql/models" + "github.com/MichaelMure/git-bug/bug" +) + +// BugTimelineItemEdgeMaker define a function that take a bug.TimelineItem and an offset and +// create an Edge. +type TimelineItemEdgeMaker func(value bug.TimelineItem, offset int) Edge + +// TimelineItemConMaker define a function that create a models.TimelineItemConnection +type TimelineItemConMaker func( + edges []*models.TimelineItemEdge, + nodes []bug.TimelineItem, + info *models.PageInfo, + totalCount int) (*models.TimelineItemConnection, error) + +// TimelineItemCon will paginate a source according to the input of a relay connection +func TimelineItemCon(source []bug.TimelineItem, edgeMaker TimelineItemEdgeMaker, conMaker TimelineItemConMaker, input models.ConnectionInput) (*models.TimelineItemConnection, error) { + var nodes []bug.TimelineItem + var edges []*models.TimelineItemEdge + var cursors []string + var pageInfo = &models.PageInfo{} + var totalCount = len(source) + + emptyCon, _ := conMaker(edges, nodes, pageInfo, 0) + + offset := 0 + + if input.After != nil { + for i, value := range source { + edge := edgeMaker(value, i) + if edge.GetCursor() == *input.After { + // remove all previous element including the "after" one + source = source[i+1:] + offset = i + 1 + pageInfo.HasPreviousPage = true + break + } + } + } + + if input.Before != nil { + for i, value := range source { + edge := edgeMaker(value, i+offset) + + if edge.GetCursor() == *input.Before { + // remove all after element including the "before" one + pageInfo.HasNextPage = true + break + } + + e := edge.(models.TimelineItemEdge) + edges = append(edges, &e) + cursors = append(cursors, edge.GetCursor()) + nodes = append(nodes, value) + } + } else { + edges = make([]*models.TimelineItemEdge, len(source)) + cursors = make([]string, len(source)) + nodes = source + + for i, value := range source { + edge := edgeMaker(value, i+offset) + e := edge.(models.TimelineItemEdge) + edges[i] = &e + cursors[i] = edge.GetCursor() + } + } + + if input.First != nil { + if *input.First < 0 { + return emptyCon, fmt.Errorf("first less than zero") + } + + if len(edges) > *input.First { + // Slice result to be of length first by removing edges from the end + edges = edges[:*input.First] + cursors = cursors[:*input.First] + nodes = nodes[:*input.First] + pageInfo.HasNextPage = true + } + } + + if input.Last != nil { + if *input.Last < 0 { + return emptyCon, fmt.Errorf("last less than zero") + } + + if len(edges) > *input.Last { + // Slice result to be of length last by removing edges from the start + edges = edges[len(edges)-*input.Last:] + cursors = cursors[len(cursors)-*input.Last:] + nodes = nodes[len(nodes)-*input.Last:] + pageInfo.HasPreviousPage = true + } + } + + // Fill up pageInfo cursors + if len(cursors) > 0 { + pageInfo.StartCursor = cursors[0] + pageInfo.EndCursor = cursors[len(cursors)-1] + } + + return conMaker(edges, nodes, pageInfo, totalCount) +} |