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

org.apache.tinkerpop.gremlin.ogm.mappers.ObjectDeserializer.kt Maven / Gradle / Ivy

There is a newer version: 0.21.0
Show newest version
package org.apache.tinkerpop.gremlin.ogm.mappers

import org.apache.tinkerpop.gremlin.ogm.reflection.GraphDescription
import org.apache.tinkerpop.gremlin.ogm.reflection.ObjectDescription
import org.apache.tinkerpop.gremlin.ogm.reflection.PropertyDescription
import kotlin.reflect.KParameter

internal class ObjectDeserializer(
        private val graphDescription: GraphDescription,
        private val objectDescription: ObjectDescription,
        private val idProperty: Pair>? = null,
        private val fromVertexParameter: Pair? = null,
        private val toVertexParameter: Pair? = null
) : Mapper, T> {

    override fun invoke(from: Map<*, *>): T {
        val constructorParameters = mutableMapOf()
        constructorParameters.putAll(objectDescription.properties.entries.associate { keyValue ->
            val propertyKey = keyValue.key
            val propertyDescription = keyValue.value
            val serializedPropertyValue = from[propertyKey]
            val deserializer = PropertyDeserializer(graphDescription, propertyDescription)
            val deserializedPropertyValue = deserializer(serializedPropertyValue)
            propertyDescription.parameter to deserializedPropertyValue
        })
        constructorParameters.putAll(objectDescription.nullConstructorParameters.associate { it to null })
        if (idProperty != null) {
            val id = from[idProperty.first]
            constructorParameters[idProperty.second.parameter] = id
        }
        if (fromVertexParameter != null) {
            val fromVertex = from[fromVertexParameter.first]
            constructorParameters[fromVertexParameter.second] = fromVertex
        }
        if (toVertexParameter != null) {
            val toVertex = from[toVertexParameter.first]
            constructorParameters[toVertexParameter.second] = toVertex
        }
        return objectDescription.constructor.callBy(constructorParameters)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy