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

commonMain.aws.sdk.kotlin.hll.dynamodbmapper.operations.Query.kt Maven / Gradle / Ivy

There is a newer version: 1.3.99-beta
Show newest version
// Code generated by dynamodb-mapper-ops-codegen. DO NOT EDIT!

package aws.sdk.kotlin.hll.dynamodbmapper.operations

import aws.sdk.kotlin.hll.dynamodbmapper.expressions.BooleanExpr
import aws.sdk.kotlin.hll.dynamodbmapper.expressions.Filter
import aws.sdk.kotlin.hll.dynamodbmapper.expressions.KeyFilter
import aws.sdk.kotlin.hll.dynamodbmapper.expressions.internal.FilterImpl
import aws.sdk.kotlin.hll.dynamodbmapper.expressions.internal.ParameterizingExpressionVisitor
import aws.sdk.kotlin.hll.dynamodbmapper.expressions.internal.toExpression
import aws.sdk.kotlin.hll.dynamodbmapper.internal.withWrappedClient
import aws.sdk.kotlin.hll.dynamodbmapper.items.ItemSchema
import aws.sdk.kotlin.hll.dynamodbmapper.model.IndexSpec
import aws.sdk.kotlin.hll.dynamodbmapper.model.TableSpec
import aws.sdk.kotlin.hll.dynamodbmapper.model.toItem
import aws.sdk.kotlin.hll.dynamodbmapper.pipeline.internal.HReqContextImpl
import aws.sdk.kotlin.hll.dynamodbmapper.pipeline.internal.MapperContextImpl
import aws.sdk.kotlin.hll.dynamodbmapper.pipeline.internal.Operation
import aws.sdk.kotlin.services.dynamodb.model.ConsumedCapacity
import aws.sdk.kotlin.services.dynamodb.model.ReturnConsumedCapacity
import aws.sdk.kotlin.services.dynamodb.model.Select
import aws.smithy.kotlin.runtime.ExperimentalApi
import kotlin.collections.List
import aws.sdk.kotlin.services.dynamodb.model.QueryRequest as LowLevelQueryRequest
import aws.sdk.kotlin.services.dynamodb.model.QueryResponse as LowLevelQueryResponse

@ExperimentalApi
public interface QueryRequest {
    @ExperimentalApi
    public companion object { }

    public val consistentRead: Boolean?
    public val exclusiveStartKey: T?
    public val filter: BooleanExpr?
    public val keyCondition: KeyFilter?
    public val limit: Int?
    public val returnConsumedCapacity: ReturnConsumedCapacity?
    public val scanIndexForward: Boolean?
    public val select: Select?
}

private data class QueryRequestImpl(
    override val consistentRead: Boolean?,
    override val exclusiveStartKey: T?,
    override val filter: BooleanExpr?,
    override val keyCondition: KeyFilter?,
    override val limit: Int?,
    override val returnConsumedCapacity: ReturnConsumedCapacity?,
    override val scanIndexForward: Boolean?,
    override val select: Select?,
): QueryRequest

/**
 * A DSL-style builder for instances of [QueryRequest]
 */
@ExperimentalApi
public class QueryRequestBuilder {
    public var consistentRead: Boolean? = null
    public var exclusiveStartKey: T? = null

    public var filter: BooleanExpr? = null

    public fun filter(block: Filter.() -> BooleanExpr?) {
        filter = FilterImpl.run(block)
    }

    public var keyCondition: KeyFilter? = null
    public var limit: Int? = null
    public var returnConsumedCapacity: ReturnConsumedCapacity? = null
    public var scanIndexForward: Boolean? = null
    public var select: Select? = null

    public fun build(): QueryRequest {
        val consistentRead = consistentRead
        val exclusiveStartKey = exclusiveStartKey
        val filter = filter
        val keyCondition = keyCondition
        val limit = limit
        val returnConsumedCapacity = returnConsumedCapacity
        val scanIndexForward = scanIndexForward
        val select = select

        return QueryRequestImpl(
            consistentRead,
            exclusiveStartKey,
            filter,
            keyCondition,
            limit,
            returnConsumedCapacity,
            scanIndexForward,
            select,
        )
    }
}

@ExperimentalApi
public fun  QueryRequest.toBuilder(): QueryRequestBuilder = QueryRequestBuilder().apply {
    consistentRead = [email protected]
    exclusiveStartKey = [email protected]
    filter = [email protected]
    keyCondition = [email protected]
    limit = [email protected]
    returnConsumedCapacity = [email protected]
    scanIndexForward = [email protected]
    select = [email protected]
}

@ExperimentalApi
public fun  QueryRequest.copy(block: QueryRequestBuilder.() -> Unit): QueryRequest =
    toBuilder().apply(block).build()

@ExperimentalApi
public fun  QueryRequest(block: QueryRequestBuilder.() -> Unit): QueryRequest =
    QueryRequestBuilder().apply(block).build()

private fun  QueryRequest.convert(
    indexName: String?, 
    tableName: String?, 
    schema: ItemSchema,
) = LowLevelQueryRequest {
    consistentRead = [email protected]
    limit = [email protected]
    returnConsumedCapacity = [email protected]
    scanIndexForward = [email protected]
    select = [email protected]
    [email protected]?.let { exclusiveStartKey = schema.converter.convertTo(it) }
    this.indexName = indexName
    this.tableName = tableName

    val expressionVisitor = ParameterizingExpressionVisitor()
    filterExpression = [email protected]?.accept(expressionVisitor)
    keyConditionExpression = [email protected]?.toExpression(schema)?.accept(expressionVisitor)
    expressionAttributeNames = expressionVisitor.expressionAttributeNames()
    expressionAttributeValues = expressionVisitor.expressionAttributeValues()
}

@ExperimentalApi
public interface QueryResponse {
    @ExperimentalApi
    public companion object { }

    public val consumedCapacity: ConsumedCapacity?
    public val count: Int
    public val items: List?
    public val lastEvaluatedKey: T?
    public val scannedCount: Int
}

private data class QueryResponseImpl(
    override val consumedCapacity: ConsumedCapacity?,
    override val count: Int,
    override val items: List?,
    override val lastEvaluatedKey: T?,
    override val scannedCount: Int,
): QueryResponse

/**
 * A DSL-style builder for instances of [QueryResponse]
 */
@ExperimentalApi
public class QueryResponseBuilder {
    public var consumedCapacity: ConsumedCapacity? = null
    public var count: Int? = null
    public var items: List? = null
    public var lastEvaluatedKey: T? = null
    public var scannedCount: Int? = null

    public fun build(): QueryResponse {
        val consumedCapacity = consumedCapacity
        val count = requireNotNull(count) { "Missing value for count" }
        val items = items
        val lastEvaluatedKey = lastEvaluatedKey
        val scannedCount = requireNotNull(scannedCount) { "Missing value for scannedCount" }

        return QueryResponseImpl(
            consumedCapacity,
            count,
            items,
            lastEvaluatedKey,
            scannedCount,
        )
    }
}

@ExperimentalApi
public fun  QueryResponse.toBuilder(): QueryResponseBuilder = QueryResponseBuilder().apply {
    consumedCapacity = [email protected]
    count = [email protected]
    items = [email protected]
    lastEvaluatedKey = [email protected]
    scannedCount = [email protected]
}

@ExperimentalApi
public fun  QueryResponse.copy(block: QueryResponseBuilder.() -> Unit): QueryResponse =
    toBuilder().apply(block).build()

@ExperimentalApi
public fun  QueryResponse(block: QueryResponseBuilder.() -> Unit): QueryResponse =
    QueryResponseBuilder().apply(block).build()

private fun  LowLevelQueryResponse.convert(schema: ItemSchema) = QueryResponse {
    consumedCapacity = [email protected]
    count = [email protected]
    scannedCount = [email protected]
    lastEvaluatedKey = [email protected]?.toItem()?.let(schema.converter::convertFrom)
    items = [email protected]?.map { schema.converter.convertFrom(it.toItem()) }
}

internal fun  queryOperation(spec: IndexSpec) = Operation(
    initialize = { highLevelReq: QueryRequest -> HReqContextImpl(highLevelReq, spec.schema, MapperContextImpl(spec, "Query")) },
    serialize = { highLevelReq, schema -> highLevelReq.convert(spec.indexName, spec.tableName, schema) },
    lowLevelInvoke = { lowLevelReq ->
        spec.mapper.client.withWrappedClient { client ->
            client.query(lowLevelReq)
        }
    },
    deserialize = LowLevelQueryResponse::convert,
    interceptors = spec.mapper.config.interceptors,
)

internal fun  queryOperation(spec: TableSpec) = Operation(
    initialize = { highLevelReq: QueryRequest -> HReqContextImpl(highLevelReq, spec.schema, MapperContextImpl(spec, "Query")) },
    serialize = { highLevelReq, schema -> highLevelReq.convert(indexName = null, spec.tableName, schema) },
    lowLevelInvoke = { lowLevelReq ->
        spec.mapper.client.withWrappedClient { client ->
            client.query(lowLevelReq)
        }
    },
    deserialize = LowLevelQueryResponse::convert,
    interceptors = spec.mapper.config.interceptors,
)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy