From a8e8c134bb49b0da21d17586e17ad319f6f69286 Mon Sep 17 00:00:00 2001 From: "Vasilii Morozov (Basil Morozov)" Date: Mon, 8 Apr 2024 11:50:37 +0300 Subject: [PATCH] Add UnmarshalJSON for InputFileString (#76) * add UnmarshalJSON for InputFileString * pointer recievers for inputFile implementations * fix test --- methods_test.go | 4 ++-- models/input_file.go | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/methods_test.go b/methods_test.go index 725d1fa..5a7bfd3 100644 --- a/methods_test.go +++ b/methods_test.go @@ -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) diff --git a/models/input_file.go b/models/input_file.go index d42cc7b..2c49253 100644 --- a/models/input_file.go +++ b/models/input_file.go @@ -1,6 +1,7 @@ package models import ( + "encoding/json" "io" ) @@ -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 @@ -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) +}