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

divkit.dsl.core.Property.kt Maven / Gradle / Ivy

Go to download

DivKit is an open source Server-Driven UI (SDUI) framework. SDUI is a an emerging technique that leverage the server to build the user interfaces of their mobile app.

There is a newer version: 30.19.0
Show newest version
package divkit.dsl.core

import kotlin.Any
import kotlin.String
import kotlin.Unit
import kotlin.collections.MutableMap

sealed interface Property

class ReferenceProperty internal constructor(
    val name: String,
) : Property

class LiteralProperty internal constructor(
    val value: T,
) : Property

class ExpressionProperty internal constructor(
    val expression: String,
) : Property

fun  reference(name: String): ReferenceProperty = ReferenceProperty(name)

fun  value(value: T): LiteralProperty = LiteralProperty(value)

fun  valueOrNull(value: T?): LiteralProperty? = if (value != null)
    LiteralProperty(value) else null

fun  expression(expression: String): ExpressionProperty =
    ExpressionProperty(expression)

internal fun MutableMap.tryPutProperty(name: String, property: Property?):
        Unit {
    if (property != null) {
        val exhaustive = when (property) {
            is LiteralProperty -> if (property.value is Boolean) {
                put(name, if (property.value) 1 else 0)
            } else {
                put(name, resolveValue(property.value))
            }

            is ReferenceProperty -> put("$$name", property.name)
            is ExpressionProperty -> put(name, property.expression)
        }
    }
}

internal fun resolveValue(value: Any) = if (value is List<*>) {
    @Suppress("UNCHECKED_CAST")
    value.map { (it as? ArrayElement)?.serialize() ?: it }
} else {
    value
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy