|
| 1 | +package com.powersync.sync |
| 2 | + |
| 3 | +import kotlinx.serialization.KSerializer |
| 4 | +import kotlinx.serialization.Serializable |
| 5 | +import kotlinx.serialization.descriptors.buildClassSerialDescriptor |
| 6 | +import kotlinx.serialization.encoding.CompositeDecoder |
| 7 | +import kotlinx.serialization.encoding.Decoder |
| 8 | +import kotlinx.serialization.encoding.Encoder |
| 9 | +import kotlinx.serialization.encoding.decodeStructure |
| 10 | +import kotlinx.serialization.json.JsonObject |
| 11 | +import kotlinx.serialization.serializer |
| 12 | + |
| 13 | +@Serializable(with = Instruction.Serializer::class) |
| 14 | +internal sealed interface Instruction { |
| 15 | + @Serializable |
| 16 | + data class LogLine(val severity: String, val line: String): Instruction |
| 17 | + |
| 18 | + @Serializable |
| 19 | + data class UpdateSyncStatus(val status: CoreSyncStatus): Instruction |
| 20 | + |
| 21 | + @Serializable |
| 22 | + data class EstablishSyncStream(val request: JsonObject): Instruction |
| 23 | + |
| 24 | + object FlushSileSystem: Instruction |
| 25 | + object CloseSyncStream: Instruction |
| 26 | + object UnknownInstruction: Instruction |
| 27 | + |
| 28 | + class Serializer : KSerializer<Instruction> { |
| 29 | + private val logLine = serializer<LogLine>() |
| 30 | + private val updateSyncStatus = serializer<UpdateSyncStatus>() |
| 31 | + private val establishSyncStream = serializer<EstablishSyncStream>() |
| 32 | + private val flushFileSystem = buildClassSerialDescriptor(FlushSileSystem::class.qualifiedName!!) {} |
| 33 | + private val closeSyncStream = buildClassSerialDescriptor(CloseSyncStream::class.qualifiedName!!) {} |
| 34 | + |
| 35 | + override val descriptor = |
| 36 | + buildClassSerialDescriptor(SyncLine::class.qualifiedName!!) { |
| 37 | + element("LogLine", logLine.descriptor, isOptional = true) |
| 38 | + element("UpdateSyncStatus", updateSyncStatus.descriptor, isOptional = true) |
| 39 | + element("EstablishSyncStream", establishSyncStream.descriptor, isOptional = true) |
| 40 | + element("FlushFileSystem", flushFileSystem, isOptional = true) |
| 41 | + element("CloseSyncStream", closeSyncStream, isOptional = true) |
| 42 | + } |
| 43 | + |
| 44 | + override fun deserialize(decoder: Decoder): Instruction = |
| 45 | + decoder.decodeStructure(descriptor) { |
| 46 | + val value = |
| 47 | + when (val index = decodeElementIndex(descriptor)) { |
| 48 | + 0 -> decodeSerializableElement(descriptor, 0, logLine) |
| 49 | + 1 -> decodeSerializableElement(descriptor, 1, updateSyncStatus) |
| 50 | + 2 -> decodeSerializableElement(descriptor, 2, establishSyncStream) |
| 51 | + 3 -> FlushSileSystem |
| 52 | + 4 -> CloseSyncStream |
| 53 | + CompositeDecoder.UNKNOWN_NAME, CompositeDecoder.DECODE_DONE -> UnknownInstruction |
| 54 | + else -> error("Unexpected index: $index") |
| 55 | + } |
| 56 | + |
| 57 | + if (decodeElementIndex(descriptor) != CompositeDecoder.DECODE_DONE) { |
| 58 | + // Sync lines are single-key objects, make sure there isn't another one. |
| 59 | + UnknownInstruction |
| 60 | + } else { |
| 61 | + value |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + override fun serialize(encoder: Encoder, value: Instruction) { |
| 66 | + // We don't need this functionality, so... |
| 67 | + throw UnsupportedOperationException("Serializing instructions") |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +@Serializable |
| 73 | +internal class CoreSyncStatus {} |
0 commit comments