Skip to content

Commit

Permalink
Reformat project
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbull committed Mar 6, 2024
1 parent 7c628cc commit 8dc1cd0
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package com.github.michaelbull.result.example.model.domain

data class Customer(
val name: PersonalName,
val email: EmailAddress
val email: EmailAddress,
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.github.michaelbull.result.example.model.domain

data class EmailAddress(
val address: String
val address: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package com.github.michaelbull.result.example.model.domain

data class PersonalName(
val first: String,
val last: String
val last: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ data class CustomerEntity(
val id: CustomerId,
val firstName: String,
val lastName: String,
val email: String
val email: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals

class FactoryTest {

class RunCatching {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import kotlin.test.assertNull

@Suppress("IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION")
class GetTest {

class Get {

@Test
fun returnsValueIfOk() {
assertEquals(
Expand All @@ -23,6 +25,7 @@ class GetTest {
}

class GetError {

@Test
fun returnsNullIfOk() {
assertNull(Ok("example").getError())
Expand All @@ -38,6 +41,7 @@ class GetTest {
}

class GetOr {

@Test
fun returnsValueIfOk() {
assertEquals(
Expand All @@ -56,6 +60,7 @@ class GetTest {
}

class GetOrThrow {

@Test
fun returnsValueIfOk() {
assertEquals(
Expand All @@ -75,6 +80,7 @@ class GetTest {
}

class GetOrThrowWithTransform {

@Test
fun returnsValueIfOk() {
assertEquals(
Expand All @@ -94,6 +100,7 @@ class GetTest {
}

class GetErrorOr {

@Test
fun returnsDefaultValueIfOk() {
assertEquals(
Expand All @@ -112,6 +119,7 @@ class GetTest {
}

class GetOrElse {

@Test
fun returnsValueIfOk() {
assertEquals(
Expand All @@ -130,6 +138,7 @@ class GetTest {
}

class GetErrorOrElse {

@Test
fun returnsTransformedValueIfOk() {
assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class OrTest {
}

class OrElse {

@Test
fun returnsValueIfOk() {
assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

class UnwrapTest {

class Unwrap {

@Test
fun returnsValueIfOk() {
assertEquals(
Expand Down

0 comments on commit 8dc1cd0

Please sign in to comment.