@@ -209,36 +209,45 @@ type PlaylistItem struct {
209209 Track PlaylistItemTrack `json:"track"`
210210}
211211
212- // PlaylistItemTrack is a union type for both tracks and episodes.
212+ // PlaylistItemTrack is a union type for both tracks and episodes. If both
213+ // values are null, it's likely that the piece of content is not available in
214+ // the configured market.
213215type PlaylistItemTrack struct {
214216 Track * FullTrack
215217 Episode * EpisodePage
216218}
217219
218220// UnmarshalJSON customises the unmarshalling based on the type flags set.
219221func (t * PlaylistItemTrack ) UnmarshalJSON (b []byte ) error {
220- is := struct {
221- Episode bool `json:"episode"`
222- Track bool `json:"track"`
222+ // Spotify API will return `track: null`` where the content is not available
223+ // in the specified market. We should respect this and just pass the null
224+ // up...
225+ if bytes .Equal (b , []byte ("null" )) {
226+ return nil
227+ }
228+
229+ itemType := struct {
230+ Type string `json:"type"`
223231 }{}
224232
225- err := json .Unmarshal (b , & is )
233+ err := json .Unmarshal (b , & itemType )
226234 if err != nil {
227235 return err
228236 }
229237
230- if is .Episode {
238+ switch itemType .Type {
239+ case "episode" :
231240 err := json .Unmarshal (b , & t .Episode )
232241 if err != nil {
233242 return err
234243 }
235- }
236-
237- if is .Track {
244+ case "track" :
238245 err := json .Unmarshal (b , & t .Track )
239246 if err != nil {
240247 return err
241248 }
249+ default :
250+ return fmt .Errorf ("unrecognized item type: %s" , itemType .Type )
242251 }
243252
244253 return nil
@@ -470,17 +479,17 @@ func (c *Client) RemoveTracksFromPlaylistOpt(
470479 ctx context.Context ,
471480 playlistID ID ,
472481 tracks []TrackToRemove ,
473- snapshotID string ) ( newSnapshotID string , err error ) {
474-
482+ snapshotID string ,
483+ ) ( newSnapshotID string , err error ) {
475484 return c .removeTracksFromPlaylist (ctx , playlistID , tracks , snapshotID )
476485}
477486
478487func (c * Client ) removeTracksFromPlaylist (
479488 ctx context.Context ,
480489 playlistID ID ,
481490 tracks interface {},
482- snapshotID string ) ( newSnapshotID string , err error ) {
483-
491+ snapshotID string ,
492+ ) ( newSnapshotID string , err error ) {
484493 m := make (map [string ]interface {})
485494 m ["tracks" ] = tracks
486495 if snapshotID != "" {
0 commit comments