Skip to content

Commit

Permalink
Add UnmarshalJSON for InputFileString (#76)
Browse files Browse the repository at this point in the history
* add UnmarshalJSON for InputFileString

* pointer recievers for inputFile implementations

* fix test
  • Loading branch information
Vasilesk authored Apr 8, 2024
1 parent 0a8269c commit a8e8c13
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,11 @@ func TestBot_Methods(t *testing.T) {

t.Run("SendVideoNote", func(t *testing.T) {
c := &httpClient{t: t, resp: `{"text":"bar"}`, reqFields: map[string]string{
"thumbnail": `{"Data":"foo"}`,
"thumbnail": `foo`,
}}
b := &Bot{client: c}
resp, err := b.SendVideoNote(context.Background(), &SendVideoNoteParams{
Thumbnail: models.InputFileString{Data: "foo"},
Thumbnail: &models.InputFileString{Data: "foo"},
})
assertNoErr(t, err)
assertEqualString(t, "bar", resp.Text)
Expand Down
9 changes: 7 additions & 2 deletions models/input_file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models

import (
"encoding/json"
"io"
)

Expand All @@ -16,7 +17,7 @@ type InputFileUpload struct {
Data io.Reader
}

func (InputFileUpload) inputFileTag() {}
func (*InputFileUpload) inputFileTag() {}

func (i *InputFileUpload) MarshalJSON() ([]byte, error) {
return []byte(`"@` + i.Filename + `"`), nil
Expand All @@ -26,8 +27,12 @@ type InputFileString struct {
Data string
}

func (InputFileString) inputFileTag() {}
func (*InputFileString) inputFileTag() {}

func (i *InputFileString) MarshalJSON() ([]byte, error) {
return []byte(`"` + i.Data + `"`), nil
}

func (i *InputFileString) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &i.Data)
}

0 comments on commit a8e8c13

Please sign in to comment.