-
Notifications
You must be signed in to change notification settings - Fork 99
/
consts.go
127 lines (111 loc) · 3.76 KB
/
consts.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
package main
import (
"errors"
"fmt"
"strings"
)
func generateConsts(d APIDescription) error {
consts := strings.Builder{}
consts.WriteString(`
// THIS FILE IS AUTOGENERATED. DO NOT EDIT.
// Regen by running 'go generate' in the repo root.
package gotgbot
`)
updateConsts, err := generateUpdateTypeConsts(d)
if err != nil {
return fmt.Errorf("failed to generate consts for update types: %w", err)
}
consts.WriteString(updateConsts)
consts.WriteString(generateParseModeConsts())
stickerTypeConsts, err := generateTypeConsts(d, "Sticker")
if err != nil {
return fmt.Errorf("failed to generate consts for sticker types: %w", err)
}
consts.WriteString(stickerTypeConsts)
chatTypeConsts, err := generateTypeConsts(d, "Chat")
if err != nil {
return fmt.Errorf("failed to generate consts for chat types: %w", err)
}
consts.WriteString(chatTypeConsts)
return writeGenToFile(consts, "gen_consts.go")
}
func generateUpdateTypeConsts(d APIDescription) (string, error) {
updType, ok := d.Types["Update"]
if !ok {
return "", errors.New("missing 'Update' type data")
}
out := strings.Builder{}
// First we generate the consts
out.WriteString("\n// The consts listed below represent all the update types that can be requested from telegram.")
out.WriteString("\nconst (")
for _, f := range updType.Fields {
if f.Required {
// All the update types are optional, so skip required values.
continue
}
out.WriteString(writeConst("UpdateType"+snakeToTitle(f.Name), f.Name))
}
out.WriteString(")\n")
// Then we generate the helper for the update kind logic
out.WriteString("\n// GetType is a helper method to easily identify the type of update that is being received.")
out.WriteString("\nfunc (u Update) GetType() string {")
out.WriteString("\nswitch {")
for _, f := range updType.Fields {
if f.Required {
// All the update types are optional, so skip required values.
continue
}
out.WriteString(fmt.Sprintf("\ncase u.%s != nil:", snakeToTitle(f.Name)))
out.WriteString("\nreturn UpdateType" + snakeToTitle(f.Name) + "\n")
}
out.WriteString("\ndefault:")
out.WriteString("\nreturn \"unknown\"")
out.WriteString("\n}")
out.WriteString("\n}")
return out.String(), nil
}
func generateTypeConsts(d APIDescription, typeName string) (string, error) {
updType, ok := d.Types[typeName]
if !ok {
return "", errors.New("missing '" + typeName + "' type data")
}
out := strings.Builder{}
out.WriteString("\n// The consts listed below represent all the " + strings.ToLower(typeName) + " types that can be obtained from telegram.")
out.WriteString("\nconst (")
for _, f := range updType.Fields {
if f.Name != "type" {
// the field we want to look at is called "type", ignore all others.
continue
}
types, err := extractQuotedValues(f.Description)
if err != nil {
return "", fmt.Errorf("failed to get quoted types: %w", err)
}
for _, t := range types {
out.WriteString(writeConst(typeName+"Type"+snakeToTitle(t), t))
}
}
out.WriteString(")\n\n")
return out.String(), nil
}
func generateParseModeConsts() string {
// Adding these manually because they're not part of the spec, and theyre not going to change much anyway.
formattingTypes := []string{"HTML", "MarkdownV2", "Markdown", "None"}
out := strings.Builder{}
out.WriteString("\n// The consts listed below represent all the parse_mode options that can be sent to telegram.")
out.WriteString("\nconst (")
for _, t := range formattingTypes {
constName := "ParseMode" + t
if t == "None" {
// no parsemode == empty string value.
out.WriteString(writeConst(constName, ""))
continue
}
out.WriteString(writeConst(constName, t))
}
out.WriteString(")\n\n")
return out.String()
}
func writeConst(name string, value string) string {
return fmt.Sprintf("\n%s = \"%s\"", name, value)
}