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

hashmap.HashMapMapper.kt Maven / Gradle / Ivy

There is a newer version: 1.4.4
Show newest version
package com.hexagonkt.store.hashmap

import com.hexagonkt.helpers.filterEmpty
import com.hexagonkt.helpers.logger
import com.hexagonkt.serialization.convertToMap
import com.hexagonkt.serialization.convertToObject
import com.hexagonkt.store.Mapper
import kotlin.reflect.KClass
import kotlin.reflect.KProperty1
import kotlin.reflect.full.declaredMemberProperties

class HashMapMapper(private val type: KClass): Mapper {

    override val fields: Map> by lazy {
        logger.time ("REFLECT") { type.declaredMemberProperties }
            .map { it.name to it }
            .toMap()
    }

    override fun toStore(instance: T): Map  =
        instance.convertToMap()
            .filterEmpty()
            .mapKeys { it.key.toString() }
            .mapValues { it.value }

    @Suppress("UNCHECKED_CAST")
    override fun fromStore(map: Map): T =
        map.filterEmpty().convertToObject(type)

    override fun fromStore(property: String, value: Any): Any =
        value
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy