diff --git a/example/src/main/kotlin/com/github/michaelbull/result/example/Application.kt b/example/src/main/kotlin/com/github/michaelbull/result/example/Application.kt index 1e325ad..b2c8dc3 100644 --- a/example/src/main/kotlin/com/github/michaelbull/result/example/Application.kt +++ b/example/src/main/kotlin/com/github/michaelbull/result/example/Application.kt @@ -45,10 +45,17 @@ import io.ktor.server.routing.post import io.ktor.server.routing.routing fun main() { - embeddedServer(Netty, port = 8080, host = "0.0.0.0") { - configureSerialization() - configureRouting() - }.start(wait = true) + embeddedServer( + factory = Netty, + port = 8080, + host = "0.0.0.0", + module = Application::exampleModule + ).start(wait = true) +} + +fun Application.exampleModule() { + configureSerialization() + configureRouting() } fun Application.configureSerialization() { @@ -111,7 +118,8 @@ private fun messageToResponse(message: DomainMessage) = when (message) { LastNameTooLong, EmailRequired, EmailTooLong, - EmailInvalid -> + EmailInvalid, + -> HttpStatusCode.BadRequest to "There is an error in your request" // exposed errors @@ -121,7 +129,8 @@ private fun messageToResponse(message: DomainMessage) = when (message) { // internal errors SqlCustomerInvalid, DatabaseTimeout, - is DatabaseError -> + is DatabaseError, + -> HttpStatusCode.InternalServerError to "Internal server error occurred" } diff --git a/example/src/main/kotlin/com/github/michaelbull/result/example/model/domain/Customer.kt b/example/src/main/kotlin/com/github/michaelbull/result/example/model/domain/Customer.kt index 32ec51b..f29c8fb 100644 --- a/example/src/main/kotlin/com/github/michaelbull/result/example/model/domain/Customer.kt +++ b/example/src/main/kotlin/com/github/michaelbull/result/example/model/domain/Customer.kt @@ -2,5 +2,5 @@ package com.github.michaelbull.result.example.model.domain data class Customer( val name: PersonalName, - val email: EmailAddress + val email: EmailAddress, ) diff --git a/example/src/main/kotlin/com/github/michaelbull/result/example/model/domain/EmailAddress.kt b/example/src/main/kotlin/com/github/michaelbull/result/example/model/domain/EmailAddress.kt index 407ba20..af005dc 100644 --- a/example/src/main/kotlin/com/github/michaelbull/result/example/model/domain/EmailAddress.kt +++ b/example/src/main/kotlin/com/github/michaelbull/result/example/model/domain/EmailAddress.kt @@ -1,5 +1,5 @@ package com.github.michaelbull.result.example.model.domain data class EmailAddress( - val address: String + val address: String, ) diff --git a/example/src/main/kotlin/com/github/michaelbull/result/example/model/domain/PersonalName.kt b/example/src/main/kotlin/com/github/michaelbull/result/example/model/domain/PersonalName.kt index afdd02f..9c322a5 100644 --- a/example/src/main/kotlin/com/github/michaelbull/result/example/model/domain/PersonalName.kt +++ b/example/src/main/kotlin/com/github/michaelbull/result/example/model/domain/PersonalName.kt @@ -2,5 +2,5 @@ package com.github.michaelbull.result.example.model.domain data class PersonalName( val first: String, - val last: String + val last: String, ) diff --git a/example/src/main/kotlin/com/github/michaelbull/result/example/model/dto/CustomerDto.kt b/example/src/main/kotlin/com/github/michaelbull/result/example/model/dto/CustomerDto.kt index 70b4ecf..7135eae 100644 --- a/example/src/main/kotlin/com/github/michaelbull/result/example/model/dto/CustomerDto.kt +++ b/example/src/main/kotlin/com/github/michaelbull/result/example/model/dto/CustomerDto.kt @@ -3,5 +3,5 @@ package com.github.michaelbull.result.example.model.dto data class CustomerDto( val firstName: String, val lastName: String, - val email: String + val email: String, ) diff --git a/example/src/main/kotlin/com/github/michaelbull/result/example/model/entity/CustomerEntity.kt b/example/src/main/kotlin/com/github/michaelbull/result/example/model/entity/CustomerEntity.kt index 6b72424..ee75746 100644 --- a/example/src/main/kotlin/com/github/michaelbull/result/example/model/entity/CustomerEntity.kt +++ b/example/src/main/kotlin/com/github/michaelbull/result/example/model/entity/CustomerEntity.kt @@ -8,5 +8,5 @@ data class CustomerEntity( val id: CustomerId, val firstName: String, val lastName: String, - val email: String + val email: String, ) diff --git a/example/src/main/kotlin/com/github/michaelbull/result/example/repository/InMemoryCustomerRepository.kt b/example/src/main/kotlin/com/github/michaelbull/result/example/repository/InMemoryCustomerRepository.kt index 1fdeb18..e9291e0 100644 --- a/example/src/main/kotlin/com/github/michaelbull/result/example/repository/InMemoryCustomerRepository.kt +++ b/example/src/main/kotlin/com/github/michaelbull/result/example/repository/InMemoryCustomerRepository.kt @@ -5,7 +5,7 @@ import com.github.michaelbull.result.example.model.entity.CustomerId import java.sql.SQLTimeoutException class InMemoryCustomerRepository( - private val customers: MutableMap + private val customers: MutableMap, ) : CustomerRepository { override fun findById(id: CustomerId): CustomerEntity? { diff --git a/example/src/main/kotlin/com/github/michaelbull/result/example/service/CustomerService.kt b/example/src/main/kotlin/com/github/michaelbull/result/example/service/CustomerService.kt index 7e1a9df..d39cd16 100644 --- a/example/src/main/kotlin/com/github/michaelbull/result/example/service/CustomerService.kt +++ b/example/src/main/kotlin/com/github/michaelbull/result/example/service/CustomerService.kt @@ -29,7 +29,7 @@ import com.github.michaelbull.result.zip import java.sql.SQLTimeoutException class CustomerService( - private val repository: CustomerRepository + private val repository: CustomerRepository, ) { fun getById(id: Long): Result { diff --git a/kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/binding/SuspendableBinding.kt b/kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/binding/SuspendableBinding.kt index 96122e8..13c5c59 100644 --- a/kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/binding/SuspendableBinding.kt +++ b/kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/binding/SuspendableBinding.kt @@ -45,7 +45,7 @@ public interface SuspendableResultBinding : CoroutineScope { @PublishedApi internal class SuspendableResultBindingImpl( - override val coroutineContext: CoroutineContext + override val coroutineContext: CoroutineContext, ) : SuspendableResultBinding { private val mutex = Mutex() diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Iterable.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Iterable.kt index f15df9a..051e40a 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Iterable.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Iterable.kt @@ -138,7 +138,7 @@ public fun Iterable>.partition(): Pair, List> { * transformation fails. Elements in the returned list are in the input [Iterable] order. */ public inline fun Iterable.mapResult( - transform: (V) -> Result + transform: (V) -> Result, ): Result, E> { return Ok(map { element -> when (val transformed = transform(element)) { @@ -155,7 +155,7 @@ public inline fun Iterable.mapResult( */ public inline fun > Iterable.mapResultTo( destination: C, - transform: (V) -> Result + transform: (V) -> Result, ): Result { return Ok(mapTo(destination) { element -> when (val transformed = transform(element)) { @@ -172,7 +172,7 @@ public inline fun > Iterable.mapResultTo * order. */ public inline fun Iterable.mapResultNotNull( - transform: (V) -> Result? + transform: (V) -> Result?, ): Result, E> { return Ok(mapNotNull { element -> when (val transformed = transform(element)) { @@ -190,7 +190,7 @@ public inline fun Iterable.mapResultNotNull( */ public inline fun > Iterable.mapResultNotNullTo( destination: C, - transform: (V) -> Result? + transform: (V) -> Result?, ): Result { return Ok(mapNotNullTo(destination) { element -> when (val transformed = transform(element)) { @@ -208,7 +208,7 @@ public inline fun > Iterable.mapRe * order. */ public inline fun Iterable.mapResultIndexed( - transform: (index: Int, V) -> Result + transform: (index: Int, V) -> Result, ): Result, E> { return Ok(mapIndexed { index, element -> when (val transformed = transform(index, element)) { @@ -225,7 +225,7 @@ public inline fun Iterable.mapResultIndexed( */ public inline fun > Iterable.mapResultIndexedTo( destination: C, - transform: (index: Int, V) -> Result + transform: (index: Int, V) -> Result, ): Result { return Ok(mapIndexedTo(destination) { index, element -> when (val transformed = transform(index, element)) { @@ -242,7 +242,7 @@ public inline fun > Iterable.mapResultIn * the input [Iterable] order. */ public inline fun Iterable.mapResultIndexedNotNull( - transform: (index: Int, V) -> Result? + transform: (index: Int, V) -> Result?, ): Result, E> { return Ok(mapIndexedNotNull { index, element -> when (val transformed = transform(index, element)) { @@ -260,7 +260,7 @@ public inline fun Iterable.mapResultIndexedNotNull( */ public inline fun > Iterable.mapResultIndexedNotNullTo( destination: C, - transform: (index: Int, V) -> Result? + transform: (index: Int, V) -> Result?, ): Result { return Ok(mapIndexedNotNullTo(destination) { index, element -> when (val transformed = transform(index, element)) { diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Recover.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Recover.kt index 72756ec..2e358a4 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Recover.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Recover.kt @@ -100,7 +100,7 @@ public inline fun Result.andThenRecover(transform: (E) -> Result Result.andThenRecoverIf( predicate: (E) -> Boolean, - transform: (E) -> Result + transform: (E) -> Result, ): Result { contract { callsInPlace(predicate, InvocationKind.AT_MOST_ONCE) @@ -123,7 +123,7 @@ public inline fun Result.andThenRecoverIf( */ public inline fun Result.andThenRecoverUnless( predicate: (E) -> Boolean, - transform: (E) -> Result + transform: (E) -> Result, ): Result { contract { callsInPlace(predicate, InvocationKind.AT_MOST_ONCE) diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Zip.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Zip.kt index 4f5d7fc..89a0dad 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Zip.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Zip.kt @@ -14,7 +14,7 @@ private typealias Producer = () -> Result public inline fun zip( producer1: Producer, producer2: Producer, - transform: (T1, T2) -> V + transform: (T1, T2) -> V, ): Result { contract { callsInPlace(producer1, InvocationKind.EXACTLY_ONCE) @@ -39,7 +39,7 @@ public inline fun zip( producer1: Producer, producer2: Producer, producer3: Producer, - transform: (T1, T2, T3) -> V + transform: (T1, T2, T3) -> V, ): Result { contract { callsInPlace(producer1, InvocationKind.EXACTLY_ONCE) @@ -68,7 +68,7 @@ public inline fun zip( producer2: Producer, producer3: Producer, producer4: Producer, - transform: (T1, T2, T3, T4) -> V + transform: (T1, T2, T3, T4) -> V, ): Result { contract { callsInPlace(producer1, InvocationKind.EXACTLY_ONCE) @@ -101,7 +101,7 @@ public inline fun zip( producer3: Producer, producer4: Producer, producer5: Producer, - transform: (T1, T2, T3, T4, T5) -> V + transform: (T1, T2, T3, T4, T5) -> V, ): Result { contract { callsInPlace(producer1, InvocationKind.EXACTLY_ONCE) diff --git a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/FactoryTest.kt b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/FactoryTest.kt index 0fa3666..050bec8 100644 --- a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/FactoryTest.kt +++ b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/FactoryTest.kt @@ -4,6 +4,7 @@ import kotlin.test.Test import kotlin.test.assertEquals class FactoryTest { + class RunCatching { @Test diff --git a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/GetTest.kt b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/GetTest.kt index c23fc9c..2a55f01 100644 --- a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/GetTest.kt +++ b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/GetTest.kt @@ -7,7 +7,9 @@ import kotlin.test.assertNull @Suppress("IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION") class GetTest { + class Get { + @Test fun returnsValueIfOk() { assertEquals( @@ -23,6 +25,7 @@ class GetTest { } class GetError { + @Test fun returnsNullIfOk() { assertNull(Ok("example").getError()) @@ -38,6 +41,7 @@ class GetTest { } class GetOr { + @Test fun returnsValueIfOk() { assertEquals( @@ -56,6 +60,7 @@ class GetTest { } class GetOrThrow { + @Test fun returnsValueIfOk() { assertEquals( @@ -75,6 +80,7 @@ class GetTest { } class GetOrThrowWithTransform { + @Test fun returnsValueIfOk() { assertEquals( @@ -94,6 +100,7 @@ class GetTest { } class GetErrorOr { + @Test fun returnsDefaultValueIfOk() { assertEquals( @@ -112,6 +119,7 @@ class GetTest { } class GetOrElse { + @Test fun returnsValueIfOk() { assertEquals( @@ -130,6 +138,7 @@ class GetTest { } class GetErrorOrElse { + @Test fun returnsTransformedValueIfOk() { assertEquals( diff --git a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/OrTest.kt b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/OrTest.kt index 0dc6cda..3660b97 100644 --- a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/OrTest.kt +++ b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/OrTest.kt @@ -27,6 +27,7 @@ class OrTest { } class OrElse { + @Test fun returnsValueIfOk() { assertEquals( diff --git a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/UnwrapTest.kt b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/UnwrapTest.kt index 1db67e8..77baac3 100644 --- a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/UnwrapTest.kt +++ b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/UnwrapTest.kt @@ -5,7 +5,9 @@ import kotlin.test.assertEquals import kotlin.test.assertFailsWith class UnwrapTest { + class Unwrap { + @Test fun returnsValueIfOk() { assertEquals(