Skip to content

Commit

Permalink
Add missing thumbnail logic to certain inputmedia types and improve
Browse files Browse the repository at this point in the history
error messages
  • Loading branch information
PaulSonOfLars committed Jun 12, 2024
1 parent a22257a commit 5d8645c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 31 deletions.
34 changes: 17 additions & 17 deletions gen_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -2927,7 +2927,7 @@ func (bot *Bot) SendAnimation(chatId int64, animation InputFileOrString, opts *S
if animation != nil {
err := animation.Attach("animation", data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach 'animation' input file: %w", err)
}
v["animation"] = animation.getValue()
}
Expand All @@ -2948,7 +2948,7 @@ func (bot *Bot) SendAnimation(chatId int64, animation InputFileOrString, opts *S
if opts.Thumbnail != nil {
err := opts.Thumbnail.Attach("thumbnail", data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach 'thumbnail' input file: %w", err)
}
v["thumbnail"] = opts.Thumbnail.getValue()
}
Expand Down Expand Up @@ -3044,7 +3044,7 @@ func (bot *Bot) SendAudio(chatId int64, audio InputFileOrString, opts *SendAudio
if audio != nil {
err := audio.Attach("audio", data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach 'audio' input file: %w", err)
}
v["audio"] = audio.getValue()
}
Expand All @@ -3070,7 +3070,7 @@ func (bot *Bot) SendAudio(chatId int64, audio InputFileOrString, opts *SendAudio
if opts.Thumbnail != nil {
err := opts.Thumbnail.Attach("thumbnail", data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach 'thumbnail' input file: %w", err)
}
v["thumbnail"] = opts.Thumbnail.getValue()
}
Expand Down Expand Up @@ -3337,7 +3337,7 @@ func (bot *Bot) SendDocument(chatId int64, document InputFileOrString, opts *Sen
if document != nil {
err := document.Attach("document", data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach 'document' input file: %w", err)
}
v["document"] = document.getValue()
}
Expand All @@ -3349,7 +3349,7 @@ func (bot *Bot) SendDocument(chatId int64, document InputFileOrString, opts *Sen
if opts.Thumbnail != nil {
err := opts.Thumbnail.Attach("thumbnail", data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach 'thumbnail' input file: %w", err)
}
v["thumbnail"] = opts.Thumbnail.getValue()
}
Expand Down Expand Up @@ -3897,7 +3897,7 @@ func (bot *Bot) SendPhoto(chatId int64, photo InputFileOrString, opts *SendPhoto
if photo != nil {
err := photo.Attach("photo", data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach 'photo' input file: %w", err)
}
v["photo"] = photo.getValue()
}
Expand Down Expand Up @@ -4116,7 +4116,7 @@ func (bot *Bot) SendSticker(chatId int64, sticker InputFileOrString, opts *SendS
if sticker != nil {
err := sticker.Attach("sticker", data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach 'sticker' input file: %w", err)
}
v["sticker"] = sticker.getValue()
}
Expand Down Expand Up @@ -4298,7 +4298,7 @@ func (bot *Bot) SendVideo(chatId int64, video InputFileOrString, opts *SendVideo
if video != nil {
err := video.Attach("video", data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach 'video' input file: %w", err)
}
v["video"] = video.getValue()
}
Expand All @@ -4319,7 +4319,7 @@ func (bot *Bot) SendVideo(chatId int64, video InputFileOrString, opts *SendVideo
if opts.Thumbnail != nil {
err := opts.Thumbnail.Attach("thumbnail", data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach 'thumbnail' input file: %w", err)
}
v["thumbnail"] = opts.Thumbnail.getValue()
}
Expand Down Expand Up @@ -4407,7 +4407,7 @@ func (bot *Bot) SendVideoNote(chatId int64, videoNote InputFileOrString, opts *S
if videoNote != nil {
err := videoNote.Attach("video_note", data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach 'video_note' input file: %w", err)
}
v["video_note"] = videoNote.getValue()
}
Expand All @@ -4425,7 +4425,7 @@ func (bot *Bot) SendVideoNote(chatId int64, videoNote InputFileOrString, opts *S
if opts.Thumbnail != nil {
err := opts.Thumbnail.Attach("thumbnail", data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach 'thumbnail' input file: %w", err)
}
v["thumbnail"] = opts.Thumbnail.getValue()
}
Expand Down Expand Up @@ -4503,7 +4503,7 @@ func (bot *Bot) SendVoice(chatId int64, voice InputFileOrString, opts *SendVoice
if voice != nil {
err := voice.Attach("voice", data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach 'voice' input file: %w", err)
}
v["voice"] = voice.getValue()
}
Expand Down Expand Up @@ -4724,7 +4724,7 @@ func (bot *Bot) SetChatPhoto(chatId int64, photo InputFile, opts *SetChatPhotoOp
if photo != nil {
err := photo.Attach("photo", data)
if err != nil {
return false, err
return false, fmt.Errorf("failed to attach 'photo' input file: %w", err)
}
v["photo"] = photo.getValue()
}
Expand Down Expand Up @@ -5348,7 +5348,7 @@ func (bot *Bot) SetStickerSetThumbnail(name string, userId int64, format string,
if opts.Thumbnail != nil {
err := opts.Thumbnail.Attach("thumbnail", data)
if err != nil {
return false, err
return false, fmt.Errorf("failed to attach 'thumbnail' input file: %w", err)
}
v["thumbnail"] = opts.Thumbnail.getValue()
}
Expand Down Expand Up @@ -5431,7 +5431,7 @@ func (bot *Bot) SetWebhook(url string, opts *SetWebhookOpts) (bool, error) {
if opts.Certificate != nil {
err := opts.Certificate.Attach("certificate", data)
if err != nil {
return false, err
return false, fmt.Errorf("failed to attach 'certificate' input file: %w", err)
}
v["certificate"] = opts.Certificate.getValue()
}
Expand Down Expand Up @@ -5802,7 +5802,7 @@ func (bot *Bot) UploadStickerFile(userId int64, sticker InputFile, stickerFormat
if sticker != nil {
err := sticker.Attach("sticker", data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach 'sticker' input file: %w", err)
}
v["sticker"] = sticker.getValue()
}
Expand Down
40 changes: 34 additions & 6 deletions gen_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4818,7 +4818,14 @@ func (v InputMediaAnimation) InputParams(mediaName string, data map[string]FileR
if v.Media != nil {
err := v.Media.Attach(mediaName, data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach input file for %s: %w", mediaName, err)
}
}

if v.Thumbnail != nil {
err := v.Thumbnail.Attach(mediaName+"-thumbnail", data)
if err != nil {
return nil, fmt.Errorf("failed to attach 'thumbnail' input file for %s: %w", mediaName, err)
}
}

Expand Down Expand Up @@ -4892,7 +4899,14 @@ func (v InputMediaAudio) InputParams(mediaName string, data map[string]FileReade
if v.Media != nil {
err := v.Media.Attach(mediaName, data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach input file for %s: %w", mediaName, err)
}
}

if v.Thumbnail != nil {
err := v.Thumbnail.Attach(mediaName+"-thumbnail", data)
if err != nil {
return nil, fmt.Errorf("failed to attach 'thumbnail' input file for %s: %w", mediaName, err)
}
}

Expand Down Expand Up @@ -4960,7 +4974,14 @@ func (v InputMediaDocument) InputParams(mediaName string, data map[string]FileRe
if v.Media != nil {
err := v.Media.Attach(mediaName, data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach input file for %s: %w", mediaName, err)
}
}

if v.Thumbnail != nil {
err := v.Thumbnail.Attach(mediaName+"-thumbnail", data)
if err != nil {
return nil, fmt.Errorf("failed to attach 'thumbnail' input file for %s: %w", mediaName, err)
}
}

Expand Down Expand Up @@ -5028,7 +5049,7 @@ func (v InputMediaPhoto) InputParams(mediaName string, data map[string]FileReade
if v.Media != nil {
err := v.Media.Attach(mediaName, data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach input file for %s: %w", mediaName, err)
}
}

Expand Down Expand Up @@ -5111,7 +5132,14 @@ func (v InputMediaVideo) InputParams(mediaName string, data map[string]FileReade
if v.Media != nil {
err := v.Media.Attach(mediaName, data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach input file for %s: %w", mediaName, err)
}
}

if v.Thumbnail != nil {
err := v.Thumbnail.Attach(mediaName+"-thumbnail", data)
if err != nil {
return nil, fmt.Errorf("failed to attach 'thumbnail' input file for %s: %w", mediaName, err)
}
}

Expand Down Expand Up @@ -5172,7 +5200,7 @@ func (v InputSticker) InputParams(mediaName string, data map[string]FileReader)
if v.Sticker != nil {
err := v.Sticker.Attach(mediaName, data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach input file for %s: %w", mediaName, err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/generate/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ const inputFileBranch = `
if {{.GoParam}} != nil {
err := {{.GoParam}}.Attach("{{.Name}}", data)
if err != nil {
return {{.DefaultReturn}}, err
return {{.DefaultReturn}}, fmt.Errorf("failed to attach '{{.Name}}' input file: %w", err)
}
v["{{.Name}}"] = {{.GoParam}}.getValue()
}`
Expand Down
33 changes: 26 additions & 7 deletions scripts/generate/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ func generateTypeDef(d APIDescription, tgType TypeDescription) (string, error) {
if ok {
// TODO: Investigate if thumbnails need special handling too.
err = inputParamsTmpl.Execute(&typeDef, inputParamsMethodData{
Type: tgType.Name,
Field: snakeToTitle(fieldName),
Type: tgType.Name,
Field: snakeToTitle(fieldName),
Thumbnail: containsThumbnail(tgType),
})
if err != nil {
return "", fmt.Errorf("failed to generate %s inputparam methods: %w", tgType.Name, err)
Expand Down Expand Up @@ -168,6 +169,15 @@ func containsInputFile(d APIDescription, tgType TypeDescription, checked map[str
return false, "", nil
}

func containsThumbnail(tgType TypeDescription) bool {
for _, f := range tgType.Fields {
if f.Name == "thumbnail" {
return true
}
}
return false
}

func generateParentType(d APIDescription, tgType TypeDescription) (string, error) {
subTypes, err := getTypesByName(d, tgType.Subtypes)
if err != nil {
Expand Down Expand Up @@ -752,19 +762,28 @@ func (v {{.Type}}) MarshalJSON() ([]byte, error) {
`

type inputParamsMethodData struct {
Type string
Field string
Type string
Field string
Thumbnail bool
}

const inputParamsMethod = `
func (v {{.Type}}) InputParams(mediaName string, data map[string]FileReader) ([]byte, error) {
if v.{{.Field}} != nil {
err := v.{{.Field}}.Attach(mediaName, data)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to attach input file for %s: %w", mediaName, err)
}
}
}
{{ if .Thumbnail }}
if v.Thumbnail != nil {
err := v.Thumbnail.Attach(mediaName+"-thumbnail", data)
if err != nil {
return nil, fmt.Errorf("failed to attach 'thumbnail' input file for %s: %w", mediaName, err)
}
}
{{- end }}
return json.Marshal(v)
}
`

0 comments on commit 5d8645c

Please sign in to comment.