From c444552b5a94e3589038f186b7d9dc3f5bf7c72c Mon Sep 17 00:00:00 2001 From: Daniil Suvorov Date: Sun, 25 Feb 2024 12:12:03 +0300 Subject: [PATCH] BREAKING CHANGE: rm GroupsSectionsList instead of `.Name` use `.Title` --- object/groups.go | 111 +++--------------------------------------- object/groups_test.go | 99 ------------------------------------- 2 files changed, 6 insertions(+), 204 deletions(-) diff --git a/object/groups.go b/object/groups.go index da47be29..52f92d8d 100644 --- a/object/groups.go +++ b/object/groups.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "fmt" - "reflect" "github.com/vmihailenco/msgpack/v5" "github.com/vmihailenco/msgpack/v5/msgpcode" @@ -544,12 +543,12 @@ type GroupsGroupSettings struct { LiveCovers struct { IsEnabled BaseBoolInt `json:"is_enabled"` } `json:"live_covers"` - Market GroupsMarketInfo `json:"market"` - SectionsList []GroupsSectionsList `json:"sections_list"` - MainSection int `json:"main_section"` - SecondarySection int `json:"secondary_section"` - ActionButton GroupsActionButton `json:"action_button"` - Phone string `json:"phone"` + Market GroupsMarketInfo `json:"market"` + SectionsList []BaseObject `json:"sections_list"` + MainSection int `json:"main_section"` + SecondarySection int `json:"secondary_section"` + ActionButton GroupsActionButton `json:"action_button"` + Phone string `json:"phone"` RecognizePhoto int `json:"recognize_photo"` @@ -616,104 +615,6 @@ type GroupsYoulaSettings struct { Radiuses []float64 `json:"radiuses"` } -// GroupsSectionsList struct. -type GroupsSectionsList struct { - ID int `json:"id"` - Name string `json:"name"` -} - -// UnmarshalJSON need for unmarshal dynamic array (Example: [1, "Фотографии"]) to struct. -// -// To unmarshal JSON into a value implementing the Unmarshaler interface, -// Unmarshal calls that value's UnmarshalJSON method. -// See more https://golang.org/pkg/encoding/json/#Unmarshal -func (g *GroupsSectionsList) UnmarshalJSON(data []byte) error { - var alias []interface{} - if err := json.Unmarshal(data, &alias); err != nil { - return fmt.Errorf("object.GroupsSectionsList: %w", err) - } - - if len(alias) != 2 { - return &json.UnmarshalTypeError{ - Value: string(data), - Type: reflect.TypeOf((*GroupsSectionsList)(nil)), - } - } - - // default concrete Go type float64 for JSON numbers - id, ok := alias[0].(float64) - if !ok { - return &json.UnmarshalTypeError{ - Value: string(data), - Type: reflect.TypeOf((*GroupsSectionsList)(nil)), - Struct: "GroupsSectionsList", - Field: "ID", - } - } - - name, ok := alias[1].(string) - if !ok { - return &json.UnmarshalTypeError{ - Value: string(data), - Type: reflect.TypeOf((*GroupsSectionsList)(nil)), - Struct: "GroupsSectionsList", - Field: "Name", - } - } - - g.ID = int(id) - g.Name = name - - return nil -} - -// DecodeMsgpack need for decode dynamic array (Example: [1, "Фотографии"]) to struct. -func (g *GroupsSectionsList) DecodeMsgpack(dec *msgpack.Decoder) error { - data, err := dec.DecodeRaw() - if err != nil { - return fmt.Errorf("object.GroupsSectionsList: %w", err) - } - - var alias []interface{} - - err = msgpack.Unmarshal(data, &alias) - if err != nil { - return fmt.Errorf("object.GroupsSectionsList: %w", err) - } - - if len(alias) != 2 { - return &json.UnmarshalTypeError{ - Value: string(data), - Type: reflect.TypeOf((*GroupsSectionsList)(nil)), - } - } - - id, ok := alias[0].(int8) - if !ok { - return &json.UnmarshalTypeError{ - Value: string(data), - Type: reflect.TypeOf((*GroupsSectionsList)(nil)), - Struct: "GroupsSectionsList", - Field: "ID", - } - } - - name, ok := alias[1].(string) - if !ok { - return &json.UnmarshalTypeError{ - Value: string(data), - Type: reflect.TypeOf((*GroupsSectionsList)(nil)), - Struct: "GroupsSectionsList", - Field: "Name", - } - } - - g.ID = int(id) - g.Name = name - - return nil -} - // GroupsActionType for action_button in groups. type GroupsActionType string diff --git a/object/groups_test.go b/object/groups_test.go index e0c3ced4..8f84ef20 100644 --- a/object/groups_test.go +++ b/object/groups_test.go @@ -87,105 +87,6 @@ func TestGroupsGroupXtrInvitedBy_ToMention(t *testing.T) { ) } -func TestGroupsSectionsList_UnmarshalJSON(t *testing.T) { - t.Parallel() - - f := func(data []byte, want object.GroupsSectionsList, wanErr bool) { - t.Helper() - - var got object.GroupsSectionsList - - err := got.UnmarshalJSON(data) - - if wanErr { - assert.Error(t, err) - } else { - assert.NoError(t, err) - assert.Equal(t, want, got) - } - } - - f( - []byte(`[123, "test"]`), - object.GroupsSectionsList{ID: 123, Name: "test"}, - false, - ) - - // Errors: - f( - []byte(`123`), - object.GroupsSectionsList{}, - true, - ) - f( - []byte(`[123, "123", "123"]`), - object.GroupsSectionsList{}, - true, - ) - f( - []byte(`["123", "123"]`), - object.GroupsSectionsList{}, - true, - ) - f( - []byte(`[123, 123]`), - object.GroupsSectionsList{}, - true, - ) -} - -func TestGroupsSectionsList_DecodeMsgpack(t *testing.T) { - t.Parallel() - - f := func(data []byte, want object.GroupsSectionsList, wanErr bool) { - t.Helper() - - var got object.GroupsSectionsList - - err := msgpack.Unmarshal(data, &got) - - if wanErr { - assert.Error(t, err) - } else { - assert.NoError(t, err) - assert.Equal(t, want, got) - } - } - - f( - []byte{0x92, 0x7B, 0xA4, 0x74, 0x65, 0x73, 0x74}, - object.GroupsSectionsList{ID: 123, Name: "test"}, - false, - ) - - // Errors: - f( - []byte{0x7B}, - object.GroupsSectionsList{}, - true, - ) - f( - []byte{0x93, 0x7B, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33}, - object.GroupsSectionsList{}, - true, - ) - f( - []byte{0x92, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33}, - object.GroupsSectionsList{}, - true, - ) - f( - []byte{0x92, 0x7B, 0x7B}, - object.GroupsSectionsList{}, - true, - ) - f( - nil, - object.GroupsSectionsList{}, - true, - ) -} - func TestGroupsLongPollServer_GetURL(t *testing.T) { t.Parallel()