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

org.jetbrains.kotlinx.ggdsl.dsl.internal.mapToNamedData.kt Maven / Gradle / Ivy

There is a newer version: 0.4.0-dev-15
Show newest version
package org.jetbrains.kotlinx.ggdsl.dsl.internal

import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalDateTime
import org.jetbrains.kotlinx.ggdsl.ir.data.TypedList
import kotlin.reflect.typeOf

/**
 * Casts a default data map to a typed one.
 *
 * @return [Map] with the same keys but with [TypedList] instead of untyped lists values.
 */
public fun Map>.toTyped(): Map {
    return map { (key, list)-> key to list.toTypedList() }.toMap()
}

/**
 * Casts an untyped lists to a typed one.
 *
 * @return [TypedList] with the same values (with dynamic type inference).
 */
public fun List.toTypedList(): TypedList {
    val type = when {
        all { it is Int } -> typeOf()
        all { it is Long } -> typeOf()
        all { it is Double } -> typeOf()
        all { it is Float } -> typeOf()
        all { it is Char } -> typeOf()
        all { it is Boolean } -> typeOf()
        all { it is String } -> typeOf()
        all { it is Instant } -> typeOf()
        all { it is LocalDateTime } -> typeOf()
        all { it is LocalDate } -> typeOf()
        else -> typeOf() // todo
    }
    return TypedList(type, this)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy