Skip to content

Commit

Permalink
Merge pull request #60 from pilagod/export-cursor-encoder-decoder
Browse files Browse the repository at this point in the history
Add cursor encoder/decoder public getter
  • Loading branch information
pilagod authored Apr 3, 2024
2 parents 057bf3d + d88f346 commit 1319f92
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
6 changes: 3 additions & 3 deletions cursor/encoding_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package cursor

import (
"testing"
"time"

"errors"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/suite"
)
Expand Down Expand Up @@ -79,6 +78,7 @@ func (s *encodingSuite) TestUintPtr() {
}

/* float */

type floatModel struct {
Value float64
ValuePtr *float64
Expand Down
18 changes: 15 additions & 3 deletions paginator/paginator.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ func (p *Paginator) Paginate(db *gorm.DB, dest interface{}) (result *gorm.DB, c
return
}

// GetCursorEncoder returns cursor encoder based on paginator rules
func (p *Paginator) GetCursorEncoder() *cursor.Encoder {
return cursor.NewEncoder(p.getEncoderFields())
}

// GetCursorDecoder returns cursor decoder based on paginator rules
func (p *Paginator) GetCursorDecoder() *cursor.Decoder {
return cursor.NewDecoder(p.getDecoderFields())
}

/* private */

func (p *Paginator) validate(db *gorm.DB, dest interface{}) (err error) {
Expand Down Expand Up @@ -173,12 +183,14 @@ func isNil(i interface{}) bool {
}

func (p *Paginator) decodeCursor(dest interface{}) (result []interface{}, err error) {
decoder := p.GetCursorDecoder()

if p.isForward() {
if result, err = cursor.NewDecoder(p.getDecoderFields()).Decode(*p.cursor.After, dest); err != nil {
if result, err = decoder.Decode(*p.cursor.After, dest); err != nil {
err = ErrInvalidCursor
}
} else if p.isBackward() {
if result, err = cursor.NewDecoder(p.getDecoderFields()).Decode(*p.cursor.Before, dest); err != nil {
if result, err = decoder.Decode(*p.cursor.Before, dest); err != nil {
err = ErrInvalidCursor
}
}
Expand Down Expand Up @@ -255,7 +267,7 @@ func (p *Paginator) buildCursorSQLQueryArgs(fields []interface{}) (args []interf
}

func (p *Paginator) encodeCursor(elems reflect.Value, hasMore bool) (result Cursor, err error) {
encoder := cursor.NewEncoder(p.getEncoderFields())
encoder := p.GetCursorEncoder()
// encode after cursor
if p.isBackward() || hasMore {
c, err := encoder.Encode(elems.Index(elems.Len() - 1))
Expand Down
2 changes: 1 addition & 1 deletion paginator/paginator_paginate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,8 @@ func (s *paginatorSuite) TestPaginateCustomTypeInt() {
var p3 []order
_, c, _ = New(cfg, WithAfter(*c.After)).Paginate(s.db, &p3)
s.Len(p3, 3)
s.assertIDs(p3, 3, 2, 1)
s.assertBackwardOnly(c)
s.assertIDs(p3, 3, 2, 1)

// go back
var p2Back []order
Expand Down

0 comments on commit 1319f92

Please sign in to comment.