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

io.github.databob.FunktionaleGenerator.kt Maven / Gradle / Ivy

The newest version!
package io.github.databob

import org.funktionale.either.Either
import org.funktionale.either.toLeft
import org.funktionale.either.toRight
import org.funktionale.option.Option
import org.funktionale.option.toOption
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import kotlin.reflect.full.defaultType
import kotlin.reflect.jvm.javaType

class FunktionaleGenerator : io.github.databob.Generator {

    private val lookup by lazy {
        mapOf, Databob) -> Any>(
            Option::class.defaultType.javaType to { t, d -> d.mk(Class.forName(t[0].typeName).kotlin).toOption().filter { d.mk().toss() } },
            Either::class.defaultType.javaType to { t, d ->
                val pair = Pair(d.mk(Class.forName(t[0].typeName).kotlin), d.mk(Class.forName(t[1].typeName).kotlin))
                if (d.mk().toss()) pair.toRight() else pair.toLeft()
            }
        )
    }

    override fun mk(type: Type, databob: Databob): Any? = when (type) {
        is ParameterizedType -> {
            lookup[type.rawType]?.invoke(type.actualTypeArguments, databob)
        }
        else -> null
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy