cursor

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 3, 2025 License: Unlicense Imports: 2 Imported by: 0

README

Cursor Continuous Integration Go Report Card Go Reference

Cursor is a generic implementation of the Relay Cursor Connections Specification. It is a simple library that can be used to paginate any list of items.

Installation

Simply run the following command to get the package and start using it:

go get github.com/asger-noer/go-cursor

Usage

You can create a new connection by providing a list of items and a cursor function that can be used to get the cursor for each item. You can also provide a list of arguments to the connection to specify the pagination.

args := []cursor.Argument{
	cursor.First(10),
	cursor.After("MQ=="),
}

// Create a new connection with the list of users
cur, err := cursor.New(users, userCursor, args...)
if err != nil {
	// Handle error
}

// pageinfo contains information about the current page
pageinfo := cur.PageInfo()

// Edges contains the list of items in the current page
edges := cur.Edges() {

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConflictingArguments = errors.New("first and last cannot be used together")
	ErrInvalidFirst         = errors.New("first argument must be a non-negative integer")
	ErrInvalidLast          = errors.New("last argument must be a non-negative integer")
)

Functions

This section is empty.

Types

type Argument

type Argument func(*arguments) error

func After

func After(after *string) Argument

After sets the after option for the connection.

func Before

func Before(before *string) Argument

Before sets the before option for the connection.

func First

func First(first *int) Argument

First sets the first option for the connection.

func Last

func Last(last *int) Argument

Last sets the last option for the connection.

type Connection

type Connection[T any] struct {
	// contains filtered or unexported fields
}

Connection is a generic connection type that is used to paginate results.

func New

func New[T any](nodes []T, cursor func(T) string, args ...Argument) (Connection[T], error)

New creates a new Pages object. The pages object is used to paginate

func (*Connection[T]) Edges

func (p *Connection[T]) Edges() []Edge[T]

Edges returns the edges for the connection.

func (*Connection[T]) PageInfo

func (p *Connection[T]) PageInfo() PageInfo

PageInfo returns the page info for the connection.

type Edge

type Edge[T any] struct {
	// contains filtered or unexported fields
}

Edge is a generic type that is used to define the edges for a connection in at paginated result. The Edge interface is used to define the the cursor that is used to paginate the nodes and the node itself.

func (Edge[T]) Cursor

func (e Edge[T]) Cursor() string

func (Edge[T]) Node

func (e Edge[T]) Node() T

type Edges

type Edges[T any] []Edge[T]

func (Edges[T]) Nodes

func (e Edges[T]) Nodes() []T

type PageInfo added in v1.0.4

type PageInfo struct {
	// contains filtered or unexported fields
}

func (*PageInfo) EndCursor added in v1.0.4

func (p *PageInfo) EndCursor() *string

EndCursor returns the end cursor for the connection.

func (*PageInfo) HasNextPage added in v1.0.4

func (p *PageInfo) HasNextPage() bool

HasNextPage returns true if there is a next page.

func (*PageInfo) HasPreviousPage added in v1.0.4

func (p *PageInfo) HasPreviousPage() bool

HasPreviousPage returns true if there is a previous page.

func (*PageInfo) StartCursor added in v1.0.4

func (p *PageInfo) StartCursor() *string

StartCursor returns the start cursor for the connection.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL