-
Notifications
You must be signed in to change notification settings - Fork 0
/
types_keyboards.py
executable file
·73 lines (58 loc) · 1.66 KB
/
types_keyboards.py
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
#!/usr/bin/env python
header = '''package telegram
// THIS FILE IS GENERATED AUTOMATICALLY. DO NOT CHANGE IT
import "encoding/json"
'''
keyboard_types = [
'ReplyKeyboardMarkup',
'ReplyKeyboardRemove',
'InlineKeyboardMarkup',
'ForceReply',
]
alias_types = ['alias' + typ for typ in keyboard_types]
alias_template = '''type {alias_type} {keyboard_type}
'''
methods_template = '''
// MarshalJSON implements json.Marshaler interface.
func (m *{keyboard_type}) MarshalJSON() ([]byte, error) {
a := (*{alias_type})(m)
b, err := json.Marshal(a)
if err != nil {
return nil, err
}
return json.Marshal(string(b))
}
// UnmarshalJSON implements json.Unmarshaler interface.
func (m *{keyboard_type}) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
var a {alias_type}
if err := json.Unmarshal([]byte(s), &a); err != nil {
return err
}
*m = ({keyboard_type})(a)
return nil
}
'''
def replace(template, replacements):
s = template
for key, value in replacements.items():
s = s.replace(key, value)
return s
def main():
with open('types_keyboards.go', 'w') as f:
f.write(header)
for typ, alias in zip(keyboard_types, alias_types):
f.write(replace(alias_template, {
'{keyboard_type}': typ,
'{alias_type}': alias,
}))
for typ, alias in zip(keyboard_types, alias_types):
f.write(replace(methods_template, {
'{keyboard_type}': typ,
'{alias_type}': alias,
}))
if __name__ == '__main__':
main()