@@ -24,6 +24,8 @@ const (
2424 DefaultMaxSize = 500
2525)
2626
27+ var dbStructTagMapper = reflectx .NewMapper ("db" )
28+
2729func (p * Paginator ) DefaultToken () PageToken { return p .defaultToken }
2830func (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.
7678func 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