Skip to content

Commit

Permalink
BREAKING CHANGE: rm GroupsSectionsList
Browse files Browse the repository at this point in the history
instead of `.Name` use `.Title`
  • Loading branch information
SevereCloud committed Feb 25, 2024
1 parent bb15d8e commit c444552
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 204 deletions.
111 changes: 6 additions & 105 deletions object/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"reflect"

"github.com/vmihailenco/msgpack/v5"
"github.com/vmihailenco/msgpack/v5/msgpcode"
Expand Down Expand Up @@ -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"`

Expand Down Expand Up @@ -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

Expand Down
99 changes: 0 additions & 99 deletions object/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit c444552

Please sign in to comment.