1
1
/* Spotify Web API - Kotlin Wrapper; MIT License, 2019; Original author: Adam Ratzman */
2
2
package com.adamratzman.spotify.models
3
3
4
- import kotlinx.serialization.Decoder
5
- import kotlinx.serialization.Encoder
6
- import kotlinx.serialization.KSerializer
7
- import kotlinx.serialization.SerialDescriptor
8
4
import kotlinx.serialization.Serializable
9
5
import kotlinx.serialization.Serializer
10
- import kotlinx.serialization.internal.StringDescriptor
6
+ import kotlinx.serialization.Transient
11
7
12
8
private fun String.matchType (type : String ): String? {
13
9
val typeRegex = " ^spotify:(?:.*:)*$type :([^:]+)(?::.*)*$|^([^:]+)$" .toRegex()
@@ -29,20 +25,14 @@ private fun String.remove(type: String): String {
29
25
throw SpotifyUriException (" Illegal Spotify ID/URI: '$this ' isn't convertible to '$type ' id" )
30
26
}
31
27
32
- private class SimpleUriSerializer <T : SpotifyUri >(val ctor : (String ) -> T ) : KSerializer<T> {
33
- override val descriptor: SerialDescriptor = StringDescriptor
34
- override fun deserialize (decoder : Decoder ): T = ctor(decoder.decodeString())
35
- override fun serialize (encoder : Encoder , obj : T ) = encoder.encodeString(obj.uri)
36
- }
37
-
38
28
/* *
39
29
* Represents a Spotify URI, parsed from either a Spotify ID or taken from an endpoint.
40
30
*
41
31
* @property uri retrieve this URI as a string
42
32
* @property id representation of this uri as an id
43
33
*/
44
34
@Serializable
45
- sealed class SpotifyUri (val input : String , type : String ) {
35
+ sealed class SpotifyUri (val input : String , val type : String ) {
46
36
val uri: String
47
37
val id: String
48
38
@@ -54,7 +44,7 @@ sealed class SpotifyUri(val input: String, type: String) {
54
44
}
55
45
56
46
override fun equals (other : Any? ): Boolean {
57
- val spotifyUri = other as ? SpotifyUri ? : return false
47
+ val spotifyUri = other as ? SpotifyUri ? : return (other as ? String )?. let { this .uri == it } ? : false
58
48
return spotifyUri.uri == this .uri
59
49
}
60
50
@@ -68,12 +58,7 @@ sealed class SpotifyUri(val input: String, type: String) {
68
58
return " SpotifyUri($uri )"
69
59
}
70
60
71
- @Serializer(forClass = SpotifyUri ::class )
72
- companion object : KSerializer <SpotifyUri > {
73
- override val descriptor: SerialDescriptor = StringDescriptor
74
- override fun deserialize (decoder : Decoder ): SpotifyUri = SpotifyUri (decoder.decodeString())
75
- override fun serialize (encoder : Encoder , obj : SpotifyUri ) = encoder.encodeString(obj.uri)
76
-
61
+ companion object {
77
62
/* *
78
63
* This function safely instantiates a SpotifyUri from given constructor.
79
64
* */
@@ -89,9 +74,12 @@ sealed class SpotifyUri(val input: String, type: String) {
89
74
* Creates a abstract SpotifyUri of given input. Doesn't allow ambiguity by disallowing creation by id.
90
75
* */
91
76
operator fun invoke (input : String ): SpotifyUri {
92
- val constructors = listOf (::AlbumUri , ::ArtistUri , TrackUri . Companion ::invoke , ::UserUri , ::PlaylistUri )
77
+ val constructors = listOf (::AlbumUri , ::ArtistUri , :: LocalTrackUri , ::PlaylistUri , ::SpotifyTrackUri , :: UserUri )
93
78
for (ctor in constructors) {
94
- safeInitiate(input, ctor)?.takeIf { it.uri == input }?.also { return it }
79
+ safeInitiate(input, ctor)?.takeIf {
80
+ @Suppress(" ReplaceCallWithBinaryOperator" )
81
+ it.equals(input)
82
+ }?.also { return it }
95
83
}
96
84
97
85
throw SpotifyUriException (" Illegal Spotify ID/URI: '$input ' isn't convertible to any arbitrary id" )
@@ -129,10 +117,7 @@ sealed class SpotifyUri(val input: String, type: String) {
129
117
* Represents a Spotify **Album** URI, parsed from either a Spotify ID or taken from an endpoint.
130
118
*/
131
119
@Serializable
132
- class AlbumUri (input : String ) : SpotifyUri(input, " album" ) {
133
- @Serializer(forClass = AlbumUri ::class )
134
- companion object : KSerializer <AlbumUri > by SimpleUriSerializer (::AlbumUri )
135
- }
120
+ class AlbumUri (@Transient private val _input : String = TRANSIENT_EMPTY_STRING ) : SpotifyUri(_input , " album" )
136
121
137
122
@Deprecated(" renamed" , ReplaceWith (" AlbumUri" , " com.adamratzman.spotify.models.AlbumUri" ))
138
123
typealias AlbumURI = AlbumUri
@@ -141,22 +126,15 @@ typealias AlbumURI = AlbumUri
141
126
* Represents a Spotify **Artist** URI, parsed from either a Spotify ID or taken from an endpoint.
142
127
*/
143
128
@Serializable
144
- class ArtistUri (input : String ) : SpotifyUri(input, " artist" ) {
145
- @Serializer(forClass = ArtistUri ::class )
146
- companion object : KSerializer <ArtistUri > by SimpleUriSerializer (::ArtistUri )
147
- }
148
-
129
+ class ArtistUri (@Transient private val _input : String = TRANSIENT_EMPTY_STRING ) : SpotifyUri(_input , " artist" )
149
130
@Deprecated(" renamed" , ReplaceWith (" ArtistUri" , " com.adamratzman.spotify.models.ArtistUri" ))
150
131
typealias ArtistURI = ArtistUri
151
132
152
133
/* *
153
134
* Represents a Spotify **User** URI, parsed from either a Spotify ID or taken from an endpoint.
154
135
*/
155
136
@Serializable
156
- class UserUri (input : String ) : SpotifyUri(input, " user" ) {
157
- @Serializer(forClass = UserUri ::class )
158
- companion object : KSerializer <UserUri > by SimpleUriSerializer (::UserUri )
159
- }
137
+ class UserUri (@Transient private val _input : String = TRANSIENT_EMPTY_STRING ) : SpotifyUri(_input , " user" )
160
138
161
139
@Deprecated(" renamed" , ReplaceWith (" UserUri" , " com.adamratzman.spotify.models.UserUri" ))
162
140
typealias UserURI = UserUri
@@ -165,9 +143,17 @@ typealias UserURI = UserUri
165
143
* Represents a Spotify **Playlist** URI, parsed from either a Spotify ID or taken from an endpoint.
166
144
*/
167
145
@Serializable
168
- class PlaylistUri (input : String ) : SpotifyUri(input, " playlist" ) {
169
- @Serializer(forClass = PlaylistUri ::class )
170
- companion object : KSerializer <PlaylistUri > by SimpleUriSerializer (::PlaylistUri )
146
+ class PlaylistUri (@Transient private val _input : String = TRANSIENT_EMPTY_STRING ) : SpotifyUri(_input , " playlist" ) {
147
+ override fun equals (other : Any? ): Boolean {
148
+ val spotifyUri = other as ? SpotifyUri ? : return (other as ? String )?.endsWith(this .uri.removePrefix(" spotify" )) ? : false
149
+ return spotifyUri.uri == this .uri
150
+ }
151
+
152
+ override fun hashCode (): Int {
153
+ var result = super .hashCode()
154
+ result = 31 * result + _input .hashCode()
155
+ return result
156
+ }
171
157
}
172
158
173
159
@Deprecated(" renamed" , ReplaceWith (" PlaylistUri" , " com.adamratzman.spotify.models.PlaylistUri" ))
@@ -178,13 +164,14 @@ typealias PlaylistURI = PlaylistUri
178
164
* from an endpoint
179
165
* */
180
166
@Serializable
181
- sealed class TrackUri (input : String , type : String ) : SpotifyUri(input, type) {
182
- @Serializer(forClass = TrackUri ::class )
183
- companion object : KSerializer <TrackUri > {
184
- override val descriptor: SerialDescriptor = StringDescriptor
185
- override fun deserialize (decoder : Decoder ): TrackUri = TrackUri (decoder.decodeString())
186
- override fun serialize (encoder : Encoder , obj : TrackUri ) = encoder.encodeString(obj.uri)
167
+ sealed class TrackUri (
168
+ @Transient private val _input : String = TRANSIENT_EMPTY_STRING ,
169
+ @Transient private val _type : String = TRANSIENT_EMPTY_STRING
170
+ ) :
171
+ SpotifyUri (_input , _type ) {
187
172
173
+ @Serializer(forClass = TrackUri ::class )
174
+ companion object {
188
175
/* *
189
176
* Creates a abstract TrackURI of given input. Prefers SpotifyTrackUri if the input is ambiguous.
190
177
* */
@@ -205,19 +192,13 @@ typealias TrackURI = TrackUri
205
192
* Represents a Spotify **Track** URI, parsed from either a Spotify ID or taken from an endpoint.
206
193
*/
207
194
@Serializable
208
- class SpotifyTrackUri (input : String ) : TrackUri(input, " track" ) {
209
- @Serializer(forClass = SpotifyTrackUri ::class )
210
- companion object : KSerializer <SpotifyTrackUri > by SimpleUriSerializer (::SpotifyTrackUri )
211
- }
195
+ class SpotifyTrackUri (@Transient private val _input : String = TRANSIENT_EMPTY_STRING ) : TrackUri(_input , " track" )
212
196
213
197
/* *
214
198
* Represents a Spotify **local track** URI
215
199
*/
216
200
@Serializable
217
- class LocalTrackUri (input : String ) : TrackUri(input, " local" ) {
218
- @Serializer(forClass = LocalTrackUri ::class )
219
- companion object : KSerializer <LocalTrackUri > by SimpleUriSerializer (::LocalTrackUri )
220
- }
201
+ class LocalTrackUri (@Transient private val _input : String = TRANSIENT_EMPTY_STRING ) : TrackUri(_input , " local" )
221
202
222
203
@Deprecated(" renamed" , ReplaceWith (" LocalTrackUri" , " com.adamratzman.spotify.models.LocalTrackUri" ))
223
204
typealias LocalTrackURI = LocalTrackUri
0 commit comments