Skip to content

Commit

Permalink
Made all TrackLink properties nil. will never fail to decode TrackL…
Browse files Browse the repository at this point in the history
…ink again.
  • Loading branch information
Peter-Schorn committed Jun 30, 2024
1 parent d367f60 commit daccfb0
Showing 1 changed file with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import Foundation
///
/// See the [Track relinking Guide][2].
/// [2]: https://developer.spotify.com/documentation/general/guides/track-relinking-guide/
public struct TrackLink: SpotifyURIConvertible, Hashable {
public struct TrackLink: Hashable {

/**
Known external urls for this track.
Expand All @@ -23,21 +23,21 @@ public struct TrackLink: SpotifyURIConvertible, Hashable {
Use ``SpotifyAPI/getFromHref(_:responseType:)`` to retrieve the results.
*/
public let href: URL
public let href: URL?

/// The [Spotify URI][1] for the track.
///
/// [1]: https://developer.spotify.com/documentation/web-api/#spotify-uris-and-ids
public let uri: String
public let uri: String?

/// The [Spotify ID][1] for the track.
///
/// [1]: https://developer.spotify.com/documentation/web-api/#spotify-uris-and-ids
public let id: String
public let id: String?

/// The object type. Always ``IDCategory/track``.
public let type: IDCategory

/**
Creates a Spotify track link object.
Expand Down Expand Up @@ -73,6 +73,30 @@ public struct TrackLink: SpotifyURIConvertible, Hashable {

extension TrackLink: Codable {

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.externalURLs = try? container.decodeIfPresent(
[String : URL].self, forKey: .externalURLs
)

self.href = try? container.decodeIfPresent(
URL.self, forKey: .href
)

self.uri = try? container.decodeIfPresent(
String.self, forKey: .uri
)

self.id = try? container.decodeIfPresent(
String.self, forKey: .id
)

self.type = (try? container.decodeIfPresent(
IDCategory.self, forKey: .type
)) ?? .track
}

private enum CodingKeys: String, CodingKey {
case externalURLs = "external_urls"
case href
Expand Down

0 comments on commit daccfb0

Please sign in to comment.