mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-18 14:47:15 +00:00
add serializable diff
This commit is contained in:
parent
da692ccfc3
commit
ec3afc615c
@ -2,6 +2,8 @@
|
||||
|
||||
package dev.inmo.micro_utils.common
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
private inline fun <T> getObject(
|
||||
additional: MutableList<T>,
|
||||
iterator: Iterator<T>
|
||||
@ -24,13 +26,14 @@ private inline fun <T> getObject(
|
||||
*
|
||||
* @see calculateDiff
|
||||
*/
|
||||
@Serializable
|
||||
data class Diff<T> internal constructor(
|
||||
val removed: List<IndexedValue<T>>,
|
||||
val removed: List<@Serializable(IndexedValueSerializer::class) IndexedValue<T>>,
|
||||
/**
|
||||
* Old-New values pairs
|
||||
*/
|
||||
val replaced: List<Pair<IndexedValue<T>, IndexedValue<T>>>,
|
||||
val added: List<IndexedValue<T>>
|
||||
val replaced: List<Pair<@Serializable(IndexedValueSerializer::class) IndexedValue<T>, @Serializable(IndexedValueSerializer::class) IndexedValue<T>>>,
|
||||
val added: List<@Serializable(IndexedValueSerializer::class) IndexedValue<T>>
|
||||
)
|
||||
|
||||
private inline fun <T> performChanges(
|
||||
|
@ -0,0 +1,30 @@
|
||||
package dev.inmo.micro_utils.common
|
||||
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.Serializer
|
||||
import kotlinx.serialization.builtins.PairSerializer
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
import kotlinx.serialization.descriptors.SerialDescriptor
|
||||
import kotlinx.serialization.encoding.Decoder
|
||||
import kotlinx.serialization.encoding.Encoder
|
||||
|
||||
class IndexedValueSerializer<T>(private val subSerializer: KSerializer<T>) : KSerializer<IndexedValue<T>> {
|
||||
private val originalSerializer = PairSerializer(Int.serializer(), subSerializer)
|
||||
override val descriptor: SerialDescriptor
|
||||
get() = originalSerializer.descriptor
|
||||
|
||||
override fun deserialize(decoder: Decoder): IndexedValue<T> {
|
||||
val pair = originalSerializer.deserialize(decoder)
|
||||
return IndexedValue(
|
||||
pair.first,
|
||||
pair.second
|
||||
)
|
||||
}
|
||||
|
||||
override fun serialize(encoder: Encoder, value: IndexedValue<T>) {
|
||||
originalSerializer.serialize(
|
||||
encoder,
|
||||
Pair(value.index, value.value)
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user