Skip to content

Commit

Permalink
Add a set of consts that are exposed by the Bot API (#62)
Browse files Browse the repository at this point in the history
* Add support for const generation

* generate new constants

* Use ParseModeNone instead of ParseModeText
  • Loading branch information
PaulSonOfLars authored Sep 26, 2022
1 parent 425e17d commit 18fa665
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 1 deletion.
37 changes: 37 additions & 0 deletions gen_consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// THIS FILE IS AUTOGENERATED. DO NOT EDIT.
// Regen by running 'go generate' in the repo root.

package gotgbot

// The consts listed below represent all the update types that can be requested from telegram.
const (
UpdateTypeMessage = "message"
UpdateTypeEditedMessage = "edited_message"
UpdateTypeChannelPost = "channel_post"
UpdateTypeEditedChannelPost = "edited_channel_post"
UpdateTypeInlineQuery = "inline_query"
UpdateTypeChosenInlineResult = "chosen_inline_result"
UpdateTypeCallbackQuery = "callback_query"
UpdateTypeShippingQuery = "shipping_query"
UpdateTypePreCheckoutQuery = "pre_checkout_query"
UpdateTypePoll = "poll"
UpdateTypePollAnswer = "poll_answer"
UpdateTypeMyChatMember = "my_chat_member"
UpdateTypeChatMember = "chat_member"
UpdateTypeChatJoinRequest = "chat_join_request"
)

// The consts listed below represent all the parse_mode options that can be sent to telegram.
const (
ParseModeHTML = "HTML"
ParseModeMarkdownV2 = "MarkdownV2"
ParseModeMarkdown = "Markdown"
ParseModeNone = ""
)

// The consts listed below represent all the sticker types that can be obtained from telegram.
const (
StickerTypeRegular = "regular"
StickerTypeMask = "mask"
StickerTypeCustomEmoji = "custom_emoji"
)
27 changes: 27 additions & 0 deletions scripts/generate/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"regexp"
"sort"
Expand Down Expand Up @@ -240,3 +241,29 @@ func getTypesByName(d APIDescription, typeNames []string) ([]TypeDescription, er

return types, nil
}

// extractQuotedValues is a very basic quote extraction method. It only works on normal double quotes ("), it does not
// handle any sort of escaping, and it expects an even number of quotes to function.
// But that's all we need for this package, and so we're happy.
func extractQuotedValues(in string) ([]string, error) {
if strings.Count(in, "\"")%2 != 0 {
return nil, errors.New("uneven number of quotes in string")
}

var out []string
startIdx := -1
for idx, r := range in {
if r == '"' {
if startIdx == -1 {
// This is an opening quote
startIdx = idx
continue
}

// This is a closing quote, so append to outputs and reset startIdx.
out = append(out, in[startIdx+1:idx])
startIdx = -1
}
}
return out, nil
}
104 changes: 104 additions & 0 deletions scripts/generate/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
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 := generateStickerTypeConsts(d)
if err != nil {
return fmt.Errorf("failed to generate consts for sticker types: %w", err)
}
consts.WriteString(stickerTypeConsts)

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{}
out.WriteString("// The consts listed below represent all the update types that can be requested from telegram.\n")
out.WriteString("const (\n")
for _, f := range updType.Fields {
if f.Required {
// All the update types are optional, so skip required values.
continue
}
constName := "UpdateType" + snakeToTitle(f.Name)
out.WriteString(writeConst(constName, f.Name))
}
out.WriteString(")\n\n")
return out.String(), nil
}

func generateStickerTypeConsts(d APIDescription) (string, error) {
updType, ok := d.Types["Sticker"]
if !ok {
return "", errors.New("missing 'Sticker' type data")
}
out := strings.Builder{}
out.WriteString("// The consts listed below represent all the sticker types that can be obtained from telegram.\n")
out.WriteString("const (\n")
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 {
constName := "StickerType" + snakeToTitle(t)
out.WriteString(writeConst(constName, 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("// The consts listed below represent all the parse_mode options that can be sent to telegram.\n")
out.WriteString("const (\n")
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("%s = \"%s\"\n", name, value)
}
4 changes: 4 additions & 0 deletions scripts/generate/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ func generate(d APIDescription) error {
return fmt.Errorf("failed to generate helpers: %w", err)
}

if err := generateConsts(d); err != nil {
return fmt.Errorf("failed to generate consts: %w", err)
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/generate/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package gotgbot

helper, err := generateHelperDef(d, tgMethod)
if err != nil {
return fmt.Errorf("failed to generate helpersfor %s: %w", tgMethodName, err)
return fmt.Errorf("failed to generate helpers for %s: %w", tgMethodName, err)
}

if helper == "" {
Expand Down

0 comments on commit 18fa665

Please sign in to comment.