Compare commits

..

15 Commits
0.9.2 ... 0.9.6

6 changed files with 106 additions and 19 deletions

View File

@@ -1,5 +1,32 @@
# Changelog # Changelog
## 0.9.6
* `Repos`:
* `Exposed`:
* Fix in `ExposedOneToManyKeyValueRepo` - now it will not use `deleteIgnoreWhere`
## 0.9.5
* `Versions`:
* `Klock`: `2.4.12` -> `2.4.13`
## 0.9.4
* `Pagination`:
* `Common`:
* Add several `optionallyReverse` functions
* `Common`:
* Changes in `Either`:
* Now `Either` uses `optionalT1` and `optionalT2` as main properties
* `Either#t1` and `Either#t2` are deprecated
* New extensions `Either#mapOnFirst` and `Either#mapOnSecond`
## 0.9.3
* `Versions`:
* `UUID`: `0.3.1` -> `0.4.0`
## 0.9.2 ## 0.9.2
* `Versions`: * `Versions`:

View File

@@ -6,7 +6,7 @@ import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.* import kotlinx.serialization.encoding.*
/** /**
* Realization of this interface will contains at least one not null - [t1] or [t2] * Realization of this interface will contains at least one not null - [optionalT1] or [optionalT2]
* *
* @see EitherFirst * @see EitherFirst
* @see EitherSecond * @see EitherSecond
@@ -14,11 +14,19 @@ import kotlinx.serialization.encoding.*
* @see Either.Companion.second * @see Either.Companion.second
* @see Either.onFirst * @see Either.onFirst
* @see Either.onSecond * @see Either.onSecond
* @see Either.mapOnFirst
* @see Either.mapOnSecond
*/ */
@Serializable(EitherSerializer::class) @Serializable(EitherSerializer::class)
sealed interface Either<T1, T2> { sealed interface Either<T1, T2> {
val optionalT1: Optional<T1>
val optionalT2: Optional<T2>
@Deprecated("Use optionalT1 instead", ReplaceWith("optionalT1"))
val t1: T1? val t1: T1?
get() = optionalT1.dataOrNull()
@Deprecated("Use optionalT2 instead", ReplaceWith("optionalT2"))
val t2: T2? val t2: T2?
get() = optionalT2.dataOrNull()
companion object { companion object {
fun <T1, T2> serializer( fun <T1, T2> serializer(
@@ -93,25 +101,25 @@ class EitherSerializer<T1, T2>(
} }
/** /**
* This type [Either] will always have not nullable [t1] * This type [Either] will always have not nullable [optionalT1]
*/ */
@Serializable @Serializable
data class EitherFirst<T1, T2>( data class EitherFirst<T1, T2>(
override val t1: T1 override val t1: T1
) : Either<T1, T2> { ) : Either<T1, T2> {
override val t2: T2? override val optionalT1: Optional<T1> = t1.optional
get() = null override val optionalT2: Optional<T2> = Optional.absent()
} }
/** /**
* This type [Either] will always have not nullable [t2] * This type [Either] will always have not nullable [optionalT2]
*/ */
@Serializable @Serializable
data class EitherSecond<T1, T2>( data class EitherSecond<T1, T2>(
override val t2: T2 override val t2: T2
) : Either<T1, T2> { ) : Either<T1, T2> {
override val t1: T1? override val optionalT1: Optional<T1> = Optional.absent()
get() = null override val optionalT2: Optional<T2> = t2.optional
} }
/** /**
@@ -124,23 +132,35 @@ inline fun <T1, T2> Either.Companion.first(t1: T1): Either<T1, T2> = EitherFirst
inline fun <T1, T2> Either.Companion.second(t2: T2): Either<T1, T2> = EitherSecond(t2) inline fun <T1, T2> Either.Companion.second(t2: T2): Either<T1, T2> = EitherSecond(t2)
/** /**
* Will call [block] in case when [Either.t1] of [this] is not null * Will call [block] in case when [this] is [EitherFirst]
*/ */
inline fun <T1, T2, E : Either<T1, T2>> E.onFirst(block: (T1) -> Unit): E { inline fun <T1, T2, E : Either<T1, T2>> E.onFirst(block: (T1) -> Unit): E {
val t1 = t1 optionalT1.onPresented(block)
t1 ?.let(block)
return this return this
} }
/** /**
* Will call [block] in case when [Either.t2] of [this] is not null * Will call [block] in case when [this] is [EitherSecond]
*/ */
inline fun <T1, T2, E : Either<T1, T2>> E.onSecond(block: (T2) -> Unit): E { inline fun <T1, T2, E : Either<T1, T2>> E.onSecond(block: (T2) -> Unit): E {
val t2 = t2 optionalT2.onPresented(block)
t2 ?.let(block)
return this return this
} }
/**
* @return Result of [block] if [this] is [EitherFirst]
*/
inline fun <T1, R> Either<T1, *>.mapOnFirst(block: (T1) -> R): R? {
return optionalT1.mapOnPresented(block)
}
/**
* @return Result of [block] if [this] is [EitherSecond]
*/
inline fun <T2, R> Either<*, T2>.mapOnSecond(block: (T2) -> R): R? {
return optionalT2.mapOnPresented(block)
}
inline fun <reified T1, reified T2> Any.either() = when (this) { inline fun <reified T1, reified T2> Any.either() = when (this) {
is T1 -> Either.first<T1, T2>(this) is T1 -> Either.first<T1, T2>(this)
is T2 -> Either.second<T1, T2>(this) is T2 -> Either.second<T1, T2>(this)

View File

@@ -14,11 +14,11 @@ kotlin_exposed_version=0.37.2
ktor_version=1.6.7 ktor_version=1.6.7
klockVersion=2.4.12 klockVersion=2.4.13
github_release_plugin_version=2.2.12 github_release_plugin_version=2.2.12
uuidVersion=0.3.1 uuidVersion=0.4.0
# ANDROID # ANDROID
@@ -40,10 +40,10 @@ crypto_js_version=4.1.1
# Dokka # Dokka
dokka_version=1.6.0 dokka_version=1.6.10
# Project data # Project data
group=dev.inmo group=dev.inmo
version=0.9.2 version=0.9.6
android_code_version=92 android_code_version=96

View File

@@ -38,3 +38,31 @@ fun <T> Set<T>.paginate(with: Pagination): PaginationResult<T> {
size.toLong() size.toLong()
) )
} }
fun <T> Iterable<T>.optionallyReverse(reverse: Boolean): Iterable<T> = when (this) {
is List<T> -> optionallyReverse(reverse)
is Set<T> -> optionallyReverse(reverse)
else -> if (reverse) {
reversed()
} else {
this
}
}
fun <T> List<T>.optionallyReverse(reverse: Boolean): List<T> = if (reverse) {
reversed()
} else {
this
}
fun <T> Set<T>.optionallyReverse(reverse: Boolean): Set<T> = if (reverse) {
reversed().toSet()
} else {
this
}
inline fun <reified T> Array<T>.optionallyReverse(reverse: Boolean) = if (reverse) {
Array(size) {
get(lastIndex - it)
}
} else {
this
}

View File

@@ -26,3 +26,15 @@ fun Pagination.reverse(datasetSize: Long): SimplePagination {
* Shortcut for [reverse] * Shortcut for [reverse]
*/ */
fun Pagination.reverse(objectsCount: Int) = reverse(objectsCount.toLong()) fun Pagination.reverse(objectsCount: Int) = reverse(objectsCount.toLong())
fun Pagination.optionallyReverse(objectsCount: Int, reverse: Boolean) = if (reverse) {
reverse(objectsCount)
} else {
this
}
fun Pagination.optionallyReverse(objectsCount: Long, reverse: Boolean) = if (reverse) {
reverse(objectsCount)
} else {
this
}

View File

@@ -48,7 +48,7 @@ open class ExposedOneToManyKeyValueRepo<Key, Value>(
transaction(database) { transaction(database) {
toRemove.keys.flatMap { k -> toRemove.keys.flatMap { k ->
toRemove[k] ?.mapNotNull { v -> toRemove[k] ?.mapNotNull { v ->
if (deleteIgnoreWhere { keyColumn.eq(k).and(valueColumn.eq(v)) } > 0 ) { if (deleteWhere { keyColumn.eq(k).and(valueColumn.eq(v)) } > 0 ) {
k to v k to v
} else { } else {
null null