PaulSonOfLars / gotgbot

Autogenerated Go wrapper for the telegram API. Inspired by the python-telegram-bot library.
MIT License
509 stars 114 forks source link

Add support for auto-generating ChatTypes constants. #79

Closed ALiwoto closed 1 year ago

ALiwoto commented 1 year ago

What

Since we already have UpdateType, ParseMode and StickerType constants, I thought it's better we have ChatType constants as well (this makes it easy to do comparison on Chat.Type field on custom filters/handlers for us)

Impact

ALiwoto commented 1 year ago

Also, @PaulSonOfLars, as for the code refactoring that you and @vassilit mentioned, I can suggest the following function:


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("// The consts listed below represent all the " + typeName + " 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 := typeName + "Type" + snakeToTitle(t)
            out.WriteString(writeConst(constName, t))
        }
    }
    out.WriteString(")\n\n")
    return out.String(), nil
}

Then we can use it like this in here:

    updateConsts, err := generateTypeConsts(d, "Update")
    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)

So, instead of defining a separated function for each of them, we can just simply pass the name of the type consts we would like to get, by only defining one single function.

PaulSonOfLars commented 1 year ago

@ALiwoto Looks good to me, feel free to refactor! Might be worth lowercasing the comment though, so it doesn't look odd in the comment. Oh, and renaming the variable from updType to something like typeData or similar, since its not just Update. (that one is entirely my fault, i didnt change it when i added the sticker func!)

out.WriteString("// The consts listed below represent all the " + strings.ToLower(typeName) + " types that can be obtained from telegram.\n")

And by the way - I tihnk your refactor is invalid for the Updatetype. That check just iterates over all update fields, it DOESNT look at the "type" field like sticker and chat do.

ALiwoto commented 1 year ago

@ALiwoto Looks good to me, feel free to refactor! Might be worth lowercasing the comment though, so it doesn't look odd in the comment. Oh, and renaming the variable from updType to something like typeData or similar, since its not just Update. (that one is entirely my fault, i didnt change it when i added the sticker func!)

out.WriteString("// The consts listed below represent all the " + strings.ToLower(typeName) + " types that can be obtained from telegram.\n")

And by the way - I tihnk your refactor is invalid for the Updatetype. That check just iterates over all update fields, it DOESNT look at the "type" field like sticker and chat do.

Yes, I just tested that, doesn't work for Update, sadly.

ALiwoto commented 1 year ago

There is also another way around for that, but it will kinda make code ugly (and a bit complicated to read), so I think we just better have separated functions for Update consts.