Reformat project

This commit is contained in:
Michael Bull 2024-03-06 01:42:50 +00:00
parent 104f6a8ecd
commit 5c4635b655
16 changed files with 50 additions and 28 deletions

View File

@ -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"
}

View File

@ -2,5 +2,5 @@ package com.github.michaelbull.result.example.model.domain
data class Customer(
val name: PersonalName,
val email: EmailAddress
val email: EmailAddress,
)

View File

@ -1,5 +1,5 @@
package com.github.michaelbull.result.example.model.domain
data class EmailAddress(
val address: String
val address: String,
)

View File

@ -2,5 +2,5 @@ package com.github.michaelbull.result.example.model.domain
data class PersonalName(
val first: String,
val last: String
val last: String,
)

View File

@ -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,
)

View File

@ -8,5 +8,5 @@ data class CustomerEntity(
val id: CustomerId,
val firstName: String,
val lastName: String,
val email: String
val email: String,
)

View File

@ -5,7 +5,7 @@ import com.github.michaelbull.result.example.model.entity.CustomerId
import java.sql.SQLTimeoutException
class InMemoryCustomerRepository(
private val customers: MutableMap<CustomerId, CustomerEntity>
private val customers: MutableMap<CustomerId, CustomerEntity>,
) : CustomerRepository {
override fun findById(id: CustomerId): CustomerEntity? {

View File

@ -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<CustomerDto, DomainMessage> {

View File

@ -45,7 +45,7 @@ public interface SuspendableResultBinding<E> : CoroutineScope {
@PublishedApi
internal class SuspendableResultBindingImpl<E>(
override val coroutineContext: CoroutineContext
override val coroutineContext: CoroutineContext,
) : SuspendableResultBinding<E> {
private val mutex = Mutex()

View File

@ -138,7 +138,7 @@ public fun <V, E> Iterable<Result<V, E>>.partition(): Pair<List<V>, List<E>> {
* transformation fails. Elements in the returned list are in the input [Iterable] order.
*/
public inline fun <V, E, U> Iterable<V>.mapResult(
transform: (V) -> Result<U, E>
transform: (V) -> Result<U, E>,
): Result<List<U>, E> {
return Ok(map { element ->
when (val transformed = transform(element)) {
@ -155,7 +155,7 @@ public inline fun <V, E, U> Iterable<V>.mapResult(
*/
public inline fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultTo(
destination: C,
transform: (V) -> Result<U, E>
transform: (V) -> Result<U, E>,
): Result<C, E> {
return Ok(mapTo(destination) { element ->
when (val transformed = transform(element)) {
@ -172,7 +172,7 @@ public inline fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultTo
* order.
*/
public inline fun <V, E, U : Any> Iterable<V>.mapResultNotNull(
transform: (V) -> Result<U, E>?
transform: (V) -> Result<U, E>?,
): Result<List<U>, E> {
return Ok(mapNotNull { element ->
when (val transformed = transform(element)) {
@ -190,7 +190,7 @@ public inline fun <V, E, U : Any> Iterable<V>.mapResultNotNull(
*/
public inline fun <V, E, U : Any, C : MutableCollection<in U>> Iterable<V>.mapResultNotNullTo(
destination: C,
transform: (V) -> Result<U, E>?
transform: (V) -> Result<U, E>?,
): Result<C, E> {
return Ok(mapNotNullTo(destination) { element ->
when (val transformed = transform(element)) {
@ -208,7 +208,7 @@ public inline fun <V, E, U : Any, C : MutableCollection<in U>> Iterable<V>.mapRe
* order.
*/
public inline fun <V, E, U> Iterable<V>.mapResultIndexed(
transform: (index: Int, V) -> Result<U, E>
transform: (index: Int, V) -> Result<U, E>,
): Result<List<U>, E> {
return Ok(mapIndexed { index, element ->
when (val transformed = transform(index, element)) {
@ -225,7 +225,7 @@ public inline fun <V, E, U> Iterable<V>.mapResultIndexed(
*/
public inline fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultIndexedTo(
destination: C,
transform: (index: Int, V) -> Result<U, E>
transform: (index: Int, V) -> Result<U, E>,
): Result<C, E> {
return Ok(mapIndexedTo(destination) { index, element ->
when (val transformed = transform(index, element)) {
@ -242,7 +242,7 @@ public inline fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultIn
* the input [Iterable] order.
*/
public inline fun <V, E, U : Any> Iterable<V>.mapResultIndexedNotNull(
transform: (index: Int, V) -> Result<U, E>?
transform: (index: Int, V) -> Result<U, E>?,
): Result<List<U>, E> {
return Ok(mapIndexedNotNull { index, element ->
when (val transformed = transform(index, element)) {
@ -260,7 +260,7 @@ public inline fun <V, E, U : Any> Iterable<V>.mapResultIndexedNotNull(
*/
public inline fun <V, E, U : Any, C : MutableCollection<in U>> Iterable<V>.mapResultIndexedNotNullTo(
destination: C,
transform: (index: Int, V) -> Result<U, E>?
transform: (index: Int, V) -> Result<U, E>?,
): Result<C, E> {
return Ok(mapIndexedNotNullTo(destination) { index, element ->
when (val transformed = transform(index, element)) {

View File

@ -100,7 +100,7 @@ public inline fun <V, E> Result<V, E>.andThenRecover(transform: (E) -> Result<V,
*/
public inline fun <V, E> Result<V, E>.andThenRecoverIf(
predicate: (E) -> Boolean,
transform: (E) -> Result<V, E>
transform: (E) -> Result<V, E>,
): Result<V, E> {
contract {
callsInPlace(predicate, InvocationKind.AT_MOST_ONCE)
@ -123,7 +123,7 @@ public inline fun <V, E> Result<V, E>.andThenRecoverIf(
*/
public inline fun <V, E> Result<V, E>.andThenRecoverUnless(
predicate: (E) -> Boolean,
transform: (E) -> Result<V, E>
transform: (E) -> Result<V, E>,
): Result<V, E> {
contract {
callsInPlace(predicate, InvocationKind.AT_MOST_ONCE)

View File

@ -14,7 +14,7 @@ private typealias Producer<T, E> = () -> Result<T, E>
public inline fun <T1, T2, E, V> zip(
producer1: Producer<T1, E>,
producer2: Producer<T2, E>,
transform: (T1, T2) -> V
transform: (T1, T2) -> V,
): Result<V, E> {
contract {
callsInPlace(producer1, InvocationKind.EXACTLY_ONCE)
@ -39,7 +39,7 @@ public inline fun <T1, T2, T3, E, V> zip(
producer1: Producer<T1, E>,
producer2: Producer<T2, E>,
producer3: Producer<T3, E>,
transform: (T1, T2, T3) -> V
transform: (T1, T2, T3) -> V,
): Result<V, E> {
contract {
callsInPlace(producer1, InvocationKind.EXACTLY_ONCE)
@ -68,7 +68,7 @@ public inline fun <T1, T2, T3, T4, E, V> zip(
producer2: Producer<T2, E>,
producer3: Producer<T3, E>,
producer4: Producer<T4, E>,
transform: (T1, T2, T3, T4) -> V
transform: (T1, T2, T3, T4) -> V,
): Result<V, E> {
contract {
callsInPlace(producer1, InvocationKind.EXACTLY_ONCE)
@ -101,7 +101,7 @@ public inline fun <T1, T2, T3, T4, T5, E, V> zip(
producer3: Producer<T3, E>,
producer4: Producer<T4, E>,
producer5: Producer<T5, E>,
transform: (T1, T2, T3, T4, T5) -> V
transform: (T1, T2, T3, T4, T5) -> V,
): Result<V, E> {
contract {
callsInPlace(producer1, InvocationKind.EXACTLY_ONCE)

View File

@ -4,6 +4,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals
class FactoryTest {
class RunCatching {
@Test

View File

@ -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(

View File

@ -27,6 +27,7 @@ class OrTest {
}
class OrElse {
@Test
fun returnsValueIfOk() {
assertEquals(

View File

@ -5,7 +5,9 @@ import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
class UnwrapTest {
class Unwrap {
@Test
fun returnsValueIfOk() {
assertEquals(