Closed ALiwoto closed 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.
@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 Update
type. That check just iterates over all update fields, it DOESNT look at the "type" field like sticker and chat do.
@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 liketypeData
or similar, since its not justUpdate
. (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
Update
type. 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.
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.
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 onChat.Type
field on custom filters/handlers for us)Impact