-
Notifications
You must be signed in to change notification settings - Fork 7
/
driver.go
152 lines (120 loc) · 2.7 KB
/
driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package base
import (
"errors"
"fmt"
"github.com/raphaelvigee/go-paginate/cursor"
"github.com/raphaelvigee/go-paginate/driver"
)
type ExecutorFactoryArgs struct {
Input interface{}
Cursor cursor.Cursor
}
var ErrNoResult = errors.New("no result")
type Executor interface {
// Must throw ErrNoResult if no result can be found
TakeFirst() (interface{}, error)
CountPrevious(cvalue interface{}) (int64, error)
FindNext(cvalue interface{}, isFirst bool) ([]interface{}, error)
Page(sm interface{}, em interface{}) driver.Executor
}
type Driver struct {
driver.CursorEncoder
ExecutorFactory func(ExecutorFactoryArgs) Executor
}
var _ driver.Driver = (*Driver)(nil)
func (d Driver) Paginate(c cursor.Cursor, input interface{}) (driver.Page, error) {
executor := d.ExecutorFactory(ExecutorFactoryArgs{
Input: input,
Cursor: c,
})
limit := c.Limit
if limit == 0 {
return nil, fmt.Errorf("limit is 0")
}
cvalue := c.Value
isFirst := cvalue == nil
if isFirst {
m, err := executor.TakeFirst()
if err != nil {
if errors.Is(err, ErrNoResult) {
return noResultPage{}, nil
}
return nil, err
}
cvalue = m
}
pc, err := executor.CountPrevious(cvalue)
if err != nil {
return nil, err
}
nvalues, err := executor.FindNext(cvalue, isFirst)
if err != nil {
return nil, err
}
nc := len(nvalues)
hasPreviousPage := pc > 0
hasNextPage := nc > limit
if nc == 0 {
return noResultPage{hasPreviousPage}, nil
}
mi := nc - 1
si := 0
ei := limit - 1
if ei > mi {
ei = mi
}
sm := nvalues[si]
em := nvalues[ei]
sc, err := d.CursorEncode(sm)
if err != nil {
return nil, err
}
ec, err := d.CursorEncode(em)
if err != nil {
return nil, err
}
pExecutor := executor.Page(sm, em)
return page{
Executor: pExecutor,
cursorFunc: func(i int64) (interface{}, error) {
return d.CursorEncode(nvalues[i])
},
pageInfo: driver.PageInfo{
HasNextPage: hasNextPage,
HasPreviousPage: hasPreviousPage,
StartCursor: sc,
EndCursor: ec,
},
}, nil
}
type noResultPage struct {
hasPrevious bool
}
func (n noResultPage) Query(interface{}) error {
return nil
}
func (n noResultPage) Count() (int64, error) {
return 0, nil
}
func (n noResultPage) Cursor(int64) (interface{}, error) {
return nil, errors.New("no cursor available")
}
func (n noResultPage) Info() driver.PageInfo {
return driver.PageInfo{
HasPreviousPage: n.hasPrevious,
HasNextPage: false,
StartCursor: nil,
EndCursor: nil,
}
}
type page struct {
driver.Executor
pageInfo driver.PageInfo
cursorFunc func(i int64) (interface{}, error)
}
func (p page) Cursor(i int64) (interface{}, error) {
return p.cursorFunc(i)
}
func (p page) Info() driver.PageInfo {
return p.pageInfo
}