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

commonMain.maryk.core.aggregations.Aggregations.kt Maven / Gradle / Ivy

Go to download

Maryk is a Kotlin Multiplatform library which helps you to store, query and send data in a structured way over multiple platforms. The data store stores any value with a version, so it is possible to request only the changed data or live listen for updates.

The newest version!
package maryk.core.aggregations

import maryk.core.models.SingleValueDataModel
import maryk.core.properties.definitions.MultiTypeDefinition
import maryk.core.properties.definitions.StringDefinition
import maryk.core.properties.definitions.map
import maryk.core.properties.types.TypedValue
import maryk.core.query.RequestContext
import maryk.core.values.ObjectValues

/**
 * For defining aggregations to run on Scan or Get.
 * They are each defined as a pair with a String and the aggregation definition. This way the output can be referred to.
 */
data class Aggregations internal constructor(
    val namedAggregations: Map>
) {
    constructor(
        vararg aggregationPair: Pair>
    ) : this(aggregationPair.toMap())

    companion object : SingleValueDataModel>>, Map>, Aggregations, Companion, RequestContext>(
        singlePropertyDefinitionGetter = { Companion.namedAggregations }
    ) {
        val namedAggregations by map(
            index = 1u,
            getter = Aggregations::namedAggregations,
            keyDefinition = StringDefinition(),
            valueDefinition = MultiTypeDefinition(
                typeEnum = AggregationRequestType
            ),
            toSerializable = { value, _ ->
                value?.mapValues { (_, value) ->
                    TypedValue(value.aggregationType, value)
                }
            },
            fromSerializable = { values ->
                values?.mapValues { (_, value) ->
                    value.value
                }
            }
        )

        override fun invoke(values: ObjectValues) = Aggregations(
            namedAggregations = values(1u)
        )
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy