diff --git a/Sources/SpotifyWebAPI/Object Model/Other Spotify Objects/TrackLink.swift b/Sources/SpotifyWebAPI/Object Model/Other Spotify Objects/TrackLink.swift index 67765e45..866133cf 100644 --- a/Sources/SpotifyWebAPI/Object Model/Other Spotify Objects/TrackLink.swift +++ b/Sources/SpotifyWebAPI/Object Model/Other Spotify Objects/TrackLink.swift @@ -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. @@ -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. @@ -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