@@ -42,14 +42,14 @@ private class SimpleUriSerializer<T : SpotifyUri>(val ctor: (String) -> T) : KSe
42
42
* @property id representation of this uri as an id
43
43
*/
44
44
@Serializable
45
- sealed class SpotifyUri (input : String , type : UriType ) {
45
+ sealed class SpotifyUri (val input : String , type : String ) {
46
46
val uri: String
47
47
val id: String
48
48
49
49
init {
50
50
input.replace(" " , " " ).let {
51
- this .uri = it.add(type.toString() )
52
- this .id = it.remove(type.toString() )
51
+ this .uri = it.add(type)
52
+ this .id = it.remove(type)
53
53
}
54
54
}
55
55
@@ -68,100 +68,156 @@ sealed class SpotifyUri(input: String, type: UriType) {
68
68
return " SpotifyUri($uri )"
69
69
}
70
70
71
- enum class UriType (private val typeStr : String ) {
72
- ALBUM (" album" ),
73
- ARTIST (" artist" ),
74
- TRACK (" track" ),
75
- USER (" user" ),
76
- PLAYLIST (" playlist" ),
77
- LOCAL_TRACK (" local" );
78
-
79
- override fun toString () = typeStr
80
- }
81
-
82
71
@Serializer(forClass = SpotifyUri ::class )
83
72
companion object : KSerializer <SpotifyUri > {
84
- fun isUriType (uri : String , type : UriType ) = uri.matchType(type.toString()) != null
85
-
86
73
override val descriptor: SerialDescriptor = StringDescriptor
87
74
override fun deserialize (decoder : Decoder ): SpotifyUri = SpotifyUri (decoder.decodeString())
88
75
override fun serialize (encoder : Encoder , obj : SpotifyUri ) = encoder.encodeString(obj.uri)
89
76
90
- private inline fun <T : SpotifyUri > safeInitiate (uri : String , ctor : (String ) -> T ): T ? {
77
+ /* *
78
+ * This function safely instantiates a SpotifyUri from given constructor.
79
+ * */
80
+ inline fun <T : SpotifyUri > safeInitiate (uri : String , ctor : (String ) -> T ): T ? {
91
81
return try {
92
- ctor(uri). takeIf { it.uri == uri }
82
+ ctor(uri)
93
83
} catch (e: SpotifyUriException ) {
94
84
null
95
85
}
96
86
}
97
87
88
+ /* *
89
+ * Creates a abstract SpotifyUri of given input. Doesn't allow ambiguity by disallowing creation by id.
90
+ * */
98
91
operator fun invoke (input : String ): SpotifyUri {
99
- val constructors = listOf (::AlbumUri , ::ArtistUri , :: TrackUri , ::UserUri , ::PlaylistUri )
92
+ val constructors = listOf (::AlbumUri , ::ArtistUri , TrackUri . Companion ::invoke , ::UserUri , ::PlaylistUri )
100
93
for (ctor in constructors) {
101
- safeInitiate(input, ctor)?.also { return it }
94
+ safeInitiate(input, ctor)?.takeIf { it.uri == input }?. also { return it }
102
95
}
103
96
104
97
throw SpotifyUriException (" Illegal Spotify ID/URI: '$input ' isn't convertible to any arbitrary id" )
105
98
}
99
+
100
+ /* *
101
+ * This function returns whether or not the given input IS a given type.
102
+ *
103
+ * @example ```Kotlin
104
+ * SpotifyUri.isType<UserUri>("abc") // returns: false
105
+ * SpotifyUri.isType<UserUri>("spotify:user:abc") // returns: true
106
+ * SpotifyUri.isType<UserUri>("spotify:track:abc") // returns: false
107
+ * ```
108
+ * */
109
+ inline fun <reified T : SpotifyUri > isType (input : String ): Boolean {
110
+ return safeInitiate(input, ::invoke)?.let { it is T } ? : false
111
+ }
112
+
113
+ /* *
114
+ * This function returns whether ot not the given input CAN be a given type.
115
+ *
116
+ * @example ```Kotlin
117
+ * SpotifyUri.canBeType<UserUri>("abc") // returns: true
118
+ * SpotifyUri.canBeType<UserUri>("spotify:user:abc") // returns: true
119
+ * SpotifyUri.canBeType<UserUri>("spotify:track:abc") // returns: false
120
+ * ```
121
+ * */
122
+ inline fun <reified T : SpotifyUri > canBeType (input : String ): Boolean {
123
+ return isType<T >(input) || ! input.contains(' :' )
124
+ }
106
125
}
107
126
}
108
127
109
128
/* *
110
129
* Represents a Spotify **Album** URI, parsed from either a Spotify ID or taken from an endpoint.
111
130
*/
112
131
@Serializable
113
- class AlbumUri (val input : String ) : SpotifyUri(input, UriType . ALBUM ) {
132
+ class AlbumUri (input : String ) : SpotifyUri(input, " album " ) {
114
133
@Serializer(forClass = AlbumUri ::class )
115
134
companion object : KSerializer <AlbumUri > by SimpleUriSerializer (::AlbumUri )
116
135
}
136
+
137
+ @Deprecated(" renamed" , ReplaceWith (" AlbumUri" , " com.adamratzman.spotify.models.AlbumUri" ))
117
138
typealias AlbumURI = AlbumUri
118
139
119
140
/* *
120
141
* Represents a Spotify **Artist** URI, parsed from either a Spotify ID or taken from an endpoint.
121
142
*/
122
143
@Serializable
123
- class ArtistUri (val input : String ) : SpotifyUri(input, UriType . ARTIST ) {
144
+ class ArtistUri (input : String ) : SpotifyUri(input, " artist " ) {
124
145
@Serializer(forClass = ArtistUri ::class )
125
146
companion object : KSerializer <ArtistUri > by SimpleUriSerializer (::ArtistUri )
126
147
}
127
- typealias ArtistURI = ArtistUri
128
148
129
- /* *
130
- * Represents a Spotify **Track** URI, parsed from either a Spotify ID or taken from an endpoint.
131
- */
132
- @Serializable
133
- class TrackUri (val input : String ) : SpotifyUri(input, UriType .TRACK ) {
134
- @Serializer(forClass = TrackUri ::class )
135
- companion object : KSerializer <TrackUri > by SimpleUriSerializer (::TrackUri )
136
- }
137
- typealias TrackURI = TrackUri
149
+ @Deprecated(" renamed" , ReplaceWith (" ArtistUri" , " com.adamratzman.spotify.models.ArtistUri" ))
150
+ typealias ArtistURI = ArtistUri
138
151
139
152
/* *
140
153
* Represents a Spotify **User** URI, parsed from either a Spotify ID or taken from an endpoint.
141
154
*/
142
155
@Serializable
143
- class UserUri (val input : String ) : SpotifyUri(input, UriType . USER ) {
156
+ class UserUri (input : String ) : SpotifyUri(input, " user " ) {
144
157
@Serializer(forClass = UserUri ::class )
145
158
companion object : KSerializer <UserUri > by SimpleUriSerializer (::UserUri )
146
159
}
160
+
161
+ @Deprecated(" renamed" , ReplaceWith (" UserUri" , " com.adamratzman.spotify.models.UserUri" ))
147
162
typealias UserURI = UserUri
148
163
149
164
/* *
150
165
* Represents a Spotify **Playlist** URI, parsed from either a Spotify ID or taken from an endpoint.
151
166
*/
152
167
@Serializable
153
- class PlaylistUri (val input : String ) : SpotifyUri(input, UriType . PLAYLIST ) {
168
+ class PlaylistUri (input : String ) : SpotifyUri(input, " playlist " ) {
154
169
@Serializer(forClass = PlaylistUri ::class )
155
170
companion object : KSerializer <PlaylistUri > by SimpleUriSerializer (::PlaylistUri )
156
171
}
172
+
173
+ @Deprecated(" renamed" , ReplaceWith (" PlaylistUri" , " com.adamratzman.spotify.models.PlaylistUri" ))
157
174
typealias PlaylistURI = PlaylistUri
158
175
176
+ /* *
177
+ * Represents a Spotify **Track** URI, ether LocalTrack or SpotifyTrack, parsed from either a Spotify ID or taken
178
+ * from an endpoint
179
+ * */
180
+ @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)
187
+
188
+ /* *
189
+ * Creates a abstract TrackURI of given input. Prefers SpotifyTrackUri if the input is ambiguous.
190
+ * */
191
+ operator fun invoke (input : String ): TrackUri {
192
+ val constructors = listOf (::SpotifyTrackUri , ::LocalTrackUri )
193
+ for (ctor in constructors) {
194
+ safeInitiate(input, ctor)?.also { return it }
195
+ }
196
+ throw SpotifyUriException (" Illegal Spotify ID/URI: '$input ' isn't convertible to 'track' or 'local' id" )
197
+ }
198
+ }
199
+ }
200
+
201
+ @Deprecated(" renamed" , ReplaceWith (" TrackUri" , " com.adamratzman.spotify.models.TrackUri" ))
202
+ typealias TrackURI = TrackUri
203
+
204
+ /* *
205
+ * Represents a Spotify **Track** URI, parsed from either a Spotify ID or taken from an endpoint.
206
+ */
207
+ @Serializable
208
+ class SpotifyTrackUri (input : String ) : TrackUri(input, " track" ) {
209
+ @Serializer(forClass = SpotifyTrackUri ::class )
210
+ companion object : KSerializer <SpotifyTrackUri > by SimpleUriSerializer (::SpotifyTrackUri )
211
+ }
212
+
159
213
/* *
160
214
* Represents a Spotify **local track** URI
161
215
*/
162
216
@Serializable
163
- class LocalTrackUri (val input : String ) : SpotifyUri (input, UriType . LOCAL_TRACK ) {
217
+ class LocalTrackUri (input : String ) : TrackUri (input, " local " ) {
164
218
@Serializer(forClass = LocalTrackUri ::class )
165
219
companion object : KSerializer <LocalTrackUri > by SimpleUriSerializer (::LocalTrackUri )
166
220
}
221
+
222
+ @Deprecated(" renamed" , ReplaceWith (" LocalTrackUri" , " com.adamratzman.spotify.models.LocalTrackUri" ))
167
223
typealias LocalTrackURI = LocalTrackUri
0 commit comments