commonMain.aws.sdk.kotlin.hll.dynamodbmapper.util.AttributeValues.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dynamodb-mapper-jvm Show documentation
Show all versions of dynamodb-mapper-jvm Show documentation
High level DynamoDbMapper client
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package aws.sdk.kotlin.hll.dynamodbmapper.util
import aws.sdk.kotlin.services.dynamodb.model.AttributeValue
import kotlin.jvm.JvmName
internal val NULL_ATTR = AttributeValue.Null(true)
internal fun attr(value: Boolean?) = value?.let(AttributeValue::Bool) ?: NULL_ATTR
internal fun attr(value: ByteArray?) = value?.let(AttributeValue::B) ?: NULL_ATTR
@JvmName("attrListAny")
internal fun attr(value: List?) = attr(value?.map(::dynamicAttr))
internal fun attr(value: List?) = value?.let(AttributeValue::L) ?: NULL_ATTR
@JvmName("attrMapStringAny")
internal fun attr(value: Map?) = attr(value?.mapValues { (_, v) -> dynamicAttr(v) })
internal fun attr(value: Map?) = value?.let(AttributeValue::M) ?: NULL_ATTR
@Suppress("UNUSED_PARAMETER")
internal fun attr(value: Nothing?) = NULL_ATTR
internal fun attr(value: Number?) = value?.let { AttributeValue.N(it.toString()) } ?: NULL_ATTR
@JvmName("attrSetByteArray")
internal fun attr(value: Set?) = value?.let { AttributeValue.Bs(it.toList()) } ?: NULL_ATTR
@JvmName("attrSetNumber")
internal fun attr(value: Set?) = value?.let { AttributeValue.Ns(it.map(Number::toString)) } ?: NULL_ATTR
@JvmName("attrSetString")
internal fun attr(value: Set?) = value?.let { AttributeValue.Ss(it.toList()) } ?: NULL_ATTR
internal fun attr(value: String?) = value?.let(AttributeValue::S) ?: NULL_ATTR
@JvmName("attrSetUByte")
internal fun attr(value: Set?) = value?.let { AttributeValue.Ns(it.map(UByte::toString)) } ?: NULL_ATTR
@JvmName("attrSetUInt")
internal fun attr(value: Set?) = value?.let { AttributeValue.Ns(it.map(UInt::toString)) } ?: NULL_ATTR
@JvmName("attrSetULong")
internal fun attr(value: Set?) = value?.let { AttributeValue.Ns(it.map(ULong::toString)) } ?: NULL_ATTR
@JvmName("attrSetUShort")
internal fun attr(value: Set?) = value?.let { AttributeValue.Ns(it.map(UShort::toString)) } ?: NULL_ATTR
internal fun attr(value: UByte?) = value?.let { AttributeValue.N(it.toString()) } ?: NULL_ATTR
internal fun attr(value: UInt?) = value?.let { AttributeValue.N(it.toString()) } ?: NULL_ATTR
internal fun attr(value: ULong?) = value?.let { AttributeValue.N(it.toString()) } ?: NULL_ATTR
internal fun attr(value: UShort?) = value?.let { AttributeValue.N(it.toString()) } ?: NULL_ATTR
@Suppress("UNCHECKED_CAST")
internal fun dynamicAttr(value: Any?): AttributeValue = when (value) {
null -> NULL_ATTR
is AttributeValue -> value
is Boolean -> attr(value)
is ByteArray -> attr(value)
is List<*> -> attr(value)
is Map<*, *> -> attr(value as Map)
is Number -> attr(value)
is Set<*> -> when (val type = value.firstOrNull()) { // Attempt to determine set type by first element
null -> attr(value as Set) // FIXME Is this a bad idea for the empty set case?
is ByteArray -> attr(value as Set)
is Number -> attr(value as Set)
is String -> attr(value as Set)
is UByte -> attr(value as Set)
is UInt -> attr(value as Set)
is ULong -> attr(value as Set)
is UShort -> attr(value as Set)
else -> error("Unsupported set element type $type")
}
is String -> attr(value)
else -> error("Unsupported attribute value type ${value::class}")
}