Skip to content

Commit

Permalink
Merge pull request #1 from VirtusLab/result
Browse files Browse the repository at this point in the history
custom Result
  • Loading branch information
krzykrucz committed Dec 7, 2019
2 parents c7af734 + b998365 commit 91614c0
Show file tree
Hide file tree
Showing 22 changed files with 682 additions and 54 deletions.
8 changes: 4 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ subprojects {
repo = "maven"
name = "base-types-kt"
desc = "The modelling for success/failure of operations in Kotlin"
userOrg = "kittinunf"
websiteUrl = "https://github.com/krzykrucz/base-types-kt"
vcsUrl = "https://github.com/krzykrucz/base-types-kt"
setLicenses("MIT")
userOrg = "VirtusLab"
websiteUrl = "https://github.com/VirtusLab/base-types-kt"
vcsUrl = "https://github.com/VirtusLab/base-types-kt"
setLicenses("Apache License 2.0")
version.apply {
name = artifactPublish
}
Expand Down
2 changes: 1 addition & 1 deletion result-arrow/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dependencies {

implementation("io.arrow-kt:arrow-core:$arrow")
implementation("io.arrow-kt:arrow-fx:$arrow")
implementation("com.github.kittinunf.result:result:2.2.0")
implementation(project(":result"))


testImplementation("io.kotlintest:kotlintest-runner-junit5:$kotlintest")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.virtuslab.basetypes.result.arrow

import arrow.fx.IO
import com.github.kittinunf.result.Result
import com.github.kittinunf.result.Result.Failure
import com.github.kittinunf.result.Result.Success
import com.github.kittinunf.result.flatMap
import com.virtuslab.basetypes.result.Result
import com.virtuslab.basetypes.result.Result.Failure
import com.virtuslab.basetypes.result.Result.Success
import com.virtuslab.basetypes.result.flatMap

typealias AsyncResult<T, E> = IO<Result<T, E>>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.virtuslab.basetypes.result.arrow

import com.github.kittinunf.result.Result
import com.virtuslab.basetypes.result.Result
import io.kotlintest.shouldBe
import io.kotlintest.shouldThrow
import org.junit.jupiter.api.Test
Expand Down
2 changes: 1 addition & 1 deletion result-reactor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies {
implementation(kotlin("stdlib", kotlinVersion))

implementation("io.projectreactor:reactor-core:$reactor")
implementation("com.github.kittinunf.result:result:2.2.0")
implementation(project(":result"))


testImplementation("io.kotlintest:kotlintest-runner-junit5:$kotlintest")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.virtuslab.basetypes.result.reactor

import com.github.kittinunf.result.Result
import com.github.kittinunf.result.Result.Failure
import com.github.kittinunf.result.Result.Success
import com.github.kittinunf.result.flatMap
import com.github.kittinunf.result.map
import com.github.kittinunf.result.mapError
import com.virtuslab.basetypes.result.Result
import com.virtuslab.basetypes.result.Result.Failure
import com.virtuslab.basetypes.result.Result.Success
import com.virtuslab.basetypes.result.flatMap
import com.virtuslab.basetypes.result.map
import com.virtuslab.basetypes.result.mapError
import reactor.core.publisher.Mono
import reactor.core.publisher.toMono

Expand All @@ -31,11 +31,7 @@ fun <S : Any, E : Exception, S2 : Any> MonoResult<S, E>.flatMapResult(mapper: (S
fun <S : Any, E : Exception, S2 : Any> MonoResult<S, E>.flatMapSuccess(mapper: (S) -> MonoResult<S2, E>): MonoResult<S2, E> =
this.flatMap { result1 ->
when (result1) {
is Success -> try {
mapper(result1.value)
} catch (ex: Exception) {
Failure(ex as E).toMono()
}
is Success -> mapper(result1.value)
is Failure -> Failure(result1.error).toMono()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.virtuslab.basetypes.result.reactor

import com.github.kittinunf.result.Result
import com.virtuslab.basetypes.result.Result
import io.kotlintest.specs.StringSpec
import reactor.core.publisher.Mono
import reactor.core.publisher.toMono
Expand All @@ -24,8 +24,7 @@ internal class MonoResultKtTest : StringSpec() {

monoResult.mapSuccess { if (true) throw runtimeException else "Some other value" }
.test()
.expectNext(Result.error(runtimeException) as Result<String, SomeFailure>)
.verifyComplete()
.verifyError(RuntimeException::class.java)
}

"should handle exception when mapping result success" {
Expand All @@ -34,8 +33,7 @@ internal class MonoResultKtTest : StringSpec() {

monoResult.flatMapResult { if (true) throw runtimeException else Result.success("Some other value") }
.test()
.expectNext(Result.error(runtimeException) as Result<String, SomeFailure>)
.verifyComplete()
.verifyError(RuntimeException::class.java)
}

"should handle exception when flatMapping MonoResult" {
Expand All @@ -44,8 +42,7 @@ internal class MonoResultKtTest : StringSpec() {

monoResult.flatMapSuccess { if (true) throw runtimeException else "Some other value".justMonoResult<String, SomeFailure>() }
.test()
.expectNext(Result.error(runtimeException) as Result<String, SomeFailure>)
.verifyComplete()
.verifyError(RuntimeException::class.java)
}

"should keep error when mapping success" {
Expand Down
2 changes: 1 addition & 1 deletion result-rxjava/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies {
implementation(kotlin("stdlib", kotlinVersion))

implementation("io.reactivex.rxjava2:rxjava:2.2.14")
implementation("com.github.kittinunf.result:result:2.2.0")
implementation(project(":result"))
implementation("io.arrow-kt:arrow-core:$arrow")

testImplementation("io.kotlintest:kotlintest-runner-junit5:$kotlintest")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package com.virtuslab.basetypes.result.rxjava
import arrow.core.None
import arrow.core.Option
import arrow.core.Some
import com.github.kittinunf.result.Result
import com.github.kittinunf.result.Result.Failure
import com.github.kittinunf.result.Result.Success
import com.github.kittinunf.result.flatMap
import com.github.kittinunf.result.map
import com.github.kittinunf.result.mapError
import com.virtuslab.basetypes.result.Result
import com.virtuslab.basetypes.result.Result.Failure
import com.virtuslab.basetypes.result.Result.Success
import com.virtuslab.basetypes.result.flatMap
import com.virtuslab.basetypes.result.map
import com.virtuslab.basetypes.result.mapError
import io.reactivex.Single

typealias SingleResult<T, E> = Single<Result<T, E>>
Expand All @@ -32,11 +32,7 @@ fun <S : Any, E : Exception, S2 : Any> SingleResult<S, E>.flatMapResult(mapper:
fun <S : Any, E : Exception, S2 : Any> SingleResult<S, E>.flatMapSuccess(mapper: (S) -> SingleResult<S2, E>): SingleResult<S2, E> =
this.flatMap { result1 ->
when (result1) {
is Success -> try {
mapper(result1.value)
} catch (ex: Exception) {
Failure(ex as E).toSingle()
}
is Success -> mapper(result1.value)
is Failure -> Failure(result1.error).toSingle()
}
}
Expand Down Expand Up @@ -76,8 +72,6 @@ fun <S : Any, E : Exception> Single<S>.liftResult(errorMapper: (Throwable) -> E)
this.map { Result.success(it) as Result<S, E> }
.onErrorReturn { Result.error(errorMapper(it)) }

//fun <V : Any, E : Exception> SingleResult<V, E>.any(predicate: (V) -> Boolean): Boolean = TODO()

fun <S> S.toSingle(): Single<S> = Single.just(this)

// TODO test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.virtuslab.basetypes.result.rxjava

import com.github.kittinunf.result.Result
import com.virtuslab.basetypes.result.Result
import io.kotlintest.specs.StringSpec
import io.reactivex.Single

Expand All @@ -16,34 +16,31 @@ internal class SingleResultKtTest: StringSpec() {
.assertComplete()
}

"should handle exception when mapping success" {
"should propagate exception when mapping success" {
val monoResult: SingleResult<String, SomeFailure> = "Some value".justSingleResult()
val runtimeException = RuntimeException()

monoResult.mapSuccess { if (true) throw runtimeException else "Some other value" }
.test()
.assertResult(Result.error(runtimeException) as Result<String, SomeFailure>)
.assertComplete()
.assertError(runtimeException)
}

"should handle exception when mapping result success" {
"should propagate exception when mapping result success" {
val monoResult: SingleResult<String, SomeFailure> = "Some value".justSingleResult()
val runtimeException = RuntimeException()

monoResult.flatMapResult { if (true) throw runtimeException else Result.success("Some other value") }
.test()
.assertResult(Result.error(runtimeException) as Result<String, SomeFailure>)
.assertComplete()
.assertError(runtimeException)
}

"should handle exception when flatMapping SingleResult" {
"should propagate exception when flatMapping SingleResult" {
val monoResult: SingleResult<String, SomeFailure> = "Some value".justSingleResult()
val runtimeException = RuntimeException()

monoResult.flatMapSuccess { if (true) throw runtimeException else "Some other value".justSingleResult<String, SomeFailure>() }
.test()
.assertResult(Result.error(runtimeException) as Result<String, SomeFailure>)
.assertComplete()
.assertError(runtimeException)
}

"should keep error when mapping success" {
Expand Down
2 changes: 2 additions & 0 deletions result/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/build

21 changes: 21 additions & 0 deletions result/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Kittinun Vantasin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
15 changes: 15 additions & 0 deletions result/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
sourceSets {
getByName("main").java.srcDirs("src/main/kotlin")
getByName("test").java.srcDirs("src/main/kotlin")
}

dependencies {
val kotlinVersion: String by project
val junit: String by project

implementation(kotlin("stdlib", kotlinVersion))

testImplementation("junit:junit:4.12")


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.virtuslab.basetypes.result

class NoException private constructor() : Exception()
Loading

0 comments on commit 91614c0

Please sign in to comment.