-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
queries.gen.go
151 lines (125 loc) · 3.84 KB
/
queries.gen.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
// Code generated by itergen, DO NOT EDIT.
package dialogs
import (
"context"
"github.com/go-faster/errors"
"github.com/gotd/td/tg"
)
// No-op definition for keeping imports.
var _ = context.Background()
// Request is a parameter for Query.
type Request struct {
OffsetDate int
OffsetID int
OffsetPeer tg.InputPeerClass
Limit int
}
// Query is an abstraction for dialogs request.
// NB: iterator mutates returned data (sorts, at least).
type Query interface {
Query(ctx context.Context, req Request) (tg.MessagesDialogsClass, error)
}
// QueryFunc is a function adapter for Query.
type QueryFunc func(ctx context.Context, req Request) (tg.MessagesDialogsClass, error)
// Query implements Query interface.
func (q QueryFunc) Query(ctx context.Context, req Request) (tg.MessagesDialogsClass, error) {
return q(ctx, req)
}
// QueryBuilder is a helper to create message queries.
type QueryBuilder struct {
raw *tg.Client
}
// NewQueryBuilder creates new QueryBuilder.
func NewQueryBuilder(raw *tg.Client) *QueryBuilder {
return &QueryBuilder{raw: raw}
}
// GetDialogsQueryBuilder is query builder of MessagesGetDialogs.
type GetDialogsQueryBuilder struct {
raw *tg.Client
req tg.MessagesGetDialogsRequest
batchSize int
offsetDate int
offsetID int
offsetPeer tg.InputPeerClass
}
// GetDialogs creates query builder of MessagesGetDialogs.
func (q *QueryBuilder) GetDialogs() *GetDialogsQueryBuilder {
b := &GetDialogsQueryBuilder{
raw: q.raw,
batchSize: 1,
req: tg.MessagesGetDialogsRequest{},
}
return b
}
// BatchSize sets buffer of message loaded from one request.
// Be carefully, when set this limit, because Telegram does not return error if limit is too big,
// so results can be incorrect.
func (b *GetDialogsQueryBuilder) BatchSize(batchSize int) *GetDialogsQueryBuilder {
b.batchSize = batchSize
return b
}
// OffsetDate sets offsetDate from which iterate start.
func (b *GetDialogsQueryBuilder) OffsetDate(offsetDate int) *GetDialogsQueryBuilder {
b.offsetDate = offsetDate
return b
}
// OffsetID sets offsetID from which iterate start.
func (b *GetDialogsQueryBuilder) OffsetID(offsetID int) *GetDialogsQueryBuilder {
b.offsetID = offsetID
return b
}
// FolderID sets FolderID field of GetDialogs query.
func (b *GetDialogsQueryBuilder) FolderID(paramFolderID int) *GetDialogsQueryBuilder {
b.req.FolderID = paramFolderID
return b
}
// Query implements Query interface.
func (b *GetDialogsQueryBuilder) Query(ctx context.Context, req Request) (tg.MessagesDialogsClass, error) {
r := &tg.MessagesGetDialogsRequest{
Limit: req.Limit,
}
r.FolderID = b.req.FolderID
r.OffsetDate = req.OffsetDate
r.OffsetID = req.OffsetID
r.OffsetPeer = req.OffsetPeer
return b.raw.MessagesGetDialogs(ctx, r)
}
// Iter returns iterator using built query.
func (b *GetDialogsQueryBuilder) Iter() *Iterator {
iter := NewIterator(b, b.batchSize)
iter = iter.OffsetDate(b.offsetDate)
iter = iter.OffsetID(b.offsetID)
return iter
}
// ForEach calls given callback on each iterator element.
func (b *GetDialogsQueryBuilder) ForEach(ctx context.Context, cb func(context.Context, Elem) error) error {
iter := b.Iter()
for iter.Next(ctx) {
if err := cb(ctx, iter.Value()); err != nil {
return err
}
}
return iter.Err()
}
// Count fetches remote state to get number of elements.
func (b *GetDialogsQueryBuilder) Count(ctx context.Context) (int, error) {
iter := b.Iter()
c, err := iter.Total(ctx)
if err != nil {
return 0, errors.Wrap(err, "get total")
}
return c, nil
}
// Collect creates iterator and collects all elements to slice.
func (b *GetDialogsQueryBuilder) Collect(ctx context.Context) ([]Elem, error) {
iter := b.Iter()
c, err := iter.Total(ctx)
if err != nil {
return nil, errors.Wrap(err, "get total")
}
r := make([]Elem, 0, c)
for iter.Next(ctx) {
r = append(r, iter.Value())
}
return r, iter.Err()
}