All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.devtools.ksp.common.PersistentMap.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-RC2-1.0.28
Show newest version
package com.google.devtools.ksp.common

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.builtins.MapSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
import kotlinx.serialization.json.encodeToStream
import java.io.BufferedInputStream
import java.io.File

private object FileSerializer : KSerializer {
    override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("File", PrimitiveKind.STRING)
    override fun serialize(encoder: Encoder, value: File) = encoder.encodeString(value.path)
    override fun deserialize(decoder: Decoder): File = File(decoder.decodeString())
}

private object SymbolSerializer : KSerializer {
    override val descriptor: SerialDescriptor =
        PrimitiveSerialDescriptor("LookupSymbolWrapper", PrimitiveKind.STRING)
    override fun serialize(encoder: Encoder, value: LookupSymbolWrapper) =
        encoder.encodeString("${value.name}:${value.scope}")
    override fun deserialize(decoder: Decoder): LookupSymbolWrapper {
        val (name, scope) = decoder.decodeString().split(':')
        return LookupSymbolWrapper(name, scope)
    }
}

private val fileToFilesMapSerializer = MapSerializer(FileSerializer, ListSerializer(FileSerializer))
private val fileToSymbolsMapSerializer = MapSerializer(FileSerializer, ListSerializer(SymbolSerializer))

abstract class PersistentMap(
    private val serializer: KSerializer>,
    private val storage: File,
    private val m: MutableMap,
) : MutableMap by m {

    @OptIn(ExperimentalSerializationApi::class)
    fun flush() {
        storage.outputStream().use {
            Json.encodeToStream(serializer, m.toMap(), it)
        }
    }
    override fun toString() = m.toString()

    companion object {
        @JvmStatic
        @OptIn(ExperimentalSerializationApi::class)
        protected fun  deserialize(serializer: KSerializer>, storage: File): MutableMap {
            return if (storage.exists()) {
                BufferedInputStream(storage.inputStream()).use {
                    Json.decodeFromStream(serializer, it).toMutableMap()
                }
            } else {
                mutableMapOf()
            }
        }
    }
}

class FileToFilesMap(
    storage: File
) : PersistentMap>(fileToFilesMapSerializer, storage, deserialize(fileToFilesMapSerializer, storage))

class FileToSymbolsMap(
    storage: File
) : PersistentMap>(
    fileToSymbolsMapSerializer,
    storage,
    deserialize(fileToSymbolsMapSerializer, storage)
)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy