Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for getting the type of an update through a helper method #151

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions gen_consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,68 @@ const (
UpdateTypeRemovedChatBoost = "removed_chat_boost"
)

// GetType is a helper method to easily identify the type of update that is being received.
func (u Update) GetType() string {
switch {
case u.Message != nil:
return UpdateTypeMessage

case u.EditedMessage != nil:
return UpdateTypeEditedMessage

case u.ChannelPost != nil:
return UpdateTypeChannelPost

case u.EditedChannelPost != nil:
return UpdateTypeEditedChannelPost

case u.MessageReaction != nil:
return UpdateTypeMessageReaction

case u.MessageReactionCount != nil:
return UpdateTypeMessageReactionCount

case u.InlineQuery != nil:
return UpdateTypeInlineQuery

case u.ChosenInlineResult != nil:
return UpdateTypeChosenInlineResult

case u.CallbackQuery != nil:
return UpdateTypeCallbackQuery

case u.ShippingQuery != nil:
return UpdateTypeShippingQuery

case u.PreCheckoutQuery != nil:
return UpdateTypePreCheckoutQuery

case u.Poll != nil:
return UpdateTypePoll

case u.PollAnswer != nil:
return UpdateTypePollAnswer

case u.MyChatMember != nil:
return UpdateTypeMyChatMember

case u.ChatMember != nil:
return UpdateTypeChatMember

case u.ChatJoinRequest != nil:
return UpdateTypeChatJoinRequest

case u.ChatBoost != nil:
return UpdateTypeChatBoost

case u.RemovedChatBoost != nil:
return UpdateTypeRemovedChatBoost

default:
return "unknown"
}
}

// The consts listed below represent all the parse_mode options that can be sent to telegram.
const (
ParseModeHTML = "HTML"
Expand Down
41 changes: 29 additions & 12 deletions scripts/generate/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,35 @@ func generateUpdateTypeConsts(d APIDescription) (string, error) {
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")
// 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
}
constName := "UpdateType" + snakeToTitle(f.Name)
out.WriteString(writeConst(constName, f.Name))
out.WriteString(writeConst("UpdateType"+snakeToTitle(f.Name), f.Name))
}
out.WriteString(")\n\n")
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
}

Expand All @@ -65,8 +83,8 @@ func generateTypeConsts(d APIDescription, typeName string) (string, error) {
return "", errors.New("missing '" + typeName + "' type data")
}
out := strings.Builder{}
out.WriteString("// The consts listed below represent all the " + strings.ToLower(typeName) + " types that can be obtained from telegram.\n")
out.WriteString("const (\n")
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.
Expand All @@ -77,8 +95,7 @@ func generateTypeConsts(d APIDescription, typeName string) (string, error) {
return "", fmt.Errorf("failed to get quoted types: %w", err)
}
for _, t := range types {
constName := typeName + "Type" + snakeToTitle(t)
out.WriteString(writeConst(constName, t))
out.WriteString(writeConst(typeName+"Type"+snakeToTitle(t), t))
}
}
out.WriteString(")\n\n")
Expand All @@ -90,8 +107,8 @@ func generateParseModeConsts() string {
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")
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" {
Expand All @@ -106,5 +123,5 @@ func generateParseModeConsts() string {
}

func writeConst(name string, value string) string {
return fmt.Sprintf("%s = \"%s\"\n", name, value)
return fmt.Sprintf("\n%s = \"%s\"", name, value)
}
Loading