com.yandex.div.dsl.context.TemplateContext.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 com.yandex.div.dsl.context
import com.fasterxml.jackson.annotation.JsonAnyGetter
import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.databind.node.ObjectNode
import com.yandex.div.dsl.LiteralProperty
import com.yandex.div.dsl.Property
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
@Deprecated("Use divkit.dsl framework")
class TemplateContext {
@JvmField
@JsonAnyGetter
internal val templates = mutableMapOf()
@JvmField
@JsonIgnore
internal var asString: String? = null
@JvmField
@JsonIgnore
internal var asNode: ObjectNode? = null
fun copyFrom(source: TemplateContext) {
clearCache()
templates.putAll(source.templates)
}
fun listOf(vararg items: LiteralProperty?): Property> {
return LiteralProperty(items.mapNotNull { property -> property?.value })
}
internal fun clearCache() {
asString = null
asNode = null
}
}
@OptIn(ExperimentalContracts::class)
@Deprecated("Use divkit.dsl framework")
fun templates(initialize: TemplateContext.() -> Unit): TemplateContext {
contract {
callsInPlace(initialize, InvocationKind.EXACTLY_ONCE)
}
val context = TemplateContext()
context.initialize()
return context
}
fun > TemplateContext.define(
templateName: String,
templateProperty: P
) {
clearCache()
templates[templateName] = templateProperty.value
}