Skip to content

Commit

Permalink
Merge pull request #4 from connorwyatt/result
Browse files Browse the repository at this point in the history
Add Result type
  • Loading branch information
connorwyatt authored Nov 20, 2023
2 parents 67d66f6 + c2adf49 commit 2217927
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions result/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependencies {
testImplementation(testingLibraries.jUnit.jupiter)
testImplementation(testingLibraries.strikt.core)

testRuntimeOnly(testingLibraries.jUnit.jupiter.engine)
}
13 changes: 13 additions & 0 deletions result/src/main/kotlin/io/connorwyatt/common/result/Result.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.connorwyatt.common.result

sealed interface Result<out TSuccess, out TFailure> {
fun getOrThrow(): TSuccess =
when (this) {
is Success -> value
is Failure -> throw Exception("Result was a failure: $error")
}

data class Success<TSuccess>(val value: TSuccess) : Result<TSuccess, Nothing>

data class Failure<TFailure>(val error: TFailure) : Result<Nothing, TFailure>
}
26 changes: 26 additions & 0 deletions result/src/test/kotlin/io/connorwyatt/common/result/ResultTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.connorwyatt.common.result

import org.junit.jupiter.api.Test
import strikt.api.expectThat
import strikt.api.expectThrows
import strikt.assertions.isEqualTo

class ResultTests {
@Test
fun `getOrThrow returns the value when Result is a success`() {
val value = "value"

val result = Result.Success(value)

expectThat(result.getOrThrow()).isEqualTo(value)
}

@Test
fun `getOrThrow throws when Result is a failure`() {
val value = "value"

val result = Result.Failure(value)

expectThrows<Exception> { result.getOrThrow() }
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ include(":http")
include(":mongodb")
include(":optional")
include(":rabbitmq")
include(":result")
include(":server")
include(":time")

Expand Down

0 comments on commit 2217927

Please sign in to comment.