diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fbbfc661b5..1df2f2c7799 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## 0.17.5 +* `Common`: + * Conversations of number primitives with bounds care +* `Repos`: + * `Common`: + * By default, `getAll` for repos will take all the size of repo as page size + ## 0.17.4 * `Serialization`: diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/PrimitivesConversations.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/PrimitivesConversations.kt new file mode 100644 index 00000000000..fb3a39d19e8 --- /dev/null +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/PrimitivesConversations.kt @@ -0,0 +1,28 @@ +package dev.inmo.micro_utils.common + +/** + * Convert [this] [Long] to [Int] with bounds of [Int.MIN_VALUE] and [Int.MAX_VALUE] + */ +fun Long.toCoercedInt(): Int = coerceIn(Int.MIN_VALUE.toLong(), Int.MAX_VALUE.toLong()).toInt() +/** + * Convert [this] [Long] to [Short] with bounds of [Short.MIN_VALUE] and [Short.MAX_VALUE] + */ +fun Long.toCoercedShort(): Short = coerceIn(Short.MIN_VALUE.toLong(), Short.MAX_VALUE.toLong()).toShort() +/** + * Convert [this] [Long] to [Byte] with bounds of [Byte.MIN_VALUE] and [Byte.MAX_VALUE] + */ +fun Long.toCoercedByte(): Byte = coerceIn(Byte.MIN_VALUE.toLong(), Byte.MAX_VALUE.toLong()).toByte() + +/** + * Convert [this] [Int] to [Short] with bounds of [Short.MIN_VALUE] and [Short.MAX_VALUE] + */ +fun Int.toCoercedShort(): Short = coerceIn(Short.MIN_VALUE.toInt(), Short.MAX_VALUE.toInt()).toShort() +/** + * Convert [this] [Int] to [Byte] with bounds of [Byte.MIN_VALUE] and [Byte.MAX_VALUE] + */ +fun Int.toCoercedByte(): Byte = coerceIn(Byte.MIN_VALUE.toInt(), Byte.MAX_VALUE.toInt()).toByte() + +/** + * Convert [this] [Short] to [Byte] with bounds of [Byte.MIN_VALUE] and [Byte.MAX_VALUE] + */ +fun Short.toCoercedByte(): Byte = coerceIn(Byte.MIN_VALUE.toShort(), Byte.MAX_VALUE.toShort()).toByte() diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/KeyValuePaginationExtensions.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/KeyValuePaginationExtensions.kt index 14fe3068416..1af608a9d54 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/KeyValuePaginationExtensions.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/KeyValuePaginationExtensions.kt @@ -1,15 +1,22 @@ package dev.inmo.micro_utils.repos.pagination +import dev.inmo.micro_utils.common.toCoercedInt import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging import dev.inmo.micro_utils.repos.ReadKeyValueRepo suspend inline fun > REPO.getAll( + pagination: Pagination, @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") crossinline methodCaller: suspend REPO.(Pagination) -> PaginationResult -): List> = getAllWithNextPaging { +): List> = getAllWithNextPaging(pagination) { val result = methodCaller(it) result.changeResultsUnchecked( result.results.mapNotNull { it to (get(it) ?: return@mapNotNull null) } ) } + +suspend inline fun > REPO.getAll( + @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") + crossinline methodCaller: suspend REPO.(Pagination) -> PaginationResult +): List> = getAll(FirstPagePagination(count().toCoercedInt()), methodCaller)