Trying out paginated cursortoken variant [UNTESTED]
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m11s
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m11s
This commit is contained in:
@@ -3,12 +3,16 @@ package cursortoken
|
||||
import (
|
||||
"encoding/base32"
|
||||
"encoding/json"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CursorToken interface {
|
||||
Token() string
|
||||
}
|
||||
|
||||
type Mode string
|
||||
|
||||
const (
|
||||
@@ -24,97 +28,6 @@ type Extra struct {
|
||||
PageSize *int
|
||||
}
|
||||
|
||||
type CursorToken struct {
|
||||
Mode Mode
|
||||
ValuePrimary string
|
||||
ValueSecondary string
|
||||
Direction SortDirection
|
||||
DirectionSecondary SortDirection
|
||||
PageSize int
|
||||
Extra Extra
|
||||
}
|
||||
|
||||
type cursorTokenSerialize struct {
|
||||
ValuePrimary *string `json:"v1,omitempty"`
|
||||
ValueSecondary *string `json:"v2,omitempty"`
|
||||
Direction *SortDirection `json:"dir,omitempty"`
|
||||
DirectionSecondary *SortDirection `json:"dir2,omitempty"`
|
||||
PageSize *int `json:"size,omitempty"`
|
||||
|
||||
ExtraTimestamp *time.Time `json:"ts,omitempty"`
|
||||
ExtraId *string `json:"id,omitempty"`
|
||||
ExtraPage *int `json:"pg,omitempty"`
|
||||
ExtraPageSize *int `json:"sz,omitempty"`
|
||||
}
|
||||
|
||||
func Start() CursorToken {
|
||||
return CursorToken{
|
||||
Mode: CTMStart,
|
||||
ValuePrimary: "",
|
||||
ValueSecondary: "",
|
||||
Direction: "",
|
||||
DirectionSecondary: "",
|
||||
PageSize: 0,
|
||||
Extra: Extra{},
|
||||
}
|
||||
}
|
||||
|
||||
func End() CursorToken {
|
||||
return CursorToken{
|
||||
Mode: CTMEnd,
|
||||
ValuePrimary: "",
|
||||
ValueSecondary: "",
|
||||
Direction: "",
|
||||
DirectionSecondary: "",
|
||||
PageSize: 0,
|
||||
Extra: Extra{},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *CursorToken) Token() string {
|
||||
if c.Mode == CTMStart {
|
||||
return "@start"
|
||||
}
|
||||
if c.Mode == CTMEnd {
|
||||
return "@end"
|
||||
}
|
||||
|
||||
// We kinda manually implement omitempty for the CursorToken here
|
||||
// because omitempty does not work for time.Time and otherwise we would always
|
||||
// get weird time values when decoding a token that initially didn't have an Timestamp set
|
||||
// For this usecase we treat Unix=0 as an empty timestamp
|
||||
|
||||
sertok := cursorTokenSerialize{}
|
||||
|
||||
if c.ValuePrimary != "" {
|
||||
sertok.ValuePrimary = &c.ValuePrimary
|
||||
}
|
||||
if c.ValueSecondary != "" {
|
||||
sertok.ValueSecondary = &c.ValueSecondary
|
||||
}
|
||||
if c.Direction != "" {
|
||||
sertok.Direction = &c.Direction
|
||||
}
|
||||
if c.DirectionSecondary != "" {
|
||||
sertok.DirectionSecondary = &c.DirectionSecondary
|
||||
}
|
||||
if c.PageSize != 0 {
|
||||
sertok.PageSize = &c.PageSize
|
||||
}
|
||||
|
||||
sertok.ExtraTimestamp = c.Extra.Timestamp
|
||||
sertok.ExtraId = c.Extra.Id
|
||||
sertok.ExtraPage = c.Extra.Page
|
||||
sertok.ExtraPageSize = c.Extra.PageSize
|
||||
|
||||
body, err := json.Marshal(sertok)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return "tok_" + base32.StdEncoding.EncodeToString(body)
|
||||
}
|
||||
|
||||
func Decode(tok string) (CursorToken, error) {
|
||||
if tok == "" {
|
||||
return Start(), nil
|
||||
@@ -125,60 +38,56 @@ func Decode(tok string) (CursorToken, error) {
|
||||
if strings.ToLower(tok) == "@end" {
|
||||
return End(), nil
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(tok, "tok_") {
|
||||
return CursorToken{}, exerr.New(exerr.TypeCursorTokenDecode, "could not decode token, missing prefix").Str("token", tok).Build()
|
||||
if strings.ToLower(tok) == "$end" {
|
||||
return PageEnd(), nil
|
||||
}
|
||||
if strings.HasPrefix(tok, "$") && len(tok) > 1 {
|
||||
n, err := strconv.ParseInt(tok[1:], 10, 64)
|
||||
if err != nil {
|
||||
return nil, exerr.Wrap(err, "failed to deserialize token").Str("token", tok).WithType(exerr.TypeCursorTokenDecode).Build()
|
||||
}
|
||||
return Page(int(n)), nil
|
||||
}
|
||||
|
||||
body, err := base32.StdEncoding.DecodeString(tok[len("tok_"):])
|
||||
if err != nil {
|
||||
return CursorToken{}, err
|
||||
}
|
||||
if strings.HasPrefix(tok, "tok_") {
|
||||
|
||||
var tokenDeserialize cursorTokenSerialize
|
||||
err = json.Unmarshal(body, &tokenDeserialize)
|
||||
if err != nil {
|
||||
return CursorToken{}, exerr.Wrap(err, "failed to deserialize token").Str("token", tok).Build()
|
||||
}
|
||||
body, err := base32.StdEncoding.DecodeString(tok[len("tok_"):])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
token := CursorToken{Mode: CTMNormal}
|
||||
var tokenDeserialize cursorTokenKeySortSerialize
|
||||
err = json.Unmarshal(body, &tokenDeserialize)
|
||||
if err != nil {
|
||||
return nil, exerr.Wrap(err, "failed to deserialize token").Str("token", tok).WithType(exerr.TypeCursorTokenDecode).Build()
|
||||
}
|
||||
|
||||
if tokenDeserialize.ValuePrimary != nil {
|
||||
token.ValuePrimary = *tokenDeserialize.ValuePrimary
|
||||
}
|
||||
if tokenDeserialize.ValueSecondary != nil {
|
||||
token.ValueSecondary = *tokenDeserialize.ValueSecondary
|
||||
}
|
||||
if tokenDeserialize.Direction != nil {
|
||||
token.Direction = *tokenDeserialize.Direction
|
||||
}
|
||||
if tokenDeserialize.DirectionSecondary != nil {
|
||||
token.DirectionSecondary = *tokenDeserialize.DirectionSecondary
|
||||
}
|
||||
if tokenDeserialize.PageSize != nil {
|
||||
token.PageSize = *tokenDeserialize.PageSize
|
||||
}
|
||||
token := CTKeySort{Mode: CTMNormal}
|
||||
|
||||
token.Extra.Timestamp = tokenDeserialize.ExtraTimestamp
|
||||
token.Extra.Id = tokenDeserialize.ExtraId
|
||||
token.Extra.Page = tokenDeserialize.ExtraPage
|
||||
token.Extra.PageSize = tokenDeserialize.ExtraPageSize
|
||||
if tokenDeserialize.ValuePrimary != nil {
|
||||
token.ValuePrimary = *tokenDeserialize.ValuePrimary
|
||||
}
|
||||
if tokenDeserialize.ValueSecondary != nil {
|
||||
token.ValueSecondary = *tokenDeserialize.ValueSecondary
|
||||
}
|
||||
if tokenDeserialize.Direction != nil {
|
||||
token.Direction = *tokenDeserialize.Direction
|
||||
}
|
||||
if tokenDeserialize.DirectionSecondary != nil {
|
||||
token.DirectionSecondary = *tokenDeserialize.DirectionSecondary
|
||||
}
|
||||
if tokenDeserialize.PageSize != nil {
|
||||
token.PageSize = *tokenDeserialize.PageSize
|
||||
}
|
||||
|
||||
return token, nil
|
||||
}
|
||||
token.Extra.Timestamp = tokenDeserialize.ExtraTimestamp
|
||||
token.Extra.Id = tokenDeserialize.ExtraId
|
||||
token.Extra.Page = tokenDeserialize.ExtraPage
|
||||
token.Extra.PageSize = tokenDeserialize.ExtraPageSize
|
||||
|
||||
return token, nil
|
||||
|
||||
func (c *CursorToken) ValuePrimaryObjectId() (primitive.ObjectID, bool) {
|
||||
if oid, err := primitive.ObjectIDFromHex(c.ValuePrimary); err == nil {
|
||||
return oid, true
|
||||
} else {
|
||||
return primitive.ObjectID{}, false
|
||||
}
|
||||
}
|
||||
|
||||
func (c *CursorToken) ValueSecondaryObjectId() (primitive.ObjectID, bool) {
|
||||
if oid, err := primitive.ObjectIDFromHex(c.ValueSecondary); err == nil {
|
||||
return oid, true
|
||||
} else {
|
||||
return primitive.ObjectID{}, false
|
||||
return nil, exerr.New(exerr.TypeCursorTokenDecode, "could not decode token, missing/unknown prefix").Str("token", tok).Build()
|
||||
}
|
||||
}
|
||||
|
128
cursortoken/tokenKeySort.go
Normal file
128
cursortoken/tokenKeySort.go
Normal file
@@ -0,0 +1,128 @@
|
||||
package cursortoken
|
||||
|
||||
import (
|
||||
"encoding/base32"
|
||||
"encoding/json"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CTKeySort struct {
|
||||
Mode Mode
|
||||
ValuePrimary string
|
||||
ValueSecondary string
|
||||
Direction SortDirection
|
||||
DirectionSecondary SortDirection
|
||||
PageSize int
|
||||
Extra Extra
|
||||
}
|
||||
|
||||
type cursorTokenKeySortSerialize struct {
|
||||
ValuePrimary *string `json:"v1,omitempty"`
|
||||
ValueSecondary *string `json:"v2,omitempty"`
|
||||
Direction *SortDirection `json:"dir,omitempty"`
|
||||
DirectionSecondary *SortDirection `json:"dir2,omitempty"`
|
||||
PageSize *int `json:"size,omitempty"`
|
||||
|
||||
ExtraTimestamp *time.Time `json:"ts,omitempty"`
|
||||
ExtraId *string `json:"id,omitempty"`
|
||||
ExtraPage *int `json:"pg,omitempty"`
|
||||
ExtraPageSize *int `json:"sz,omitempty"`
|
||||
}
|
||||
|
||||
func NewKeySortToken(valuePrimary string, valueSecondary string, direction SortDirection, directionSecondary SortDirection, pageSize int, extra Extra) CursorToken {
|
||||
return CTKeySort{
|
||||
Mode: CTMNormal,
|
||||
ValuePrimary: valuePrimary,
|
||||
ValueSecondary: valueSecondary,
|
||||
Direction: direction,
|
||||
DirectionSecondary: directionSecondary,
|
||||
PageSize: pageSize,
|
||||
Extra: extra,
|
||||
}
|
||||
}
|
||||
|
||||
func Start() CursorToken {
|
||||
return CTKeySort{
|
||||
Mode: CTMStart,
|
||||
ValuePrimary: "",
|
||||
ValueSecondary: "",
|
||||
Direction: "",
|
||||
DirectionSecondary: "",
|
||||
PageSize: 0,
|
||||
Extra: Extra{},
|
||||
}
|
||||
}
|
||||
|
||||
func End() CursorToken {
|
||||
return CTKeySort{
|
||||
Mode: CTMEnd,
|
||||
ValuePrimary: "",
|
||||
ValueSecondary: "",
|
||||
Direction: "",
|
||||
DirectionSecondary: "",
|
||||
PageSize: 0,
|
||||
Extra: Extra{},
|
||||
}
|
||||
}
|
||||
|
||||
func (c CTKeySort) Token() string {
|
||||
|
||||
if c.Mode == CTMStart {
|
||||
return "@start"
|
||||
}
|
||||
if c.Mode == CTMEnd {
|
||||
return "@end"
|
||||
}
|
||||
|
||||
// We kinda manually implement omitempty for the CursorToken here
|
||||
// because omitempty does not work for time.Time and otherwise we would always
|
||||
// get weird time values when decoding a token that initially didn't have an Timestamp set
|
||||
// For this usecase we treat Unix=0 as an empty timestamp
|
||||
|
||||
sertok := cursorTokenKeySortSerialize{}
|
||||
|
||||
if c.ValuePrimary != "" {
|
||||
sertok.ValuePrimary = &c.ValuePrimary
|
||||
}
|
||||
if c.ValueSecondary != "" {
|
||||
sertok.ValueSecondary = &c.ValueSecondary
|
||||
}
|
||||
if c.Direction != "" {
|
||||
sertok.Direction = &c.Direction
|
||||
}
|
||||
if c.DirectionSecondary != "" {
|
||||
sertok.DirectionSecondary = &c.DirectionSecondary
|
||||
}
|
||||
if c.PageSize != 0 {
|
||||
sertok.PageSize = &c.PageSize
|
||||
}
|
||||
|
||||
sertok.ExtraTimestamp = c.Extra.Timestamp
|
||||
sertok.ExtraId = c.Extra.Id
|
||||
sertok.ExtraPage = c.Extra.Page
|
||||
sertok.ExtraPageSize = c.Extra.PageSize
|
||||
|
||||
body, err := json.Marshal(sertok)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return "tok_" + base32.StdEncoding.EncodeToString(body)
|
||||
}
|
||||
|
||||
func (c CTKeySort) valuePrimaryObjectId() (primitive.ObjectID, bool) {
|
||||
if oid, err := primitive.ObjectIDFromHex(c.ValuePrimary); err == nil {
|
||||
return oid, true
|
||||
} else {
|
||||
return primitive.ObjectID{}, false
|
||||
}
|
||||
}
|
||||
|
||||
func (c CTKeySort) valueSecondaryObjectId() (primitive.ObjectID, bool) {
|
||||
if oid, err := primitive.ObjectIDFromHex(c.ValueSecondary); err == nil {
|
||||
return oid, true
|
||||
} else {
|
||||
return primitive.ObjectID{}, false
|
||||
}
|
||||
}
|
33
cursortoken/tokenPaginate.go
Normal file
33
cursortoken/tokenPaginate.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package cursortoken
|
||||
|
||||
import "strconv"
|
||||
|
||||
type CTPaginated struct {
|
||||
Mode Mode
|
||||
Page int
|
||||
}
|
||||
|
||||
func Page(p int) CursorToken {
|
||||
return CTPaginated{
|
||||
Mode: CTMNormal,
|
||||
Page: p,
|
||||
}
|
||||
}
|
||||
|
||||
func PageEnd() CursorToken {
|
||||
return CTPaginated{
|
||||
Mode: CTMEnd,
|
||||
Page: 0,
|
||||
}
|
||||
}
|
||||
|
||||
func (c CTPaginated) Token() string {
|
||||
if c.Mode == CTMStart {
|
||||
return "$1"
|
||||
}
|
||||
if c.Mode == CTMEnd {
|
||||
return "$end"
|
||||
}
|
||||
|
||||
return "$" + strconv.Itoa(c.Page)
|
||||
}
|
Reference in New Issue
Block a user