Skip to content

Commit

Permalink
fix: Serialize compatible packages as a map instead of a set of pairs.
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Aug 16, 2024
1 parent 92ff93d commit 737e272
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
17 changes: 11 additions & 6 deletions src/commonMain/kotlin/app/revanced/library/Serialization.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package app.revanced.library

import app.revanced.patcher.patch.*
import kotlinx.serialization.*
import app.revanced.patcher.patch.Option
import app.revanced.patcher.patch.Patch
import app.revanced.patcher.patch.VersionName
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.*
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.descriptors.element
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.encoding.encodeStructure
import kotlinx.serialization.json.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.encodeToStream
import kotlinx.serialization.serializer
import java.io.OutputStream

private class PatchSerializer : KSerializer<Patch<*>> {
Expand All @@ -17,7 +22,7 @@ private class PatchSerializer : KSerializer<Patch<*>> {
element<String?>("description")
element<Boolean>("use")
element<List<String>>("dependencies")
element<Set<Package>?>("compatiblePackages")
element<Map<PackageName, Set<VersionName>?>?>("compatiblePackages")
element("options", OptionSerializer.descriptor)
}

Expand Down Expand Up @@ -52,8 +57,8 @@ private class PatchSerializer : KSerializer<Patch<*>> {
encodeNullableSerializableElement(
descriptor,
4,
SetSerializer(PairSerializer(String.serializer(), SetSerializer(String.serializer()).nullable)),
value.compatiblePackages,
MapSerializer(String.serializer(), SetSerializer(String.serializer()).nullable),
value.compatiblePackages?.associate { (packageName, versions) -> packageName to versions },
)
encodeSerializableElement(
descriptor,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package app.revanced.library

import app.revanced.patcher.patch.*
import app.revanced.patcher.patch.booleanOption
import app.revanced.patcher.patch.bytecodePatch
import app.revanced.patcher.patch.floatsOption
import app.revanced.patcher.patch.stringOption
import kotlinx.serialization.json.*
import java.io.ByteArrayOutputStream
import kotlin.test.Test
Expand Down Expand Up @@ -33,7 +36,7 @@ class SerializationTest {

assert(deserializedPatch["name"]!!.jsonPrimitive.content == "Test patch")

assert(deserializedPatch["compatiblePackages"]!!.jsonArray.size == 2) {
assert(deserializedPatch["compatiblePackages"]!!.jsonObject.size == 2) {
"The patch should be compatible with two packages."
}

Expand Down

0 comments on commit 737e272

Please sign in to comment.