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

divkit.dsl.Template.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.28.0
Show newest version
package divkit.dsl

import divkit.dsl.core.*
import divkit.dsl.scope.DivScope
import divkit.dsl.scope.TemplateScope
import kotlin.Any
import kotlin.String
import kotlin.collections.Iterator
import kotlin.collections.List
import kotlin.collections.MutableMap


class Template internal constructor(
    val name: String,
    val div: T,
    val dependencies: List>,
    val supplements: Map, Supplement>,
)


fun  template(name: String, builder: TemplateScope.() -> T): Template {
    val scope = TemplateScope()
    val template = builder.invoke(scope)
    return Template(
        name = name,
        div = template,
        dependencies = scope.templates.values.toList(),
        supplements = scope.supplements,
    )
}


// JvmName is set to prevent jvm signature clash
@JvmName("renderDiv")
fun  DivScope.render(
    template: Template, vararg
    resolutions: Resolution
): Component = internalRenderDiv(
    template,
    resolutions.iterator()
)


// JvmName is set to prevent jvm signature clash
@JvmName("renderComponent")
fun  DivScope.render(
    template: Template>, vararg
    resolutions: Resolution
): Component = internalRenderComponent(
    template,
    resolutions.iterator()
)


fun  DivScope.render(template: Template, resolutions: List>):
        Component = internalRenderDiv(template, resolutions.iterator())


private fun  DivScope.internalRenderDiv(
    template: Template,
    resolutions: Iterator>
): Component {
    registerTemplates(this.templates, template)
    registerSupplements(this.supplements, template)
    val properties = mutableMapOf()
    for (resolution in resolutions) {
        when (resolution) {
            is FinalResolution -> {
                properties[resolution.reference.name] = resolveValue(resolution.value)
            }

            is ProxyResolution -> {
                properties["$" + resolution.reference.name] = resolution.proxy.name
            }
        }
    }
    return Component(
        template = template,
        properties = properties
    )
}

private fun  DivScope.internalRenderComponent(
    template: Template>,
    resolutions: Iterator>
): Component {
    registerTemplates(this.templates, template)
    registerSupplements(this.supplements, template)
    val properties = mutableMapOf()
    for (resolution in resolutions) {
        when (resolution) {
            is FinalResolution -> {
                properties[resolution.reference.name] = resolveValue(resolution.value)
            }

            is ProxyResolution -> {
                properties["$" + resolution.reference.name] = resolution.proxy.name
            }
        }
    }
    return Component(
        template = Template(
            name = template.name,
            div = template.div.template.div,
            dependencies = template.dependencies,
            supplements = template.supplements
        ),
        properties = properties
    )
}


private fun registerTemplates(
    registry: MutableMap>,
    template: Template
) {
    registry[template.name] = template
    for (dependency in template.dependencies) {
        registerTemplates(registry, dependency)
    }
}

private fun registerSupplements(
    registry: MutableMap, Supplement>,
    template: Template
) {
    for ((key, value) in template.supplements) {
        val existing = registry[key]
        if (existing != null) {
            registry[key] = existing.extend(value)
        } else {
            registry[key] = value
        }
    }
    for (dependency in template.dependencies) {
        registerSupplements(registry, dependency)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy