attribute builder now may skip nullable values

This commit is contained in:
InsanusMokrassar 2022-02-09 01:26:33 +06:00
parent ca8f927304
commit 1f8298f626
2 changed files with 11 additions and 4 deletions

View File

@ -24,9 +24,10 @@ class UIKitAttributeValueBuilder {
fun AttrsBuilder<*>.buildAndAddAttribute(
attributeName: String,
skipNullValues: Boolean = true,
block: AttributeBuilder.() -> Unit
) {
buildAttribute(attributeName, block).let {
buildAttribute(attributeName, skipNullValues, block).let {
attr(it.first, it.second)
}
}

View File

@ -2,10 +2,15 @@ package dev.inmo.jsuikit.utils
class AttributeBuilder (
val attributeName: String,
val skipNullValues: Boolean = true,
private val parametersPreset: MutableMap<String, String?> = mutableMapOf()
) {
fun add(k: String, v: Any? = null) = parametersPreset.set(k, v ?.toString())
fun add(k: String, v: Any? = null) {
if (v != null || !skipNullValues) {
parametersPreset[k] = v ?.toString()
}
}
infix fun String.to(value: Any?) = add(this, value)
operator fun String.unaryPlus() = add(this, null)
@ -16,6 +21,7 @@ class AttributeBuilder (
)
}
inline fun buildAttribute(attributeName: String, block: AttributeBuilder.() -> Unit) = AttributeBuilder(
attributeName
inline fun buildAttribute(attributeName: String, skipNullValues: Boolean = true, block: AttributeBuilder.() -> Unit) = AttributeBuilder(
attributeName,
skipNullValues
).apply(block).build()