Skip to content

Commit

Permalink
add Marshal functions for struct with many types
Browse files Browse the repository at this point in the history
- ChatBoostSource
- ChatBackground
- ChatMember
- MenuButton
- MaybeInaccessibleMessage
- ReactionType
- MessageOrigin
  • Loading branch information
negasus committed May 22, 2024
1 parent 267a217 commit 95b8f8c
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 146 deletions.
2 changes: 1 addition & 1 deletion methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ func TestBot_Methods(t *testing.T) {
ChatID: 123,
})
assertNoErr(t, err)
assertEqualInt(t, int(models.ChatMemberTypeAdministrator), int(resp.Type))
assertEqualString(t, string(models.ChatMemberTypeAdministrator), string(resp.Type))
})

t.Run("SetChatStickerSet", func(t *testing.T) {
Expand Down
44 changes: 30 additions & 14 deletions models/boost.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@ type ChatBoostSource struct {

func (cbs *ChatBoostSource) UnmarshalJSON(data []byte) error {
v := struct {
Source string `json:"source"`
Source ChatBoostSourceType `json:"source"`
}{}
err := json.Unmarshal(data, &v)
if err != nil {
return err
}

switch v.Source {
case "premium":
case ChatBoostSourceTypePremium:
cbs.Source = ChatBoostSourceTypePremium
cbs.ChatBoostSourcePremium = &ChatBoostSourcePremium{}
return json.Unmarshal(data, cbs.ChatBoostSourcePremium)
case "gift_code":
case ChatBoostSourceTypeGiftCode:
cbs.Source = ChatBoostSourceTypeGiftCode
cbs.ChatBoostSourceGiftCode = &ChatBoostSourceGiftCode{}
return json.Unmarshal(data, cbs.ChatBoostSourceGiftCode)
case "giveaway":
case ChatBoostSourceTypeGiveaway:
cbs.Source = ChatBoostSourceTypeGiveaway
cbs.ChatBoostSourceGiveaway = &ChatBoostSourceGiveaway{}
return json.Unmarshal(data, cbs.ChatBoostSourceGiveaway)
Expand All @@ -73,29 +73,45 @@ func (cbs *ChatBoostSource) UnmarshalJSON(data []byte) error {
return fmt.Errorf("unsupported ChatBoostSource type")
}

func (cbs *ChatBoostSource) MarshalJSON() ([]byte, error) {
switch cbs.Source {
case ChatBoostSourceTypePremium:
cbs.ChatBoostSourcePremium.Source = ChatBoostSourceTypePremium
return json.Marshal(cbs.ChatBoostSourcePremium)
case ChatBoostSourceTypeGiftCode:
cbs.ChatBoostSourceGiftCode.Source = ChatBoostSourceTypeGiftCode
return json.Marshal(cbs.ChatBoostSourceGiftCode)
case ChatBoostSourceTypeGiveaway:
cbs.ChatBoostSourceGiveaway.Source = ChatBoostSourceTypeGiveaway
return json.Marshal(cbs.ChatBoostSourceGiveaway)
}

return nil, fmt.Errorf("unsupported ChatBoostSource type")
}

// ChatBoostSourceType https://core.telegram.org/bots/api#chatboostsource
type ChatBoostSourceType int
type ChatBoostSourceType string

const (
ChatBoostSourceTypePremium ChatBoostSourceType = iota
ChatBoostSourceTypeGiftCode
ChatBoostSourceTypeGiveaway
ChatBoostSourceTypePremium ChatBoostSourceType = "premium"
ChatBoostSourceTypeGiftCode ChatBoostSourceType = "gift_code"
ChatBoostSourceTypeGiveaway ChatBoostSourceType = "giveaway"
)

// ChatBoostSourcePremium https://core.telegram.org/bots/api#chatboostsourcepremium
type ChatBoostSourcePremium struct {
Source string `json:"source"` // always “premium”
User User `json:"user"`
Source ChatBoostSourceType `json:"source"` // always “premium”
User User `json:"user"`
}

// ChatBoostSourceGiftCode https://core.telegram.org/bots/api#chatboostsourcegiftcode
type ChatBoostSourceGiftCode struct {
Source string `json:"source"` // always “gift_code”
User User `json:"user"`
Source ChatBoostSourceType `json:"source"` // always “gift_code”
User User `json:"user"`
}

// ChatBoostSourceGiveaway https://core.telegram.org/bots/api#chatboostsourcegiveaway
type ChatBoostSourceGiveaway struct {
Source string `json:"source"` // always “giveaway”
User User `json:"user"`
Source ChatBoostSourceType `json:"source"` // always “giveaway”
User User `json:"user"`
}
45 changes: 45 additions & 0 deletions models/boost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,48 @@ func TestChatBoostSource_giveaway(t *testing.T) {
t.Fatal("invalid user id")
}
}

func TestChatBoostSource_premium_marshal_unmarshal(t *testing.T) {
src := `{"source":"premium","user":{"id":123}}`

cbs := &ChatBoostSource{}
err := json.Unmarshal([]byte(src), cbs)
if err != nil {
t.Fatal(err)
}

if cbs.Source != ChatBoostSourceTypePremium {
t.Fatal("invalid type")
}

if cbs.ChatBoostSourcePremium == nil {
t.Fatal("invalid ChatBoostSourcePremium")
}

if cbs.ChatBoostSourcePremium.User.ID != 123 {
t.Fatal("invalid user id")
}

src2, err2 := json.Marshal(cbs)
if err2 != nil {
t.Fatal(err2)
}

var cbs2 ChatBoostSource
err = json.Unmarshal(src2, &cbs2)
if err != nil {
t.Fatal(err)
}

if cbs2.Source != ChatBoostSourceTypePremium {
t.Fatal("invalid type")
}

if cbs2.ChatBoostSourcePremium == nil {
t.Fatal("invalid ChatBoostSourcePremium")
}

if cbs2.ChatBoostSourcePremium.User.ID != 123 {
t.Fatal("invalid user id")
}
}
82 changes: 62 additions & 20 deletions models/chat_background.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,26 @@ type ChatBackground struct {

func (cb *ChatBackground) UnmarshalJSON(data []byte) error {
v := struct {
Type string `json:"type"`
Type BackgroundType `json:"type"`
}{}
if err := json.Unmarshal(data, &v); err != nil {
return err
}

switch v.Type {
case "fill":
case ChatBackgroundTypeFill:
cb.Type = ChatBackgroundTypeFill
cb.Fill = &BackgroundTypeFill{}
return json.Unmarshal(data, cb.Fill)
case "wallpaper":
case ChatBackgroundTypeWallpaper:
cb.Type = ChatBackgroundTypeWallpaper
cb.Wallpaper = &BackgroundTypeWallpaper{}
return json.Unmarshal(data, cb.Wallpaper)
case "pattern":
case ChatBackgroundTypePattern:
cb.Type = ChatBackgroundTypePattern
cb.Pattern = &BackgroundTypePattern{}
return json.Unmarshal(data, cb.Pattern)
case "chat_theme":
case ChatBackgroundTypeChatTheme:
cb.Type = ChatBackgroundTypeChatTheme
cb.Theme = &BackgroundTypeChatTheme{}
return json.Unmarshal(data, cb.Theme)
Expand All @@ -54,22 +54,44 @@ func (cb *ChatBackground) UnmarshalJSON(data []byte) error {
return fmt.Errorf("unsupported ChatBackground type")
}

func (cb *ChatBackground) MarshalJSON() ([]byte, error) {
switch cb.Type {
case ChatBackgroundTypeFill:
cb.Fill.Type = ChatBackgroundTypeFill
return json.Marshal(cb.Fill)
case ChatBackgroundTypeWallpaper:
cb.Wallpaper.Type = ChatBackgroundTypeWallpaper
return json.Marshal(cb.Wallpaper)
case ChatBackgroundTypePattern:
cb.Pattern.Type = ChatBackgroundTypePattern
return json.Marshal(cb.Pattern)
case ChatBackgroundTypeChatTheme:
cb.Theme.Type = ChatBackgroundTypeChatTheme
return json.Marshal(cb.Theme)
}

return nil, fmt.Errorf("unsupported ChatBackground type")
}

// BackgroundTypeFill https://core.telegram.org/bots/api#backgroundtypefill
type BackgroundTypeFill struct {
Type BackgroundType `json:"type"`
Fill BackgroundFill `json:"fill"`
DarkThemeDimming int `json:"dark_theme_dimming"`
}

// BackgroundTypeWallpaper https://core.telegram.org/bots/api#backgroundtypewallpaper
type BackgroundTypeWallpaper struct {
Document Document `json:"document"`
DarkThemeDimming int `json:"dark_theme_dimming"`
IsBlurred bool `json:"is_blurred,omitempty"`
IsMoving bool `json:"is_moving,omitempty"`
Type BackgroundType `json:"type"`
Document Document `json:"document"`
DarkThemeDimming int `json:"dark_theme_dimming"`
IsBlurred bool `json:"is_blurred,omitempty"`
IsMoving bool `json:"is_moving,omitempty"`
}

// BackgroundTypePattern https://core.telegram.org/bots/api#backgroundtypepattern
type BackgroundTypePattern struct {
Type BackgroundType `json:"type"`
Document Document `json:"document"`
Fill BackgroundFill `json:"fill"`
Intensity int `json:"intensity"`
Expand All @@ -79,7 +101,8 @@ type BackgroundTypePattern struct {

// BackgroundTypeChatTheme https://core.telegram.org/bots/api#backgroundtypechattheme
type BackgroundTypeChatTheme struct {
ThemeName string `json:"theme_name"`
Type BackgroundType `json:"type"`
ThemeName string `json:"theme_name"`
}

type BackgroundFillType string
Expand All @@ -91,30 +114,30 @@ const (
)

type BackgroundFill struct {
Type BackgroundFillType `json:"type"`
Type BackgroundFillType
Solid *BackgroundFillSolid
Gradient *BackgroundFillGradient
FreeformGradient *BackgroundFillFreeformGradient
}

func (bf *BackgroundFill) UnmarshalJSON(data []byte) error {
v := struct {
Type string `json:"type"`
Type BackgroundFillType `json:"type"`
}{}
if err := json.Unmarshal(data, &v); err != nil {
return err
}

switch v.Type {
case "solid":
case BackgroundFillTypeSolid:
bf.Type = BackgroundFillTypeSolid
bf.Solid = &BackgroundFillSolid{}
return json.Unmarshal(data, bf.Solid)
case "gradient":
case BackgroundFillTypeGradient:
bf.Type = BackgroundFillTypeGradient
bf.Gradient = &BackgroundFillGradient{}
return json.Unmarshal(data, bf.Gradient)
case "freeform_gradient":
case BackgroundFillTypeFreeformGradient:
bf.Type = BackgroundFillTypeFreeformGradient
bf.FreeformGradient = &BackgroundFillFreeformGradient{}
return json.Unmarshal(data, bf.FreeformGradient)
Expand All @@ -123,19 +146,38 @@ func (bf *BackgroundFill) UnmarshalJSON(data []byte) error {
return fmt.Errorf("unsupported BackgroundFill type")
}

func (bf *BackgroundFill) MarshalJSON() ([]byte, error) {
switch bf.Type {
case BackgroundFillTypeSolid:
bf.Solid.Type = BackgroundFillTypeSolid
return json.Marshal(bf.Solid)
case BackgroundFillTypeGradient:
bf.Gradient.Type = BackgroundFillTypeGradient
return json.Marshal(bf.Gradient)
case BackgroundFillTypeFreeformGradient:
bf.FreeformGradient.Type = BackgroundFillTypeFreeformGradient
return json.Marshal(bf.FreeformGradient)
}

return nil, fmt.Errorf("unsupported BackgroundFill type")
}

// BackgroundFillSolid https://core.telegram.org/bots/api#backgroundfillsolid
type BackgroundFillSolid struct {
Color int `json:"color"`
Type BackgroundFillType `json:"type"`
Color int `json:"color"`
}

// BackgroundFillGradient https://core.telegram.org/bots/api#backgroundfillgradient
type BackgroundFillGradient struct {
TopColor int `json:"top_color"`
BottomColor int `json:"bottom_color"`
RotationAngle int `json:"rotation_angle"`
Type BackgroundFillType `json:"type"`
TopColor int `json:"top_color"`
BottomColor int `json:"bottom_color"`
RotationAngle int `json:"rotation_angle"`
}

// BackgroundFillFreeformGradient https://core.telegram.org/bots/api#backgroundfillfreeformgradient
type BackgroundFillFreeformGradient struct {
Colors []int `json:"colors"`
Type BackgroundFillType `json:"type"`
Colors []int `json:"colors"`
}
Loading

0 comments on commit 95b8f8c

Please sign in to comment.