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] 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