From 992897732b7227466794bc4c4ef051e7330c7ed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?To=CF=80?= Date: Thu, 12 Oct 2023 17:47:33 +0200 Subject: [PATCH 1/5] allow for deserialization of plugin info into data classes --- .../src/main/java/lavalink/server/util/util.kt | 6 +++--- .../dev/arbjerg/lavalink/protocol/v4/loadResult.kt | 4 ++-- .../dev/arbjerg/lavalink/protocol/v4/player.kt | 13 +++++++++++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/LavalinkServer/src/main/java/lavalink/server/util/util.kt b/LavalinkServer/src/main/java/lavalink/server/util/util.kt index 4a093cd27..6074eb025 100644 --- a/LavalinkServer/src/main/java/lavalink/server/util/util.kt +++ b/LavalinkServer/src/main/java/lavalink/server/util/util.kt @@ -54,7 +54,7 @@ fun AudioTrack.toTrack(encoded: String, pluginInfoModifiers: List): JsonObject { +fun AudioPlaylist.toPluginInfo(pluginInfoModifiers: List): PluginData { val pluginInfo = pluginInfoModifiers.fold(JsonObject(emptyMap())) { acc, it -> val jsonObject = it.modifyAudioPlaylistPluginInfo(this) ?: JsonObject(emptyMap()) acc + jsonObject } - return pluginInfo + return PluginData(pluginInfo) } fun LavalinkPlayer.toPlayer(context: SocketContext, pluginInfoModifiers: List): Player { diff --git a/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/loadResult.kt b/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/loadResult.kt index 59ca4f8ee..95e631e9b 100644 --- a/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/loadResult.kt +++ b/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/loadResult.kt @@ -96,7 +96,7 @@ sealed interface LoadResult { companion object { fun trackLoaded(track: Track) = TrackLoaded(track) - fun playlistLoaded(playlistInfo: PlaylistInfo, pluginInfo: JsonObject, tracks: List) = + fun playlistLoaded(playlistInfo: PlaylistInfo, pluginInfo: PluginData, tracks: List) = PlaylistLoaded( Playlist( playlistInfo, @@ -133,7 +133,7 @@ data class PlaylistInfo( @Serializable data class Playlist( val info: PlaylistInfo, - val pluginInfo: JsonObject, + val pluginInfo: PluginData, val tracks: List ) : LoadResult.Data diff --git a/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/player.kt b/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/player.kt index 5cc31ac00..b3f237871 100644 --- a/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/player.kt +++ b/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/player.kt @@ -1,10 +1,19 @@ package dev.arbjerg.lavalink.protocol.v4 +import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.Serializable import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.serializer import kotlin.jvm.JvmInline -@Serializable() +@Serializable +@JvmInline +value class PluginData(private val obj: JsonObject){ + inline fun deserialize(): T = deserialize(json.serializersModule.serializer()) + fun deserialize(deserializer: DeserializationStrategy): T = json.decodeFromJsonElement(deserializer, obj) +} + +@Serializable @JvmInline value class Players(val players: List) @@ -23,7 +32,7 @@ data class Player( data class Track( val encoded: String, val info: TrackInfo, - val pluginInfo: JsonObject + val pluginInfo: PluginData ) : LoadResult.Data @Serializable From 030052163d182d72a6cdb0abc6c2ededcd59bab6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?To=CF=80?= Date: Thu, 12 Oct 2023 18:00:05 +0200 Subject: [PATCH 2/5] fix unit test --- protocol/src/commonTest/kotlin/LoadResultSerializerTest.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/protocol/src/commonTest/kotlin/LoadResultSerializerTest.kt b/protocol/src/commonTest/kotlin/LoadResultSerializerTest.kt index c16e3ee13..f4dbe29ca 100644 --- a/protocol/src/commonTest/kotlin/LoadResultSerializerTest.kt +++ b/protocol/src/commonTest/kotlin/LoadResultSerializerTest.kt @@ -1,6 +1,7 @@ import dev.arbjerg.lavalink.protocol.v4.Exception import dev.arbjerg.lavalink.protocol.v4.LoadResult import dev.arbjerg.lavalink.protocol.v4.ResultStatus +import kotlinx.serialization.json.JsonObject import kotlin.js.JsName import kotlin.test.Test import kotlin.test.assertIs @@ -150,7 +151,7 @@ class LoadResultSerializerTest { loadType shouldBe ResultStatus.PLAYLIST assertIs(this) data { - pluginInfo shouldBe emptyMap() + pluginInfo shouldBe JsonObject(emptyMap()) info { name shouldBe "Example YouTube Playlist" selectedTrack shouldBe 3 From 62d03df687cfc856af62cf507d7a913830508f8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?To=CF=80?= Date: Thu, 12 Oct 2023 18:26:46 +0200 Subject: [PATCH 3/5] fixup java access for plugin info deserialization Co-authored-by: viztea --- .../src/main/java/lavalink/server/util/util.kt | 6 +++--- .../arbjerg/lavalink/protocol/v4/loadResult.kt | 8 +++++--- .../dev/arbjerg/lavalink/protocol/v4/player.kt | 17 +++++++++-------- .../kotlin/LoadResultSerializerTest.kt | 2 +- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/LavalinkServer/src/main/java/lavalink/server/util/util.kt b/LavalinkServer/src/main/java/lavalink/server/util/util.kt index 6074eb025..4a093cd27 100644 --- a/LavalinkServer/src/main/java/lavalink/server/util/util.kt +++ b/LavalinkServer/src/main/java/lavalink/server/util/util.kt @@ -54,7 +54,7 @@ fun AudioTrack.toTrack(encoded: String, pluginInfoModifiers: List): PluginData { +fun AudioPlaylist.toPluginInfo(pluginInfoModifiers: List): JsonObject { val pluginInfo = pluginInfoModifiers.fold(JsonObject(emptyMap())) { acc, it -> val jsonObject = it.modifyAudioPlaylistPluginInfo(this) ?: JsonObject(emptyMap()) acc + jsonObject } - return PluginData(pluginInfo) + return pluginInfo } fun LavalinkPlayer.toPlayer(context: SocketContext, pluginInfoModifiers: List): Player { diff --git a/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/loadResult.kt b/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/loadResult.kt index 95e631e9b..a996e5ab4 100644 --- a/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/loadResult.kt +++ b/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/loadResult.kt @@ -96,7 +96,7 @@ sealed interface LoadResult { companion object { fun trackLoaded(track: Track) = TrackLoaded(track) - fun playlistLoaded(playlistInfo: PlaylistInfo, pluginInfo: PluginData, tracks: List) = + fun playlistLoaded(playlistInfo: PlaylistInfo, pluginInfo: JsonObject, tracks: List) = PlaylistLoaded( Playlist( playlistInfo, @@ -133,9 +133,11 @@ data class PlaylistInfo( @Serializable data class Playlist( val info: PlaylistInfo, - val pluginInfo: PluginData, + val pluginInfo: JsonObject, val tracks: List -) : LoadResult.Data +) : LoadResult.Data { + fun deserialize(deserializer: DeserializationStrategy): T = pluginInfo.deserialize(deserializer) +} @Serializable data class Exception( diff --git a/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/player.kt b/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/player.kt index b3f237871..95bfdc4ef 100644 --- a/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/player.kt +++ b/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/player.kt @@ -6,12 +6,11 @@ import kotlinx.serialization.json.JsonObject import kotlinx.serialization.serializer import kotlin.jvm.JvmInline -@Serializable -@JvmInline -value class PluginData(private val obj: JsonObject){ - inline fun deserialize(): T = deserialize(json.serializersModule.serializer()) - fun deserialize(deserializer: DeserializationStrategy): T = json.decodeFromJsonElement(deserializer, obj) -} +inline fun JsonObject.deserialize(): T = + deserialize(json.serializersModule.serializer()) + +fun JsonObject.deserialize(deserializer: DeserializationStrategy): T = + json.decodeFromJsonElement(deserializer, this) @Serializable @JvmInline @@ -32,8 +31,10 @@ data class Player( data class Track( val encoded: String, val info: TrackInfo, - val pluginInfo: PluginData -) : LoadResult.Data + val pluginInfo: JsonObject +) : LoadResult.Data { + fun deserialize(deserializer: DeserializationStrategy): T = pluginInfo.deserialize(deserializer) +} @Serializable @JvmInline diff --git a/protocol/src/commonTest/kotlin/LoadResultSerializerTest.kt b/protocol/src/commonTest/kotlin/LoadResultSerializerTest.kt index f4dbe29ca..01842c3dc 100644 --- a/protocol/src/commonTest/kotlin/LoadResultSerializerTest.kt +++ b/protocol/src/commonTest/kotlin/LoadResultSerializerTest.kt @@ -151,7 +151,7 @@ class LoadResultSerializerTest { loadType shouldBe ResultStatus.PLAYLIST assertIs(this) data { - pluginInfo shouldBe JsonObject(emptyMap()) + pluginInfo shouldBe emptyMap() info { name shouldBe "Example YouTube Playlist" selectedTrack shouldBe 3 From 83128eef6cbb679bd500926ca32735a61e9ac12e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?To=CF=80?= Date: Thu, 12 Oct 2023 22:14:23 +0200 Subject: [PATCH 4/5] rename deserialize to deserializePluginInfo & docs --- .../dev/arbjerg/lavalink/protocol/v4/loadResult.kt | 12 +++++++++++- .../dev/arbjerg/lavalink/protocol/v4/player.kt | 12 +++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/loadResult.kt b/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/loadResult.kt index a996e5ab4..f1b902d1f 100644 --- a/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/loadResult.kt +++ b/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/loadResult.kt @@ -136,7 +136,17 @@ data class Playlist( val pluginInfo: JsonObject, val tracks: List ) : LoadResult.Data { - fun deserialize(deserializer: DeserializationStrategy): T = pluginInfo.deserialize(deserializer) + + /** + * Deserialize the plugin info into a specific type. + * This method is a convenience method meant to be used in Java, + * since Kotlin extension methods are painful to use in Java. + * + * @param deserializer The deserializer to use. (e.g. `T.Companion.serializer()`) + * + * @return the deserialized plugin info as type T + */ + fun deserializePluginInfo(deserializer: DeserializationStrategy): T = pluginInfo.deserialize(deserializer) } @Serializable diff --git a/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/player.kt b/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/player.kt index 95bfdc4ef..f76d14c84 100644 --- a/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/player.kt +++ b/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/player.kt @@ -33,7 +33,17 @@ data class Track( val info: TrackInfo, val pluginInfo: JsonObject ) : LoadResult.Data { - fun deserialize(deserializer: DeserializationStrategy): T = pluginInfo.deserialize(deserializer) + + /** + * Deserialize the plugin info into a specific type. + * This method is a convenience method meant to be used in Java, + * since Kotlin extension methods are painful to use in Java. + * + * @param deserializer The deserializer to use. (e.g. `T.Companion.serializer()`) + * + * @return the deserialized plugin info as type T + */ + fun deserializePluginInfo(deserializer: DeserializationStrategy): T = pluginInfo.deserialize(deserializer) } @Serializable From ab6c327a8ff12db2dbec42909c8f5d965e8a4bd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?To=CF=80?= Date: Sat, 14 Oct 2023 18:17:21 +0200 Subject: [PATCH 5/5] remove unused import --- protocol/src/commonTest/kotlin/LoadResultSerializerTest.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/protocol/src/commonTest/kotlin/LoadResultSerializerTest.kt b/protocol/src/commonTest/kotlin/LoadResultSerializerTest.kt index 01842c3dc..c16e3ee13 100644 --- a/protocol/src/commonTest/kotlin/LoadResultSerializerTest.kt +++ b/protocol/src/commonTest/kotlin/LoadResultSerializerTest.kt @@ -1,7 +1,6 @@ import dev.arbjerg.lavalink.protocol.v4.Exception import dev.arbjerg.lavalink.protocol.v4.LoadResult import dev.arbjerg.lavalink.protocol.v4.ResultStatus -import kotlinx.serialization.json.JsonObject import kotlin.js.JsName import kotlin.test.Test import kotlin.test.assertIs