11package types
22
3+ import (
4+ "encoding/json"
5+ "fmt"
6+ )
7+
38// AlbumTypeMinimal represents minimal information about an album, including artist and explicit content details.
49type AlbumTypeMinimal struct {
510 ALB_ID string `json:"ALB_ID"` // Album ID, e.g., '9188269'
@@ -21,14 +26,38 @@ type AlbumTypeMinimal struct {
2126 TYPE_INTERNAL string `json:"__TYPE__"` // Internal type, e.g., 'album'
2227}
2328
29+ // AlbumContributors represents contributors to the album.
30+ type AlbumContributors struct {
31+ MainArtist []string `json:"main_artist"` // Main artist, e.g., ['Avicii']
32+ }
33+
34+ // UnmarshalJSON for AlbumContributors allows dynamic handling of multiple structures.
35+ // Albums without contributor metadata (e.g., various-artists soundtracks) are returned
36+ // by the API as an empty array instead of an object.
37+ func (ac * AlbumContributors ) UnmarshalJSON (data []byte ) error {
38+ // Attempt to unmarshal directly into the struct
39+ type Alias AlbumContributors
40+ var tmp Alias
41+ if err := json .Unmarshal (data , & tmp ); err == nil {
42+ * ac = AlbumContributors (tmp )
43+ return nil
44+ }
45+
46+ // If the above fails, try parsing as an empty array
47+ if string (data ) == "[]" || string (data ) == "{}" {
48+ * ac = AlbumContributors {}
49+ return nil
50+ }
51+
52+ return fmt .Errorf ("failed to unmarshal AlbumContributors: %s" , string (data ))
53+ }
54+
2455// AlbumType represents detailed information about an album including contributors and release dates.
2556type AlbumType struct {
26- ALB_CONTRIBUTORS struct { // Contributors to the album
27- MainArtist []string `json:"main_artist"` // Main artist, e.g., ['Avicii']
28- } `json:"ALB_CONTRIBUTORS"`
29- ALB_ID string `json:"ALB_ID"` // Album ID, e.g., '9188269'
30- ALB_PICTURE string `json:"ALB_PICTURE"` // Album picture hash, e.g., '6e58a99f59a150e9b4aefbeb2d6fc856'
31- EXPLICIT_ALBUM_CONTENT struct { // Explicit content details
57+ ALB_CONTRIBUTORS AlbumContributors `json:"ALB_CONTRIBUTORS"` // Contributors to the album
58+ ALB_ID string `json:"ALB_ID"` // Album ID, e.g., '9188269'
59+ ALB_PICTURE string `json:"ALB_PICTURE"` // Album picture hash, e.g., '6e58a99f59a150e9b4aefbeb2d6fc856'
60+ EXPLICIT_ALBUM_CONTENT struct { // Explicit content details
3261 EXPLICIT_LYRICS_STATUS int `json:"EXPLICIT_LYRICS_STATUS"` // Explicit lyrics status, e.g., 0
3362 EXPLICIT_COVER_STATUS int `json:"EXPLICIT_COVER_STATUS"` // Explicit cover status, e.g., 0
3463 } `json:"EXPLICIT_ALBUM_CONTENT"`
0 commit comments