-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
constraint.go
113 lines (98 loc) · 2.65 KB
/
constraint.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
package peer
import (
"context"
"fmt"
"golang.org/x/xerrors"
"github.com/gotd/td/tg"
)
// PromiseDecorator is a decorator of peer promise.
type PromiseDecorator = func(Promise) Promise
// ConstraintError is a peer resolve constraint error.
type ConstraintError struct {
Expected string
Got tg.InputPeerClass
}
// Error implements error.
func (c *ConstraintError) Error() string {
return fmt.Sprintf("expected %q, got %T", c.Expected, c.Got)
}
func tryUnpackConstraint(p tg.InputPeerClass, resolveErr error) (tg.InputPeerClass, error) {
var constraintErr *ConstraintError
if xerrors.As(resolveErr, &constraintErr) {
return constraintErr.Got, nil
}
return p, resolveErr
}
// OnlyChannel returns Promise which returns error if resolved peer is not a channel.
func OnlyChannel(p Promise) Promise {
return func(ctx context.Context) (tg.InputPeerClass, error) {
resolved, err := tryUnpackConstraint(p(ctx))
if err != nil {
return nil, err
}
switch resolved.(type) {
case *tg.InputPeerChannel, *tg.InputPeerChannelFromMessage:
return resolved, nil
default:
return nil, &ConstraintError{
Expected: "channel",
Got: resolved,
}
}
}
}
// OnlyChat returns Promise which returns error if resolved peer is not a chat.
func OnlyChat(p Promise) Promise {
return func(ctx context.Context) (tg.InputPeerClass, error) {
resolved, err := tryUnpackConstraint(p(ctx))
if err != nil {
return nil, err
}
switch resolved.(type) {
case *tg.InputPeerChat:
return resolved, nil
default:
return nil, &ConstraintError{
Expected: "chat",
Got: resolved,
}
}
}
}
// OnlyUser returns Promise which returns error if resolved peer is not a user.
func OnlyUser(p Promise) Promise {
return func(ctx context.Context) (tg.InputPeerClass, error) {
resolved, err := tryUnpackConstraint(p(ctx))
if err != nil {
return nil, err
}
switch resolved.(type) {
case *tg.InputPeerUser, *tg.InputPeerUserFromMessage, *tg.InputPeerSelf:
return resolved, nil
default:
return nil, &ConstraintError{
Expected: "user",
Got: resolved,
}
}
}
}
// OnlyUserID returns Promise which returns error if resolved peer is not a user object with ID.
// Unlike OnlyUser, it returns error if resolved peer is tg.InputPeerSelf.
func OnlyUserID(p Promise) Promise {
return func(ctx context.Context) (tg.InputPeerClass, error) {
resolved, err := tryUnpackConstraint(p(ctx))
if err != nil {
return nil, err
}
switch resolved.(type) {
case *tg.InputPeerUser, *tg.InputPeerUserFromMessage:
return resolved, nil
default:
return nil, &ConstraintError{
Expected: "userID",
Got: resolved,
}
}
}
}