Merge pull request #34 from InsanusMokrassar/0.0.33

attribute builder now may skip nullable values
This commit is contained in:
InsanusMokrassar 2022-02-09 01:27:47 +06:00 committed by GitHub
commit c1c1f847a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 4 deletions

View File

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

View File

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