pager

package
v0.0.67 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: Apache-2.0, NCSA Imports: 5 Imported by: 0

Documentation

Overview

Package pager implements a generic SetPager that splits a stream of Groups into a single Set and one-or-more associated Pages. Useful for constructing paged serving data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Group

type Group any

A Group is part of a Set.

type Head any

A Head signals the start of a new Set.

type Set

type Set any

A Set is a set of Groups.

type SetPager

type SetPager struct {
	// MaxPageSize is the maximum size of a Set or Page, as calculated by the
	// given Size function.
	MaxPageSize int

	// SkipEmpty determines whether empty Sets/Pages will be emitted.
	SkipEmpty bool

	// OutputSet should output the given Set and Groups not previously emitted by
	// OutputPage.  The total size of all Groups is given.
	OutputSet func(context.Context, int, Set, []Group) error
	// OutputPage should output the given Group as an individual Page.  The Set
	// currently being built is given for any necessary mutations.
	OutputPage func(context.Context, Set, Group) error

	// NewSet returns a new Set for the given Head.
	NewSet func(Head) Set
	// Combine possibly merges two Groups with the Head together.  If not
	// possible, nil should be returned.
	//
	//   Constraints (if g != nil):
	//     Combine(l, r) == Split(Size(l), g)
	Combine func(l, r Group) (g Group)
	// Split splits the given Group into a Group of the given size and a Group
	// with any leftovers.
	//
	//   Constraints:
	//     Size(l) == total
	//     Size(r) == Size(g) - total
	//     g == Combine(l, r)
	Split func(total int, g Group) (l, r Group)
	// Size returns the size of the given Group.
	//
	// Constraints:
	//   Size(l) + Size(r) == Size(Combine(l, r))
	Size func(Group) int
	// contains filtered or unexported fields
}

SetPager constructs a set of Sets and Pages from a sequence of Heads and Groups. For each set of Groups with the same Head, a call to StartSet must precede. All Groups for the same Head are then assumed to be given sequentially to AddGroup. Flush must be called after the final call to AddGroup.

func (*SetPager) AddGroup

func (p *SetPager) AddGroup(ctx context.Context, g Group) error

AddGroup adds a Group to current Set being built, possibly emitting a new Set and/or Page. StartSet must be called before any calls to this method. See SetPager's documentation for the assumed order of the groups and this method's relation to StartSet.

func (*SetPager) Flush

func (p *SetPager) Flush(ctx context.Context) error

Flush signals the end of the current Set being built, flushing it, and its Groups to the output function. This should be called after the final call to AddGroup. Manually calling Flush at any other time is unnecessary.

func (*SetPager) StartSet

func (p *SetPager) StartSet(ctx context.Context, hd Head) error

StartSet begins a new Set for the given Head, possibly emitting a previous Set. Each following call to AddGroup adds the group to this new Set until another call to StartSet is made.

Jump to

Keyboard shortcuts

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