Files
Mrixs dbb823ade3 Fix R128 and remaining ID fields to accept both JSON types
- Track.R128 and TrackShort.R128: float64 → JSONR128 (accepts both JSON number and object)
- All remaining primary ID fields: string → JSONID (accepts both JSON string and number)
  Fixed in: account, artist, clip, concert, feed, label, landing, metatag,
  music_history, pin, queue, radio, search, settings, supplement
2026-10-18 11:01:26 +03:00

317 lines
8.7 KiB
Go

// Package models provides data models for the Yandex Music API.
package models
import (
"encoding/json"
)
// AccountSettings represents account settings from the Yandex Music API.
type AccountSettings struct {
BaseModel
Language string `json:"language"`
Region string `json:"region"`
Email string `json:"email"`
Phone string `json:"phone"`
Notifications bool `json:"notifications"`
AutoPlay bool `json:"auto_play"`
HighQuality bool `json:"high_quality"`
CacheSize int `json:"cache_size"`
DownloadQuality string `json:"download_quality"`
ExplicitContent bool `json:"explicit_content"`
NormalizeVolume bool `json:"normalize_volume"`
Crossfade bool `json:"crossfade"`
GaplessPlayback bool `json:"gapless_playback"`
ShowLyrics bool `json:"show_lyrics"`
ShowCredits bool `json:"show_credits"`
AutoDownload bool `json:"auto_download"`
DownloadOnWifiOnly bool `json:"download_on_wifi_only"`
}
// DeJSON deserializes AccountSettings from JSON data.
func (a *AccountSettings) DeJSON(data []byte, c ClientInterface) (Model, error) {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return nil, err
}
settings := &AccountSettings{BaseModel: *NewBaseModel(c)}
if v, ok := raw["language"]; ok {
json.Unmarshal(v, &settings.Language)
}
if v, ok := raw["region"]; ok {
json.Unmarshal(v, &settings.Region)
}
if v, ok := raw["email"]; ok {
json.Unmarshal(v, &settings.Email)
}
if v, ok := raw["phone"]; ok {
json.Unmarshal(v, &settings.Phone)
}
if v, ok := raw["notifications"]; ok {
json.Unmarshal(v, &settings.Notifications)
}
if v, ok := raw["auto_play"]; ok {
json.Unmarshal(v, &settings.AutoPlay)
}
if v, ok := raw["high_quality"]; ok {
json.Unmarshal(v, &settings.HighQuality)
}
if v, ok := raw["cache_size"]; ok {
json.Unmarshal(v, &settings.CacheSize)
}
if v, ok := raw["download_quality"]; ok {
json.Unmarshal(v, &settings.DownloadQuality)
}
if v, ok := raw["explicit_content"]; ok {
json.Unmarshal(v, &settings.ExplicitContent)
}
if v, ok := raw["normalize_volume"]; ok {
json.Unmarshal(v, &settings.NormalizeVolume)
}
if v, ok := raw["crossfade"]; ok {
json.Unmarshal(v, &settings.Crossfade)
}
if v, ok := raw["gapless_playback"]; ok {
json.Unmarshal(v, &settings.GaplessPlayback)
}
if v, ok := raw["show_lyrics"]; ok {
json.Unmarshal(v, &settings.ShowLyrics)
}
if v, ok := raw["show_credits"]; ok {
json.Unmarshal(v, &settings.ShowCredits)
}
if v, ok := raw["auto_download"]; ok {
json.Unmarshal(v, &settings.AutoDownload)
}
if v, ok := raw["download_on_wifi_only"]; ok {
json.Unmarshal(v, &settings.DownloadOnWifiOnly)
}
return settings, nil
}
// DeList deserializes a list of AccountSettings from JSON data.
func (a *AccountSettings) DeList(data []byte, c ClientInterface) ([]Model, error) {
var rawList []json.RawMessage
if err := json.Unmarshal(data, &rawList); err != nil {
return nil, err
}
result := make([]Model, 0, len(rawList))
for _, item := range rawList {
settings := &AccountSettings{BaseModel: *NewBaseModel(c)}
if err := json.Unmarshal(item, settings); err != nil {
continue
}
result = append(result, settings)
}
return result, nil
}
// Subscription represents subscription info.
type Subscription struct {
BaseModel
ID JSONID `json:"id"`
Type string `json:"type"`
Status string `json:"status"`
StartDate string `json:"start_date"`
EndDate string `json:"end_date"`
AutoRenew bool `json:"auto_renew"`
PaymentMethod string `json:"payment_method"`
Price int `json:"price"`
Currency string `json:"currency"`
Features []string `json:"features"`
CanCancel bool `json:"can_cancel"`
CanUpgrade bool `json:"can_upgrade"`
TrialAvailable bool `json:"trial_available"`
TrialUsed bool `json:"trial_used"`
}
// DeJSON deserializes Subscription from JSON data.
func (s *Subscription) DeJSON(data []byte, c ClientInterface) (Model, error) {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return nil, err
}
sub := &Subscription{BaseModel: *NewBaseModel(c)}
if v, ok := raw["id"]; ok {
json.Unmarshal(v, &sub.ID)
}
if v, ok := raw["type"]; ok {
json.Unmarshal(v, &sub.Type)
}
if v, ok := raw["status"]; ok {
json.Unmarshal(v, &sub.Status)
}
if v, ok := raw["start_date"]; ok {
json.Unmarshal(v, &sub.StartDate)
}
if v, ok := raw["end_date"]; ok {
json.Unmarshal(v, &sub.EndDate)
}
if v, ok := raw["auto_renew"]; ok {
json.Unmarshal(v, &sub.AutoRenew)
}
if v, ok := raw["payment_method"]; ok {
json.Unmarshal(v, &sub.PaymentMethod)
}
if v, ok := raw["price"]; ok {
json.Unmarshal(v, &sub.Price)
}
if v, ok := raw["currency"]; ok {
json.Unmarshal(v, &sub.Currency)
}
if v, ok := raw["features"]; ok {
json.Unmarshal(v, &sub.Features)
}
if v, ok := raw["can_cancel"]; ok {
json.Unmarshal(v, &sub.CanCancel)
}
if v, ok := raw["can_upgrade"]; ok {
json.Unmarshal(v, &sub.CanUpgrade)
}
if v, ok := raw["trial_available"]; ok {
json.Unmarshal(v, &sub.TrialAvailable)
}
if v, ok := raw["trial_used"]; ok {
json.Unmarshal(v, &sub.TrialUsed)
}
return sub, nil
}
// DeList deserializes a list of Subscriptions from JSON data.
func (s *Subscription) DeList(data []byte, c ClientInterface) ([]Model, error) {
var rawList []json.RawMessage
if err := json.Unmarshal(data, &rawList); err != nil {
return nil, err
}
result := make([]Model, 0, len(rawList))
for _, item := range rawList {
sub := &Subscription{BaseModel: *NewBaseModel(c)}
if err := json.Unmarshal(item, sub); err != nil {
continue
}
result = append(result, sub)
}
return result, nil
}
// PlusInfo represents Plus subscription info.
type PlusInfo struct {
BaseModel
HasPlus bool `json:"has_plus"`
IsTutorialCompleted bool `json:"is_tutorial_completed"`
Migrated *bool `json:"migrated,omitempty"`
}
// DeJSON deserializes PlusInfo from JSON data.
func (p *PlusInfo) DeJSON(data []byte, c ClientInterface) (Model, error) {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return nil, err
}
plus := &PlusInfo{BaseModel: *NewBaseModel(c)}
if v, ok := raw["has_plus"]; ok {
json.Unmarshal(v, &plus.HasPlus)
}
if v, ok := raw["is_tutorial_completed"]; ok {
json.Unmarshal(v, &plus.IsTutorialCompleted)
}
if v, ok := raw["migrated"]; ok {
var val bool
json.Unmarshal(v, &val)
plus.Migrated = &val
}
return plus, nil
}
// DeList deserializes a list of PlusInfo from JSON data.
func (p *PlusInfo) DeList(data []byte, c ClientInterface) ([]Model, error) {
var rawList []json.RawMessage
if err := json.Unmarshal(data, &rawList); err != nil {
return nil, err
}
result := make([]Model, 0, len(rawList))
for _, item := range rawList {
plus := &PlusInfo{BaseModel: *NewBaseModel(c)}
if err := json.Unmarshal(item, plus); err != nil {
continue
}
result = append(result, plus)
}
return result, nil
}
// PromoCodeStatus represents promo code status.
type PromoCodeStatus struct {
BaseModel
Code string `json:"code"`
Status string `json:"status"`
Description string `json:"description"`
Discount int `json:"discount"`
Months int `json:"months"`
ExpiresAt string `json:"expires_at"`
Activated bool `json:"activated"`
}
// DeJSON deserializes PromoCodeStatus from JSON data.
func (p *PromoCodeStatus) DeJSON(data []byte, c ClientInterface) (Model, error) {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return nil, err
}
promo := &PromoCodeStatus{BaseModel: *NewBaseModel(c)}
if v, ok := raw["code"]; ok {
json.Unmarshal(v, &promo.Code)
}
if v, ok := raw["status"]; ok {
json.Unmarshal(v, &promo.Status)
}
if v, ok := raw["description"]; ok {
json.Unmarshal(v, &promo.Description)
}
if v, ok := raw["discount"]; ok {
json.Unmarshal(v, &promo.Discount)
}
if v, ok := raw["months"]; ok {
json.Unmarshal(v, &promo.Months)
}
if v, ok := raw["expires_at"]; ok {
json.Unmarshal(v, &promo.ExpiresAt)
}
if v, ok := raw["activated"]; ok {
json.Unmarshal(v, &promo.Activated)
}
return promo, nil
}
// DeList deserializes a list of PromoCodeStatus from JSON data.
func (p *PromoCodeStatus) DeList(data []byte, c ClientInterface) ([]Model, error) {
var rawList []json.RawMessage
if err := json.Unmarshal(data, &rawList); err != nil {
return nil, err
}
result := make([]Model, 0, len(rawList))
for _, item := range rawList {
promo := &PromoCodeStatus{BaseModel: *NewBaseModel(c)}
if err := json.Unmarshal(item, promo); err != nil {
continue
}
result = append(result, promo)
}
return result, nil
}