divkit.dsl.core.Property.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-json-builder Show documentation
Show all versions of kotlin-json-builder Show documentation
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.
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
}