diff --git a/src/commonMain/kotlin/at/asitplus/KmmResult.kt b/src/commonMain/kotlin/at/asitplus/KmmResult.kt index 7c50fdb..a3a9f8e 100644 --- a/src/commonMain/kotlin/at/asitplus/KmmResult.kt +++ b/src/commonMain/kotlin/at/asitplus/KmmResult.kt @@ -85,6 +85,27 @@ class KmmResult private constructor( inline fun map(block: (T) -> R): KmmResult = getOrNull()?.let { success(block(it)) } ?: this as KmmResult + /** + * Transforms this KmmResult into a KmmResult of different success type according to `block` and leaves the failure case untouched + * Avoids nested KmmResults + */ + @Suppress("UNCHECKED_CAST") + inline fun transform(block: (T) -> KmmResult): KmmResult = + getOrNull()?.let { block(it) } ?: this as KmmResult + + + /** + * Returns the encapsulated result of the given [block] function applied to the encapsulated value + * if this instance represents [success][KmmResult.isSuccess] or the + * original encapsulated [Throwable] exception if it is [failure][KmmResult.isFailure]. + * + * This function catches any [Throwable] exception thrown by [block] function and encapsulates it as a failure. + * See [map] for an alternative that rethrows exceptions from `transform` function. + */ + @Suppress("UNCHECKED_CAST") + inline fun mapCatching(block: (T) -> R): KmmResult = unwrap().mapCatching { block(it) }.wrap() + + /** * Transforms this KmmResult's failure-case according to `block` and leaves the success case untouched * (type erasure FTW!) diff --git a/src/commonTest/kotlin/KmmResultTest.kt b/src/commonTest/kotlin/KmmResultTest.kt index 7cc9457..311a3ac 100644 --- a/src/commonTest/kotlin/KmmResultTest.kt +++ b/src/commonTest/kotlin/KmmResultTest.kt @@ -1,9 +1,11 @@ package at.asitplus +import at.asitplus.KmmResult.Companion.success import at.asitplus.KmmResult.Companion.wrap import kotlin.test.* class KmmResultTest { + @Test fun testMap() { assertEquals("1234", KmmResult.success(1234).map { it.toString() }.getOrThrow()) @@ -15,6 +17,16 @@ class KmmResultTest { assertEquals(9, KmmResult.success(3).map { it * 3 }.getOrThrow()) } + @Test + fun testTransform() { + val intResult = success(1234) + val stringResult = success("1234") + assertEquals(stringResult, intResult.transform { success(it.toString()) }) + val throwable = NullPointerException("Null") + val fail: KmmResult = KmmResult.failure(throwable) + assertEquals(fail, fail.transform { success( it * 3 ) }) + } + @Test fun testMapFailure() { assertTrue(