Skip to content

Commit

Permalink
allow for deserialization of plugin info into data classes
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Oct 12, 2023
1 parent 8df70c2 commit 9928977
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
6 changes: 3 additions & 3 deletions LavalinkServer/src/main/java/lavalink/server/util/util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fun AudioTrack.toTrack(encoded: String, pluginInfoModifiers: List<AudioPluginInf
acc + jsonObject
}

return Track(encoded, this.toInfo(), pluginInfo)
return Track(encoded, this.toInfo(), PluginData(pluginInfo))
}

private operator fun JsonObject.plus(other: JsonObject) = JsonObject(toMap() + other.toMap())
Expand All @@ -80,12 +80,12 @@ fun AudioPlaylist.toPlaylistInfo(): PlaylistInfo {
}


fun AudioPlaylist.toPluginInfo(pluginInfoModifiers: List<AudioPluginInfoModifier>): JsonObject {
fun AudioPlaylist.toPluginInfo(pluginInfoModifiers: List<AudioPluginInfoModifier>): 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<AudioPluginInfoModifier>): Player {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ sealed interface LoadResult {
companion object {
fun trackLoaded(track: Track) = TrackLoaded(track)

fun playlistLoaded(playlistInfo: PlaylistInfo, pluginInfo: JsonObject, tracks: List<Track>) =
fun playlistLoaded(playlistInfo: PlaylistInfo, pluginInfo: PluginData, tracks: List<Track>) =
PlaylistLoaded(
Playlist(
playlistInfo,
Expand Down Expand Up @@ -133,7 +133,7 @@ data class PlaylistInfo(
@Serializable
data class Playlist(
val info: PlaylistInfo,
val pluginInfo: JsonObject,
val pluginInfo: PluginData,
val tracks: List<Track>
) : LoadResult.Data

Expand Down
Original file line number Diff line number Diff line change
@@ -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 <reified T> deserialize(): T = deserialize(json.serializersModule.serializer<T>())
fun <T> deserialize(deserializer: DeserializationStrategy<T>): T = json.decodeFromJsonElement(deserializer, obj)
}

@Serializable
@JvmInline
value class Players(val players: List<Player>)

Expand All @@ -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
Expand Down

0 comments on commit 9928977

Please sign in to comment.