Skip to content

Commit

Permalink
Fix Kotlin Result migration issues to 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mars885 committed Aug 12, 2024
1 parent 469a626 commit 6fc2e70
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.paulrybitskyi.gamedge.common.domain.common.extensions

import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok
import com.github.michaelbull.result.Result
import com.github.michaelbull.result.map
import com.github.michaelbull.result.mapEither
Expand All @@ -38,9 +36,9 @@ fun <T> Flow<DomainResult<T>>.onEachFailure(action: suspend (Error) -> Unit): Fl
}

fun <T> Flow<DomainResult<T>>.resultOrError(): Flow<T> {
return map {
if (it is Ok) return@map it.value
if (it is Err) throw DomainException(it.error)
return map { result ->
if (result.isOk) return@map result.value
if (result.isErr) throw DomainException(result.error)

error("The result is neither Ok nor Err.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok
import com.github.michaelbull.result.Result

fun <T> T.asSuccess(): Ok<T> = Ok(this)
fun <T> T.asSuccess(): Result<T, Nothing> = Ok(this)

fun <T> T.asFailure(): Err<T> = Err(this)
fun <T> T.asFailure(): Result<Nothing, T> = Err(this)

suspend fun <V, E> Result<V, E>.onSuccess(action: suspend (V) -> Unit): Result<V, E> {
if (this is Ok) {
if (isOk) {
action(value)
}

return this
}

suspend fun <V, E> Result<V, E>.onFailure(action: suspend (E) -> Unit): Result<V, E> {
if (this is Err) {
if (isErr) {
action(error)
}

Expand Down

0 comments on commit 6fc2e70

Please sign in to comment.