-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
113 lines (93 loc) · 2.28 KB
/
context.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 tgot
import (
stdcontext "context"
"github.com/karalef/tgot/api"
)
// Context represents any context.
type Context[T BaseContext] interface {
BaseContext
// Base copies the context without data.
Base() Empty
// WithName copies the context with nested name.
WithName(name string) T
}
// BaseContext represents base context.
type BaseContext interface {
stdcontext.Context
// Path returns context path.
Path() string
// Bot returns bot instance.
Bot() *Bot
ctx() *context
}
// Empty represents empty context.
type Empty interface {
Context[Empty]
}
// NewContext creates new context.
func (b *Bot) NewContext(ctx stdcontext.Context, name string) Empty {
return newContext(ctx, name, b, nil)
}
func newContext(c stdcontext.Context, path string, bot *Bot, data *api.Data) *context {
if data != nil {
cp := data.Copy()
data = cp
}
return &context{c, bot, path, data}
}
func nestPath(path string, name string) string {
if path == "" {
return name
}
if name == "" {
return path
}
return path + "::" + name
}
var _ Empty = &context{}
type context struct {
stdcontext.Context
bot *Bot
path string
data *api.Data
}
func (c *context) ctx() *context { return c }
func (c *context) Bot() *Bot { return c.bot }
func (c *context) Path() string { return c.path }
func (c *context) Base() Empty { return c.with(nil) }
func (c *context) WithName(name string) Empty { return c.child(name) }
func (c *context) with(d *api.Data) *context {
if d == nil && c.data == nil {
return c
}
return newContext(c.Context, c.path, c.bot, d)
}
func (c *context) add(d *api.Data) *context {
if d == nil {
return c
}
return newContext(c.Context, c.path, c.bot, c.data.WriteTo(d))
}
func (c *context) child(name string) *context {
if name == "" {
return c
}
return newContext(c.Context, nestPath(c.path, name), c.bot, c.data)
}
func (c *context) method(meth string, d ...*api.Data) error {
_, err := method[api.Empty](c, meth, d...)
return err
}
func method[T any](c BaseContext, method string, d ...*api.Data) (T, error) {
var data *api.Data
if len(d) > 0 {
data = d[0]
}
ctxData := c.ctx().data
if data == nil {
data = ctxData
} else {
ctxData.WriteTo(data)
}
return api.Request[T](c, c.Bot().API(), method, data)
}