-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbutton.go
66 lines (47 loc) · 1.7 KB
/
button.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
package telebot
import "reflect"
// Row Represents a row of Buttons
// Maximum number of Buttons in a ReplyMarkup is 100 (telegram limits).
type Row []Button
// Button is the interface for all buttons
// It is implemented by InlineKeyboardButton and KeyboardButton
type Button interface {
Button()
String() string
MarshalJSON() ([]byte, error)
UnmarshalJSON(data []byte) error
deepEqual(other any) bool // private for internal use
GetText() string
SetText(string)
Clone() Button
}
// Button is here so InlineKeyboardButton implements Button
func (*InlineKeyboardButton) Button() {}
// Button is here so InlineKeyboardButton implements Button
func (*KeyboardButton) Button() {}
func (i *InlineKeyboardButton) GetText() string { return i.Text }
func (i *InlineKeyboardButton) SetText(t string) { i.Text = t }
func (i *KeyboardButton) GetText() string { return i.Text }
func (i *KeyboardButton) SetText(t string) { i.Text = t }
func (i *InlineKeyboardButton) Clone() Button {
clone := reflect.New(reflect.TypeOf(i).Elem()).Interface()
// Use reflection to copy the fields
valueOfReceiver := reflect.ValueOf(i).Elem()
valueOfClone := reflect.ValueOf(clone).Elem()
for i := 0; i < valueOfReceiver.NumField(); i++ {
field := valueOfReceiver.Field(i)
valueOfClone.Field(i).Set(field)
}
return clone.(Button)
}
func (i *KeyboardButton) Clone() Button {
clone := reflect.New(reflect.TypeOf(i).Elem()).Interface()
// Use reflection to copy the fields
valueOfReceiver := reflect.ValueOf(i).Elem()
valueOfClone := reflect.ValueOf(clone).Elem()
for i := 0; i < valueOfReceiver.NumField(); i++ {
field := valueOfReceiver.Field(i)
valueOfClone.Field(i).Set(field)
}
return clone.(Button)
}