pagetoken

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package pagetoken pkg provides a token pagination mechanism determined by the limit, offset, and any other optional parameters that have been added to the pagetoken. See example tests for an example of the behavior this package provides.

Index

Examples

Constants

View Source
const (
	// TokenQueryParam is the query param that identifies the next page token.
	TokenQueryParam = "page-token"

	// LimitQueryParam is the query param that identifies with the limit.
	LimitQueryParam = "limit"

	// TotalTODO indicates that the calling code doesn't send the total number of
	// hits and should be updated in the future to do so.
	TotalTODO = -1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Token

type Token struct {
	Limit  int
	Offset int

	Params map[string]string
	// contains filtered or unexported fields
}

Token is a next page token constructor.

Example (First)
params := url.Values{
	"limit":  []string{"5"},
	"offset": []string{"0"},
}

token := GetTokenFromQuery(params)

fmt.Println(token.Limit)
fmt.Println(token.Offset)
fmt.Println(token.String())
Output:

5
0
NP-BAwEBBVRva2VuAf-CAAEDAQVMaW1pdAEEAAEGT2Zmc2V0AQQAAQZQYXJhbXMB_4QAAAAh_4MEAQERbWFwW3N0cmluZ11zdHJpbmcB_4QAAQwBDAAABf-CAQoA
Example (Second)
params := url.Values{
	TokenQueryParam: []string{"NP-BAwEBBVRva2VuAf-CAAEDAQVMaW1pdAEEAAEGT2Zmc2V0AQQAAQZQYXJhbXMB_4QAAAAh_4MEAQERbWFwW3N0cmluZ11zdHJpbmcB_4QAAQwBDAAABf-CAQoA"},
}

token := GetTokenFromQuery(params)

fmt.Println(token.Limit)
fmt.Println(token.Offset)
fmt.Println(token.String())
Output:

5
0
NP-BAwEBBVRva2VuAf-CAAEDAQVMaW1pdAEEAAEGT2Zmc2V0AQQAAQZQYXJhbXMB_4QAAAAh_4MEAQERbWFwW3N0cmluZ11zdHJpbmcB_4QAAQwBDAAABf-CAQoA

func GetToken

func GetToken(token string) Token

GetToken parses a token string and returns a valid Token

func GetTokenFromQuery

func GetTokenFromQuery(q url.Values, opts ...TokenOptFn) Token

GetTokenFromQuery provides the Token from the url.Values.

func (Token) GetParam

func (t Token) GetParam(key string) string

GetParam returns the available value for the key provided. If none exists then "" is returned.

func (Token) GetParamAsBool

func (t Token) GetParamAsBool(key string) bool

GetParamAsBool returns the available value for the key provided, as a bool. If none exists then false is returned.

func (Token) NextToken

func (t Token) NextToken(numRecords, totalRecords int) string

NextToken generates new token from existing token and number of records returned from operation. If number of records is less than the limit or if the current offset + limit is greater than or equal tot the total records then there is no next page. Set totalRecords to TotalTODO to preserve backwards compatability until you fix your handlers to provide an accurate total.

Example (First)
params := url.Values{
	"limit":  []string{"5"},
	"offset": []string{"0"},
}

token := GetTokenFromQuery(params)

totalRecords := 6
nextPageToken := token.NextToken(5, totalRecords)

fmt.Println(nextPageToken)

token = GetToken(nextPageToken)
fmt.Println(token.Limit)
fmt.Println(token.Offset)
fmt.Println(token.String())
Output:

NP-BAwEBBVRva2VuAf-CAAEDAQVMaW1pdAEEAAEGT2Zmc2V0AQQAAQZQYXJhbXMB_4QAAAAh_4MEAQERbWFwW3N0cmluZ11zdHJpbmcB_4QAAQwBDAAAB_-CAQoBCgA=
5
5
NP-BAwEBBVRva2VuAf-CAAEDAQVMaW1pdAEEAAEGT2Zmc2V0AQQAAQZQYXJhbXMB_4QAAAAh_4MEAQERbWFwW3N0cmluZ11zdHJpbmcB_4QAAQwBDAAAB_-CAQoBCgA=
Example (Fourth)
// strToken below is equivalent to setting limit=5 and offset=0
strToken := "NP-BAwEBBVRva2VuAf-CAAEDAQVMaW1pdAEEAAEGT2Zmc2V0AQQAAQZQYXJhbXMB_4QAAAAh_4MEAQERbWFwW3N0cmluZ11zdHJpbmcB_4QAAQwBDAAABf-CAQoA"
params := url.Values{
	TokenQueryParam: []string{strToken},
}

token := GetTokenFromQuery(params)

totalRecords := 5
// total records == 5, so no pagination
nextPageToken := token.NextToken(5, totalRecords)
fmt.Println(nextPageToken)
Output:

Example (Second)
// strToken below is equivalent to setting limit=5 and offset=0
strToken := "NP-BAwEBBVRva2VuAf-CAAEDAQVMaW1pdAEEAAEGT2Zmc2V0AQQAAQZQYXJhbXMB_4QAAAAh_4MEAQERbWFwW3N0cmluZ11zdHJpbmcB_4QAAQwBDAAABf-CAQoA"
params := url.Values{
	TokenQueryParam: []string{strToken},
}

token := GetTokenFromQuery(params)

totalRecords := 6
nextPageToken := token.NextToken(5, totalRecords)

fmt.Println(nextPageToken)

token = GetToken(nextPageToken)
fmt.Println(token.Limit)
fmt.Println(token.Offset)
fmt.Println(token.String())
Output:

NP-BAwEBBVRva2VuAf-CAAEDAQVMaW1pdAEEAAEGT2Zmc2V0AQQAAQZQYXJhbXMB_4QAAAAh_4MEAQERbWFwW3N0cmluZ11zdHJpbmcB_4QAAQwBDAAAB_-CAQoBCgA=
5
5
NP-BAwEBBVRva2VuAf-CAAEDAQVMaW1pdAEEAAEGT2Zmc2V0AQQAAQZQYXJhbXMB_4QAAAAh_4MEAQERbWFwW3N0cmluZ11zdHJpbmcB_4QAAQwBDAAAB_-CAQoBCgA=
Example (Third)
params := url.Values{
	"limit":  []string{"5"},
	"offset": []string{"0"},
}

token := GetTokenFromQuery(params)

totalRecords := 5
nextPageToken := token.NextToken(5, totalRecords)
fmt.Println(nextPageToken)
Output:

func (Token) PreviousToken

func (t Token) PreviousToken() string

PreviousToken generates a new token from existing token for the previous page of results.

func (Token) String

func (t Token) String() string

String implements the fmt.Stringer interfaces to get a string representation of the TokenDeets.

type TokenOptFn

type TokenOptFn func(Token) Token

TokenOptFn is a functional option for setting token deets with sane bounds.

Example
params := url.Values{
	"limit": []string{"2000"},
}

token := GetTokenFromQuery(params,
	DefaultLimit(10),
	MinLimit(1),
	MaxLimit(100),
	Param("foo", "bar"),
)

fmt.Println(token.Limit)
Output:

100

func DefaultLimit

func DefaultLimit(v int) TokenOptFn

DefaultLimit applies a default limit when no value had been limitSet.

Example
params := url.Values{}

token := GetTokenFromQuery(params, DefaultLimit(10))

fmt.Println(token.Limit)
Output:

10

func MaxLimit

func MaxLimit(max int) TokenOptFn

MaxLimit applies a higher bound to the token limit.

Example
params := url.Values{
	"limit": []string{"2000"},
}

token := GetTokenFromQuery(params, MaxLimit(100))

fmt.Println(token.Limit)
Output:

100

func MinLimit

func MinLimit(min int) TokenOptFn

MinLimit applies a lower bound to the token limit.

Example
params := url.Values{
	"limit": []string{"-2000"},
}

token := GetTokenFromQuery(params, MinLimit(1))

fmt.Println(token.Limit)
Output:

1

func Param

func Param(key, val string) TokenOptFn

Param assigns a param value to an unset param key.

Example
params := url.Values{}

token := GetTokenFromQuery(params,
	DefaultLimit(10),
	Param("sort", "name"),
	Param("order", "asc"),
)

fmt.Println(token.Limit)
fmt.Println(token.GetParam("sort"))
fmt.Println(token.GetParam("order"))

// params are baked into the NextToken
totalRecords := 11
nextToken := GetToken(token.NextToken(10, totalRecords))

fmt.Println(nextToken.Limit)
fmt.Println(token.GetParam("sort"))
fmt.Println(token.GetParam("order"))
Output:

10
name
asc
10
name
asc

Jump to

Keyboard shortcuts

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