Skip to content

Commit 756708e

Browse files
zepatrikory-bot
authored andcommitted
feat: custom page token column extraction
GitOrigin-RevId: 706b836df390da53f8ef3e3800391b206b715949
1 parent f8a53b0 commit 756708e

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

oryx/pagination/keysetpagination_v2/paginator.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const (
2424
DefaultMaxSize = 500
2525
)
2626

27+
var dbStructTagMapper = reflectx.NewMapper("db")
28+
2729
func (p *Paginator) DefaultToken() PageToken { return p.defaultToken }
2830
func (p *Paginator) IsLast() bool { return p.isLast }
2931

@@ -74,6 +76,15 @@ func (p *Paginator) ToOptions() []Option {
7476

7577
// Result removes the last item (if applicable) and returns the paginator for the next page.
7678
func Result[I any](items []I, p *Paginator) ([]I, *Paginator) {
79+
return ResultFunc(items, p, func(last I, colName string) any {
80+
lastItemVal := reflect.ValueOf(last)
81+
return dbStructTagMapper.FieldByName(lastItemVal, colName).Interface()
82+
})
83+
}
84+
85+
// ResultFunc removes the last item (if applicable) and returns the paginator for the next page.
86+
// The extractor function is used to extract the column values from the last item.
87+
func ResultFunc[I any](items []I, p *Paginator, extractor func(last I, colName string) any) ([]I, *Paginator) {
7788
if len(items) <= p.Size() {
7889
return items, &Paginator{
7990
isLast: true,
@@ -88,15 +99,13 @@ func Result[I any](items []I, p *Paginator) ([]I, *Paginator) {
8899
items = items[:p.Size()]
89100
lastItem := items[len(items)-1]
90101

91-
mapper := reflectx.NewMapper("db")
92-
lastItemVal := reflect.ValueOf(lastItem)
93102
currentCols := p.PageToken().Columns()
94103
newCols := make([]Column, len(currentCols))
95104
for i, col := range currentCols {
96105
newCols[i] = Column{
97106
Name: col.Name,
98107
Order: col.Order,
99-
Value: mapper.FieldByName(lastItemVal, col.Name).Interface(),
108+
Value: extractor(lastItem, col.Name),
100109
}
101110
}
102111

0 commit comments

Comments
 (0)