-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from matheus-corregiari/release/1.8.1
Release 1.8.1
- Loading branch information
Showing
9 changed files
with
1,349 additions
and
632 deletions.
There are no files selected for viewing
679 changes: 455 additions & 224 deletions
679
toolkit/event-observer/src/main/kotlin/br/com/arch/toolkit/util/_chain.kt
Large diffs are not rendered by default.
Oops, something went wrong.
574 changes: 342 additions & 232 deletions
574
toolkit/event-observer/src/main/kotlin/br/com/arch/toolkit/util/_combine.kt
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
toolkit/event-observer/src/main/kotlin/br/com/arch/toolkit/util/_responseTransform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
@file:Suppress("Filename", "unused") | ||
|
||
package br.com.arch.toolkit.util | ||
|
||
import br.com.arch.toolkit.annotation.Experimental | ||
import br.com.arch.toolkit.result.DataResult | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flowOn | ||
import kotlinx.coroutines.flow.mapNotNull | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
@Experimental | ||
internal fun <T, R, X> Flow<DataResult<Pair<T, R>>>.applyTransformation( | ||
context: CoroutineContext, | ||
transform: ResponseTransform<T, R, X> | ||
) = flowOn(transform.dispatcher).mapNotNull(transform::apply).flowOn(context) | ||
|
||
@Experimental | ||
sealed class ResponseTransform<T, R, X> { | ||
abstract val dispatcher: CoroutineDispatcher | ||
abstract val failMode: Mode | ||
abstract val func: suspend (DataResult<Pair<T, R>>) -> DataResult<X> | ||
abstract val onErrorReturn: (suspend (Throwable) -> DataResult<X>)? | ||
|
||
internal suspend fun apply(data: DataResult<Pair<T, R>>) = | ||
runCatching { func(data) }.let { result -> | ||
val finalResult = result.exceptionOrNull()?.let { error -> | ||
onErrorReturn?.runCatching { invoke(error) } | ||
} ?: result | ||
|
||
when { | ||
finalResult.isFailure -> when (failMode) { | ||
Mode.OMIT_WHEN_FAIL -> null | ||
Mode.ERROR_STATUS_WHEN_FAIL -> | ||
dataResultError(finalResult.exceptionOrNull()) | ||
} | ||
|
||
else -> finalResult.getOrNull() | ||
} | ||
} | ||
|
||
@Experimental | ||
enum class Mode { | ||
ERROR_STATUS_WHEN_FAIL, | ||
OMIT_WHEN_FAIL | ||
} | ||
|
||
@Experimental | ||
class StatusFail<T, R, X>( | ||
override val dispatcher: CoroutineDispatcher, | ||
override val func: suspend (DataResult<Pair<T, R>>) -> DataResult<X> | ||
) : ResponseTransform<T, R, X>() { | ||
override val failMode: Mode = Mode.ERROR_STATUS_WHEN_FAIL | ||
override val onErrorReturn: (suspend (Throwable) -> DataResult<X>)? = null | ||
} | ||
|
||
@Experimental | ||
class OmitFail<T, R, X>( | ||
override val dispatcher: CoroutineDispatcher, | ||
override val func: suspend (DataResult<Pair<T, R>>) -> DataResult<X> | ||
) : ResponseTransform<T, R, X>() { | ||
override val failMode: Mode = Mode.OMIT_WHEN_FAIL | ||
override val onErrorReturn: (suspend (Throwable) -> DataResult<X>)? = null | ||
} | ||
|
||
@Experimental | ||
class Fallback<T, R, X>( | ||
override val dispatcher: CoroutineDispatcher, | ||
override val func: suspend (DataResult<Pair<T, R>>) -> DataResult<X>, | ||
override val onErrorReturn: suspend (Throwable) -> DataResult<X> | ||
) : ResponseTransform<T, R, X>() { | ||
override val failMode: Mode = Mode.ERROR_STATUS_WHEN_FAIL | ||
} | ||
|
||
@Experimental | ||
class Custom<T, R, X>( | ||
override val dispatcher: CoroutineDispatcher, | ||
override val failMode: Mode, | ||
override val func: suspend (DataResult<Pair<T, R>>) -> DataResult<X>, | ||
override val onErrorReturn: suspend (Throwable) -> DataResult<X> | ||
) : ResponseTransform<T, R, X>() | ||
} |
Oops, something went wrong.