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 import io.ktor.server.routing.routing
fun main() { fun main() {
embeddedServer(Netty, port = 8080, host = "0.0.0.0") { embeddedServer(
factory = Netty,
port = 8080,
host = "0.0.0.0",
module = Application::exampleModule
).start(wait = true)
}
fun Application.exampleModule() {
configureSerialization() configureSerialization()
configureRouting() configureRouting()
}.start(wait = true)
} }
fun Application.configureSerialization() { fun Application.configureSerialization() {
@ -111,7 +118,8 @@ private fun messageToResponse(message: DomainMessage) = when (message) {
LastNameTooLong, LastNameTooLong,
EmailRequired, EmailRequired,
EmailTooLong, EmailTooLong,
EmailInvalid -> EmailInvalid,
->
HttpStatusCode.BadRequest to "There is an error in your request" HttpStatusCode.BadRequest to "There is an error in your request"
// exposed errors // exposed errors
@ -121,7 +129,8 @@ private fun messageToResponse(message: DomainMessage) = when (message) {
// internal errors // internal errors
SqlCustomerInvalid, SqlCustomerInvalid,
DatabaseTimeout, DatabaseTimeout,
is DatabaseError -> is DatabaseError,
->
HttpStatusCode.InternalServerError to "Internal server error occurred" HttpStatusCode.InternalServerError to "Internal server error occurred"
} }

View File

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

View File

@ -1,5 +1,5 @@
package com.github.michaelbull.result.example.model.domain package com.github.michaelbull.result.example.model.domain
data class EmailAddress( 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( data class PersonalName(
val first: String, 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( data class CustomerDto(
val firstName: String, val firstName: String,
val lastName: String, val lastName: String,
val email: String val email: String,
) )

View File

@ -8,5 +8,5 @@ data class CustomerEntity(
val id: CustomerId, val id: CustomerId,
val firstName: String, val firstName: String,
val lastName: 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 import java.sql.SQLTimeoutException
class InMemoryCustomerRepository( class InMemoryCustomerRepository(
private val customers: MutableMap<CustomerId, CustomerEntity> private val customers: MutableMap<CustomerId, CustomerEntity>,
) : CustomerRepository { ) : CustomerRepository {
override fun findById(id: CustomerId): CustomerEntity? { override fun findById(id: CustomerId): CustomerEntity? {

View File

@ -29,7 +29,7 @@ import com.github.michaelbull.result.zip
import java.sql.SQLTimeoutException import java.sql.SQLTimeoutException
class CustomerService( class CustomerService(
private val repository: CustomerRepository private val repository: CustomerRepository,
) { ) {
fun getById(id: Long): Result<CustomerDto, DomainMessage> { fun getById(id: Long): Result<CustomerDto, DomainMessage> {

View File

@ -45,7 +45,7 @@ public interface SuspendableResultBinding<E> : CoroutineScope {
@PublishedApi @PublishedApi
internal class SuspendableResultBindingImpl<E>( internal class SuspendableResultBindingImpl<E>(
override val coroutineContext: CoroutineContext override val coroutineContext: CoroutineContext,
) : SuspendableResultBinding<E> { ) : SuspendableResultBinding<E> {
private val mutex = Mutex() 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. * transformation fails. Elements in the returned list are in the input [Iterable] order.
*/ */
public inline fun <V, E, U> Iterable<V>.mapResult( public inline fun <V, E, U> Iterable<V>.mapResult(
transform: (V) -> Result<U, E> transform: (V) -> Result<U, E>,
): Result<List<U>, E> { ): Result<List<U>, E> {
return Ok(map { element -> return Ok(map { element ->
when (val transformed = transform(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( public inline fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultTo(
destination: C, destination: C,
transform: (V) -> Result<U, E> transform: (V) -> Result<U, E>,
): Result<C, E> { ): Result<C, E> {
return Ok(mapTo(destination) { element -> return Ok(mapTo(destination) { element ->
when (val transformed = transform(element)) { when (val transformed = transform(element)) {
@ -172,7 +172,7 @@ public inline fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultTo
* order. * order.
*/ */
public inline fun <V, E, U : Any> Iterable<V>.mapResultNotNull( public inline fun <V, E, U : Any> Iterable<V>.mapResultNotNull(
transform: (V) -> Result<U, E>? transform: (V) -> Result<U, E>?,
): Result<List<U>, E> { ): Result<List<U>, E> {
return Ok(mapNotNull { element -> return Ok(mapNotNull { element ->
when (val transformed = transform(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( public inline fun <V, E, U : Any, C : MutableCollection<in U>> Iterable<V>.mapResultNotNullTo(
destination: C, destination: C,
transform: (V) -> Result<U, E>? transform: (V) -> Result<U, E>?,
): Result<C, E> { ): Result<C, E> {
return Ok(mapNotNullTo(destination) { element -> return Ok(mapNotNullTo(destination) { element ->
when (val transformed = transform(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. * order.
*/ */
public inline fun <V, E, U> Iterable<V>.mapResultIndexed( 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> { ): Result<List<U>, E> {
return Ok(mapIndexed { index, element -> return Ok(mapIndexed { index, element ->
when (val transformed = transform(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( public inline fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultIndexedTo(
destination: C, destination: C,
transform: (index: Int, V) -> Result<U, E> transform: (index: Int, V) -> Result<U, E>,
): Result<C, E> { ): Result<C, E> {
return Ok(mapIndexedTo(destination) { index, element -> return Ok(mapIndexedTo(destination) { index, element ->
when (val transformed = transform(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. * the input [Iterable] order.
*/ */
public inline fun <V, E, U : Any> Iterable<V>.mapResultIndexedNotNull( 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> { ): Result<List<U>, E> {
return Ok(mapIndexedNotNull { index, element -> return Ok(mapIndexedNotNull { index, element ->
when (val transformed = transform(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( public inline fun <V, E, U : Any, C : MutableCollection<in U>> Iterable<V>.mapResultIndexedNotNullTo(
destination: C, destination: C,
transform: (index: Int, V) -> Result<U, E>? transform: (index: Int, V) -> Result<U, E>?,
): Result<C, E> { ): Result<C, E> {
return Ok(mapIndexedNotNullTo(destination) { index, element -> return Ok(mapIndexedNotNullTo(destination) { index, element ->
when (val transformed = transform(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( public inline fun <V, E> Result<V, E>.andThenRecoverIf(
predicate: (E) -> Boolean, predicate: (E) -> Boolean,
transform: (E) -> Result<V, E> transform: (E) -> Result<V, E>,
): Result<V, E> { ): Result<V, E> {
contract { contract {
callsInPlace(predicate, InvocationKind.AT_MOST_ONCE) 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( public inline fun <V, E> Result<V, E>.andThenRecoverUnless(
predicate: (E) -> Boolean, predicate: (E) -> Boolean,
transform: (E) -> Result<V, E> transform: (E) -> Result<V, E>,
): Result<V, E> { ): Result<V, E> {
contract { contract {
callsInPlace(predicate, InvocationKind.AT_MOST_ONCE) 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( public inline fun <T1, T2, E, V> zip(
producer1: Producer<T1, E>, producer1: Producer<T1, E>,
producer2: Producer<T2, E>, producer2: Producer<T2, E>,
transform: (T1, T2) -> V transform: (T1, T2) -> V,
): Result<V, E> { ): Result<V, E> {
contract { contract {
callsInPlace(producer1, InvocationKind.EXACTLY_ONCE) callsInPlace(producer1, InvocationKind.EXACTLY_ONCE)
@ -39,7 +39,7 @@ public inline fun <T1, T2, T3, E, V> zip(
producer1: Producer<T1, E>, producer1: Producer<T1, E>,
producer2: Producer<T2, E>, producer2: Producer<T2, E>,
producer3: Producer<T3, E>, producer3: Producer<T3, E>,
transform: (T1, T2, T3) -> V transform: (T1, T2, T3) -> V,
): Result<V, E> { ): Result<V, E> {
contract { contract {
callsInPlace(producer1, InvocationKind.EXACTLY_ONCE) callsInPlace(producer1, InvocationKind.EXACTLY_ONCE)
@ -68,7 +68,7 @@ public inline fun <T1, T2, T3, T4, E, V> zip(
producer2: Producer<T2, E>, producer2: Producer<T2, E>,
producer3: Producer<T3, E>, producer3: Producer<T3, E>,
producer4: Producer<T4, E>, producer4: Producer<T4, E>,
transform: (T1, T2, T3, T4) -> V transform: (T1, T2, T3, T4) -> V,
): Result<V, E> { ): Result<V, E> {
contract { contract {
callsInPlace(producer1, InvocationKind.EXACTLY_ONCE) callsInPlace(producer1, InvocationKind.EXACTLY_ONCE)
@ -101,7 +101,7 @@ public inline fun <T1, T2, T3, T4, T5, E, V> zip(
producer3: Producer<T3, E>, producer3: Producer<T3, E>,
producer4: Producer<T4, E>, producer4: Producer<T4, E>,
producer5: Producer<T5, E>, producer5: Producer<T5, E>,
transform: (T1, T2, T3, T4, T5) -> V transform: (T1, T2, T3, T4, T5) -> V,
): Result<V, E> { ): Result<V, E> {
contract { contract {
callsInPlace(producer1, InvocationKind.EXACTLY_ONCE) callsInPlace(producer1, InvocationKind.EXACTLY_ONCE)

View File

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

View File

@ -7,7 +7,9 @@ import kotlin.test.assertNull
@Suppress("IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION") @Suppress("IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION")
class GetTest { class GetTest {
class Get { class Get {
@Test @Test
fun returnsValueIfOk() { fun returnsValueIfOk() {
assertEquals( assertEquals(
@ -23,6 +25,7 @@ class GetTest {
} }
class GetError { class GetError {
@Test @Test
fun returnsNullIfOk() { fun returnsNullIfOk() {
assertNull(Ok("example").getError()) assertNull(Ok("example").getError())
@ -38,6 +41,7 @@ class GetTest {
} }
class GetOr { class GetOr {
@Test @Test
fun returnsValueIfOk() { fun returnsValueIfOk() {
assertEquals( assertEquals(
@ -56,6 +60,7 @@ class GetTest {
} }
class GetOrThrow { class GetOrThrow {
@Test @Test
fun returnsValueIfOk() { fun returnsValueIfOk() {
assertEquals( assertEquals(
@ -75,6 +80,7 @@ class GetTest {
} }
class GetOrThrowWithTransform { class GetOrThrowWithTransform {
@Test @Test
fun returnsValueIfOk() { fun returnsValueIfOk() {
assertEquals( assertEquals(
@ -94,6 +100,7 @@ class GetTest {
} }
class GetErrorOr { class GetErrorOr {
@Test @Test
fun returnsDefaultValueIfOk() { fun returnsDefaultValueIfOk() {
assertEquals( assertEquals(
@ -112,6 +119,7 @@ class GetTest {
} }
class GetOrElse { class GetOrElse {
@Test @Test
fun returnsValueIfOk() { fun returnsValueIfOk() {
assertEquals( assertEquals(
@ -130,6 +138,7 @@ class GetTest {
} }
class GetErrorOrElse { class GetErrorOrElse {
@Test @Test
fun returnsTransformedValueIfOk() { fun returnsTransformedValueIfOk() {
assertEquals( assertEquals(

View File

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

View File

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