-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathresult.go
58 lines (47 loc) · 1.48 KB
/
result.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
package zammad
// Result is a generic type that holds a slice of results and provides methods
// to paginate through them.
type Result[T any] struct {
res []T // Slice of results of type T
resFunc func(options RequestOptions) ([]T, error) // Function to fetch results based on RequestOptions
opts RequestOptions // Options for the request
lastErr error // Last error encountered during fetching
}
// Next fetches the next page of results. It returns false if there are no more
// results to fetch. If an error occurs during fetching, it sets the lastErr
// field and returns true.
func (tr *Result[T]) Next() bool {
res, err := tr.resFunc(tr.opts)
if err != nil {
tr.res = nil
tr.lastErr = err
return true
}
if tr.opts.page == 0 {
tr.opts.page = 1
}
tr.opts.page++
tr.res = res
if len(res) == 0 {
return false
}
return true
}
// Fetch returns the current slice of results and the last error encountered.
func (tr *Result[T]) Fetch() ([]T, error) {
return tr.res, tr.lastErr
}
// FetchAll fetches all pages of results and returns them as a single slice.
// If an error occurs during fetching, it returns the results fetched so far
// along with the error.
func (tr *Result[T]) FetchAll() ([]T, error) {
resAll := make([]T, 0)
for tr.Next() {
res, err := tr.Fetch()
if err != nil {
return resAll, err
}
resAll = append(resAll, res...)
}
return resAll, nil
}