Skip to content

Commit

Permalink
fixup java access for plugin info deserialization
Browse files Browse the repository at this point in the history
Co-authored-by: viztea <hi@vzt.gay>
  • Loading branch information
topi314 and viztea committed Oct 12, 2023
1 parent 0300521 commit 62d03df
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 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(), PluginData(pluginInfo))
return Track(encoded, this.toInfo(), 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>): PluginData {
fun AudioPlaylist.toPluginInfo(pluginInfoModifiers: List<AudioPluginInfoModifier>): 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<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: PluginData, tracks: List<Track>) =
fun playlistLoaded(playlistInfo: PlaylistInfo, pluginInfo: JsonObject, tracks: List<Track>) =
PlaylistLoaded(
Playlist(
playlistInfo,
Expand Down Expand Up @@ -133,9 +133,11 @@ data class PlaylistInfo(
@Serializable
data class Playlist(
val info: PlaylistInfo,
val pluginInfo: PluginData,
val pluginInfo: JsonObject,
val tracks: List<Track>
) : LoadResult.Data
) : LoadResult.Data {
fun <T> deserialize(deserializer: DeserializationStrategy<T>): T = pluginInfo.deserialize(deserializer)
}

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

fun <T> JsonObject.deserialize(deserializer: DeserializationStrategy<T>): T =
json.decodeFromJsonElement(deserializer, this)

@Serializable
@JvmInline
Expand All @@ -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 <T> deserialize(deserializer: DeserializationStrategy<T>): T = pluginInfo.deserialize(deserializer)
}

@Serializable
@JvmInline
Expand Down
2 changes: 1 addition & 1 deletion protocol/src/commonTest/kotlin/LoadResultSerializerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class LoadResultSerializerTest {
loadType shouldBe ResultStatus.PLAYLIST
assertIs<LoadResult.PlaylistLoaded>(this)
data {
pluginInfo shouldBe JsonObject(emptyMap())
pluginInfo shouldBe emptyMap()
info {
name shouldBe "Example YouTube Playlist"
selectedTrack shouldBe 3
Expand Down

0 comments on commit 62d03df

Please sign in to comment.