-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
iter.go
174 lines (146 loc) · 3.66 KB
/
iter.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Package photos contains photos iteration helper.
package photos
import (
"context"
"github.com/go-faster/errors"
"github.com/gotd/td/telegram/message/peer"
"github.com/gotd/td/tg"
)
// Elem is a photo iterator element.
type Elem struct {
Photo tg.PhotoClass
Entities peer.Entities
}
// Iterator is a photo stream iterator.
type Iterator struct {
// Current state.
lastErr error
// Buffer state.
buf []Elem
bufCur int
// Request state.
limit int
lastBatch bool
// Offset parameters state.
offset int
// Remote state.
count int
totalGot bool
// Query builder.
query Query
}
// NewIterator creates new iterator.
func NewIterator(query Query, limit int) *Iterator {
return &Iterator{
buf: make([]Elem, 0, limit),
bufCur: -1,
limit: limit,
query: query,
}
}
// Offset sets Offset request parameter.
func (m *Iterator) Offset(offset int) *Iterator {
m.offset = offset
return m
}
func (m *Iterator) apply(r tg.PhotosPhotosClass) error {
if m.lastBatch {
return nil
}
var (
photos []tg.PhotoClass
entities peer.Entities
)
switch phts := r.(type) {
case *tg.PhotosPhotos: // photos.photos#8dca6aa5
photos = phts.Photos
entities = peer.NewEntities(phts.MapUsers().UserToMap(), map[int64]*tg.Chat{}, map[int64]*tg.Channel{})
m.count = len(phts.Photos)
m.lastBatch = true
case *tg.PhotosPhotosSlice: // photos.photosSlice#15051f54
photos = phts.Photos
entities = peer.NewEntities(phts.MapUsers().UserToMap(), map[int64]*tg.Chat{}, map[int64]*tg.Channel{})
m.count = phts.Count
m.lastBatch = len(phts.Photos) < m.limit
default:
return errors.Errorf("unexpected type %T", r)
}
m.totalGot = true
m.offset += len(photos)
m.bufCur = -1
m.buf = m.buf[:0]
for i := range photos {
m.buf = append(m.buf, Elem{Photo: photos[i], Entities: entities})
}
return nil
}
func (m *Iterator) requestNext(ctx context.Context) error {
r, err := m.query.Query(ctx, Request{
Offset: m.offset,
Limit: m.limit,
})
if err != nil {
return err
}
return m.apply(r)
}
func (m *Iterator) bufNext() bool {
if len(m.buf)-1 <= m.bufCur {
return false
}
m.bufCur++
return true
}
// Total returns last fetched count of elements.
// If count was not fetched before, it requests server using FetchTotal.
func (m *Iterator) Total(ctx context.Context) (int, error) {
if m.totalGot {
return m.count, nil
}
return m.FetchTotal(ctx)
}
// FetchTotal fetches and returns count of elements.
func (m *Iterator) FetchTotal(ctx context.Context) (int, error) {
r, err := m.query.Query(ctx, Request{
Limit: 1,
})
if err != nil {
return 0, errors.Wrap(err, "fetch total")
}
switch phts := r.(type) {
case *tg.PhotosPhotos: // photos.photos#8dca6aa5
m.count = len(phts.Photos)
case *tg.PhotosPhotosSlice: // photos.photosSlice#15051f54
m.count = phts.Count
default:
return 0, errors.Errorf("unexpected type %T", r)
}
m.totalGot = true
return m.count, nil
}
// Next prepares the next message for reading with the Value method.
// It returns true on success, or false if there is no next message or an error happened while preparing it.
// Err should be consulted to distinguish between the two cases.
func (m *Iterator) Next(ctx context.Context) bool {
if m.lastErr != nil {
return false
}
if !m.bufNext() {
// If buffer is empty, we should fetch next batch.
if err := m.requestNext(ctx); err != nil {
m.lastErr = err
return false
}
// Try again with new buffer.
return m.bufNext()
}
return true
}
// Value returns current message.
func (m *Iterator) Value() Elem {
return m.buf[m.bufCur]
}
// Err returns the error, if any, that was encountered during iteration.
func (m *Iterator) Err() error {
return m.lastErr
}