Initial commit
This commit is contained in:
commit
02a6dabcfb
62 files changed
+15751
No files matched your search
@@ -0,0 +1,145 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
// Landing represents a landing page
|
||||
type Landing struct {
|
||||
BaseModel
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
CoverURI string `json:"cover_uri"`
|
||||
Type string `json:"type"`
|
||||
Blocks []LandingBlock `json:"blocks"`
|
||||
Available bool `json:"available"`
|
||||
}
|
||||
|
||||
// DeJSON deserializes a Landing from JSON
|
||||
func (l *Landing) DeJSON(data []byte, c ClientInterface) (Model, error) {
|
||||
var raw map[string]json.RawMessage
|
||||
if err := json.Unmarshal(data, &raw); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
landing := &Landing{BaseModel: *NewBaseModel(c)}
|
||||
|
||||
if v, ok := raw["id"]; ok {
|
||||
json.Unmarshal(v, &landing.ID)
|
||||
}
|
||||
if v, ok := raw["title"]; ok {
|
||||
json.Unmarshal(v, &landing.Title)
|
||||
}
|
||||
if v, ok := raw["description"]; ok {
|
||||
json.Unmarshal(v, &landing.Description)
|
||||
}
|
||||
if v, ok := raw["cover_uri"]; ok {
|
||||
json.Unmarshal(v, &landing.CoverURI)
|
||||
}
|
||||
if v, ok := raw["type"]; ok {
|
||||
json.Unmarshal(v, &landing.Type)
|
||||
}
|
||||
if v, ok := raw["available"]; ok {
|
||||
json.Unmarshal(v, &landing.Available)
|
||||
}
|
||||
|
||||
if v, ok := raw["blocks"]; ok {
|
||||
var blocks []LandingBlock
|
||||
json.Unmarshal(v, &blocks)
|
||||
landing.Blocks = blocks
|
||||
}
|
||||
|
||||
return landing, nil
|
||||
}
|
||||
|
||||
// DeList deserializes a list of Landings
|
||||
func (l *Landing) 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 {
|
||||
landing := &Landing{BaseModel: *NewBaseModel(c)}
|
||||
if err := json.Unmarshal(item, landing); err != nil {
|
||||
continue
|
||||
}
|
||||
result = append(result, landing)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// LandingBlock represents a block in a landing
|
||||
type LandingBlock struct {
|
||||
BaseModel
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Data *LandingBlockData `json:"data"`
|
||||
}
|
||||
|
||||
// LandingBlockData represents data in a landing block
|
||||
type LandingBlockData struct {
|
||||
Tracks []Track `json:"tracks"`
|
||||
Playlists []Playlist `json:"playlists"`
|
||||
Albums []Album `json:"albums"`
|
||||
Artists []Artist `json:"artists"`
|
||||
GeneratedPlaylist *GeneratedPlaylist `json:"generated_playlist"`
|
||||
}
|
||||
|
||||
// LandingList represents a list of landings
|
||||
type LandingList struct {
|
||||
BaseModel
|
||||
Landings []Landing `json:"landings"`
|
||||
Total int `json:"total"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"page_size"`
|
||||
}
|
||||
|
||||
// DeJSON deserializes a LandingList from JSON
|
||||
func (ll *LandingList) DeJSON(data []byte, c ClientInterface) (Model, error) {
|
||||
var raw map[string]json.RawMessage
|
||||
if err := json.Unmarshal(data, &raw); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list := &LandingList{BaseModel: *NewBaseModel(c)}
|
||||
|
||||
if v, ok := raw["landings"]; ok {
|
||||
var landings []Landing
|
||||
json.Unmarshal(v, &landings)
|
||||
list.Landings = landings
|
||||
}
|
||||
if v, ok := raw["total"]; ok {
|
||||
json.Unmarshal(v, &list.Total)
|
||||
}
|
||||
if v, ok := raw["page"]; ok {
|
||||
json.Unmarshal(v, &list.Page)
|
||||
}
|
||||
if v, ok := raw["page_size"]; ok {
|
||||
json.Unmarshal(v, &list.PageSize)
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// DeList deserializes a list of LandingLists
|
||||
func (ll *LandingList) 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 {
|
||||
list := &LandingList{BaseModel: *NewBaseModel(c)}
|
||||
if err := json.Unmarshal(item, list); err != nil {
|
||||
continue
|
||||
}
|
||||
result = append(result, list)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
Reference in new issue
Block a user