Files
Mrixs 9d3bcc4a9b Add missing model fields from Python audit
LandingList: Added type, type_for_from, title, id, new_releases, new_playlists, podcasts
New type: PlaylistID
Presaves: Added upcoming_albums, released_albums
Presaves: Added DeJSON method
QueueItem: Added id, context, modified fields (fixed duplicate context)
Clip: Fixed PreviewURI → PreviewURL field name
2026-10-18 13:18:24 +03:00

261 lines
6.5 KiB
Go

// Package models provides data models for the Yandex Music API.
package models
import (
"encoding/json"
)
// Pin represents a pin from the Yandex Music API.
type Pin struct {
BaseModel
ID JSONID `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Image string `json:"image"`
Type string `json:"type"` // "track", "album", "artist", "playlist"
Track *Track `json:"track"`
Album *Album `json:"album"`
Artist *Artist `json:"artist"`
Playlist *Playlist `json:"playlist"`
Position int `json:"position"`
CreatedAt string `json:"created_at"`
ExpiresAt string `json:"expires_at"`
Active bool `json:"active"`
ContentRestrictions *ContentRestrictions `json:"content_restrictions"`
}
// DeJSON deserializes a Pin from JSON data.
func (p *Pin) DeJSON(data []byte, client ClientInterface) (Model, error) {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return nil, err
}
pin := &Pin{BaseModel: *NewBaseModel(client)}
if v, ok := raw["id"]; ok {
json.Unmarshal(v, &pin.ID)
}
if v, ok := raw["title"]; ok {
json.Unmarshal(v, &pin.Title)
}
if v, ok := raw["description"]; ok {
json.Unmarshal(v, &pin.Description)
}
if v, ok := raw["image"]; ok {
json.Unmarshal(v, &pin.Image)
}
if v, ok := raw["type"]; ok {
json.Unmarshal(v, &pin.Type)
}
if v, ok := raw["position"]; ok {
json.Unmarshal(v, &pin.Position)
}
if v, ok := raw["created_at"]; ok {
json.Unmarshal(v, &pin.CreatedAt)
}
if v, ok := raw["expires_at"]; ok {
json.Unmarshal(v, &pin.ExpiresAt)
}
if v, ok := raw["active"]; ok {
json.Unmarshal(v, &pin.Active)
}
if v, ok := raw["content_restrictions"]; ok {
var cr ContentRestrictions
json.Unmarshal(v, &cr)
pin.ContentRestrictions = &cr
}
if v, ok := raw["track"]; ok {
var track Track
json.Unmarshal(v, &track)
pin.Track = &track
}
if v, ok := raw["album"]; ok {
var album Album
json.Unmarshal(v, &album)
pin.Album = &album
}
if v, ok := raw["artist"]; ok {
var artist Artist
json.Unmarshal(v, &artist)
pin.Artist = &artist
}
if v, ok := raw["playlist"]; ok {
var playlist Playlist
json.Unmarshal(v, &playlist)
pin.Playlist = &playlist
}
return pin, nil
}
// DeList deserializes a list of Pins from JSON data.
func (p *Pin) DeList(data []byte, client 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 {
pin := &Pin{BaseModel: *NewBaseModel(client)}
if err := json.Unmarshal(item, pin); err != nil {
continue
}
result = append(result, pin)
}
return result, nil
}
// PinData represents pin data.
type PinData struct {
BaseModel
Pins []Pin `json:"pins"`
Total int `json:"total"`
}
// PinsList represents a list of pins.
type PinsList struct {
BaseModel
Pins []Pin `json:"pins"`
Total int `json:"total"`
Page int `json:"page"`
PageSize int `json:"page_size"`
}
// Presaves represents presaves.
type Presaves struct {
BaseModel
UpcomingAlbums []Album `json:"upcoming_albums"`
ReleasedAlbums []Album `json:"released_albums"`
Presaves []Presave `json:"presaves"`
Total int `json:"total"`
Page int `json:"page"`
PageSize int `json:"page_size"`
}
// Presave represents a presave.
type Presave struct {
BaseModel
ID JSONID `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Image string `json:"image"`
ReleaseDate string `json:"release_date"`
Track *Track `json:"track"`
Album *Album `json:"album"`
Artist *Artist `json:"artist"`
Status string `json:"status"` // "pending", "released", "cancelled"
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
// DeJSON deserializes a Presave from JSON data.
func (p *Presave) DeJSON(data []byte, client ClientInterface) (Model, error) {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return nil, err
}
presave := &Presave{BaseModel: *NewBaseModel(client)}
if v, ok := raw["id"]; ok {
json.Unmarshal(v, &presave.ID)
}
if v, ok := raw["title"]; ok {
json.Unmarshal(v, &presave.Title)
}
if v, ok := raw["description"]; ok {
json.Unmarshal(v, &presave.Description)
}
if v, ok := raw["image"]; ok {
json.Unmarshal(v, &presave.Image)
}
if v, ok := raw["release_date"]; ok {
json.Unmarshal(v, &presave.ReleaseDate)
}
if v, ok := raw["status"]; ok {
json.Unmarshal(v, &presave.Status)
}
if v, ok := raw["created_at"]; ok {
json.Unmarshal(v, &presave.CreatedAt)
}
if v, ok := raw["updated_at"]; ok {
json.Unmarshal(v, &presave.UpdatedAt)
}
if v, ok := raw["track"]; ok {
var track Track
json.Unmarshal(v, &track)
presave.Track = &track
}
if v, ok := raw["album"]; ok {
var album Album
json.Unmarshal(v, &album)
presave.Album = &album
}
if v, ok := raw["artist"]; ok {
var artist Artist
json.Unmarshal(v, &artist)
presave.Artist = &artist
}
return presave, nil
}
// DeList deserializes a list of Presaves from JSON data.
func (p *Presave) DeList(data []byte, client 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 {
presave := &Presave{BaseModel: *NewBaseModel(client)}
if err := json.Unmarshal(item, presave); err != nil {
continue
}
result = append(result, presave)
}
return result, nil
}
// DeJSON deserializes Presaves from JSON data.
func (p *Presaves) DeJSON(data []byte, client ClientInterface) (Model, error) {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return nil, err
}
presaves := &Presaves{BaseModel: *NewBaseModel(client)}
if v, ok := raw["upcoming_albums"]; ok {
var albums []Album
json.Unmarshal(v, &albums)
presaves.UpcomingAlbums = albums
}
if v, ok := raw["released_albums"]; ok {
var albums []Album
json.Unmarshal(v, &albums)
presaves.ReleasedAlbums = albums
}
if v, ok := raw["presaves"]; ok {
var presaveList []Presave
json.Unmarshal(v, &presaveList)
presaves.Presaves = presaveList
}
if v, ok := raw["total"]; ok {
json.Unmarshal(v, &presaves.Total)
}
if v, ok := raw["page"]; ok {
json.Unmarshal(v, &presaves.Page)
}
if v, ok := raw["page_size"]; ok {
json.Unmarshal(v, &presaves.PageSize)
}
return presaves, nil
}